[sdk/go] Add stack output helpers for numeric types (#7410)
This commit is contained in:
parent
a4ee050afc
commit
bd208df180
|
@ -1,5 +1,8 @@
|
|||
### Improvements
|
||||
|
||||
|
||||
- [sdk/go] - Add stack output helpers for numeric types.
|
||||
[#7410](https://github.com/pulumi/pulumi/pull/7410)
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package pulumi
|
||||
|
||||
import "reflect"
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// StackReference manages a reference to a Pulumi stack.
|
||||
type StackReference struct {
|
||||
|
@ -39,6 +42,27 @@ func (s *StackReference) GetIDOutput(name StringInput) IDOutput {
|
|||
}).(IDOutput)
|
||||
}
|
||||
|
||||
// GetFloat64Output returns a stack output keyed by the given name as an Float64Output
|
||||
func (s *StackReference) GetFloat64Output(name StringInput) Float64Output {
|
||||
return s.GetOutput(name).ApplyT(func(out interface{}) (float64, error) {
|
||||
if numf, ok := out.(float64); ok {
|
||||
return numf, nil
|
||||
}
|
||||
return 0.0, fmt.Errorf("failed to convert %T to float64", out)
|
||||
}).(Float64Output)
|
||||
}
|
||||
|
||||
// GetIntOutput returns a stack output keyed by the given name as an IntOutput
|
||||
func (s *StackReference) GetIntOutput(name StringInput) IntOutput {
|
||||
return s.GetOutput(name).ApplyT(func(out interface{}) (int, error) {
|
||||
numf, ok := out.(float64)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("failed to convert %T to int", out)
|
||||
}
|
||||
return int(numf), nil
|
||||
}).(IntOutput)
|
||||
}
|
||||
|
||||
type stackReferenceArgs struct {
|
||||
Name string `pulumi:"name"`
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package pulumi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
|
||||
|
@ -15,6 +16,8 @@ func TestStackReference(t *testing.T) {
|
|||
"zed": map[string]interface{}{
|
||||
"alpha": "beta",
|
||||
},
|
||||
"numf": 123.4,
|
||||
"numi": 567.0,
|
||||
}
|
||||
mocks := &testMonitor{
|
||||
NewResourceF: func(args MockResourceArgs) (string, resource.PropertyMap, error) {
|
||||
|
@ -52,6 +55,18 @@ func TestStackReference(t *testing.T) {
|
|||
zed1, _, _, _, err := await(ref1.GetOutput(String("zed")))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, outputs["zed"], zed1)
|
||||
numf, _, _, _, err := await(ref1.GetFloat64Output(String("numf")))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, outputs["numf"], numf)
|
||||
_, _, _, _, err = await(ref1.GetFloat64Output(String("foo")))
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, fmt.Errorf("failed to convert %T to float64", outputs["foo"]), err)
|
||||
numi, _, _, _, err := await(ref1.GetIntOutput(String("numi")))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, int(outputs["numi"].(float64)), numi)
|
||||
_, _, _, _, err = await(ref1.GetIntOutput(String("foo")))
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, fmt.Errorf("failed to convert %T to int", outputs["foo"]), err)
|
||||
return nil
|
||||
}, WithMocks("project", "stack", mocks))
|
||||
assert.NoError(t, err)
|
||||
|
|
Loading…
Reference in a new issue