Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sdk/go] Simplify Apply method options to reduce binary size #6607

Merged
merged 6 commits into from Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions CHANGELOG_PENDING.md
Expand Up @@ -3,20 +3,23 @@
- [CLI] Standardize the `--stack` flag to *not* set the stack as current (i.e. setStack=false) across CLI commands.
[#6300](https://github.com/pulumi/pulumi/pull/6300)

- [sdk/go] Simplify `Apply` method options to reduce binary size
[#6607](https://github.com/pulumi/pulumi/pull/6607)

- [Automation/*] All operations use `--stack` to specify the stack instead of running `select stack` before the operation.
[#6300](https://github.com/pulumi/pulumi/pull/6300)

- [Automation/go] Moving go automation API package from sdk/v2/go/x/auto -> sdk/v2/go/auto
[#6518](https://github.com/pulumi/pulumi/pull/6518)

- [Automation/nodejs] Moving NodeJS automation API package from sdk/nodejs/x/automation -> sdk/nodejs/automation
[#6518](https://github.com/pulumi/pulumi/pull/6518)

- [Automation/python] Moving Python automation API package from pulumi.x.automation -> pulumi.automation
[#6518](https://github.com/pulumi/pulumi/pull/6518)



### Enhancements



### Bug Fixes
16 changes: 8 additions & 8 deletions sdk/go/pulumi/alias.go
Expand Up @@ -82,19 +82,19 @@ func (a Alias) collapseToURN(defaultName, defaultType string, defaultParent Reso
func CreateURN(name, t, parent, project, stack StringInput) URNOutput {
var parentPrefix StringInput
if parent != nil {
parentPrefix = parent.ToStringOutput().ApplyString(func(p string) string {
parentPrefix = parent.ToStringOutput().ApplyT(func(p string) string {
return p[0:strings.LastIndex(p, "::")] + "$"
})
}).(StringOutput)
} else {
parentPrefix = All(stack, project).ApplyString(func(a []interface{}) string {
parentPrefix = All(stack, project).ApplyT(func(a []interface{}) string {
return "urn:pulumi:" + a[0].(string) + "::" + a[1].(string) + "::"
})
}).(StringOutput)

}

return All(parentPrefix, t, name).ApplyURN(func(a []interface{}) URN {
return All(parentPrefix, t, name).ApplyT(func(a []interface{}) URN {
return URN(a[0].(string) + a[1].(string) + "::" + a[2].(string))
})
}).(URNOutput)
}

// inheritedChildAlias computes the alias that should be applied to a child based on an alias applied to it's parent.
Expand All @@ -103,10 +103,10 @@ func CreateURN(name, t, parent, project, stack StringInput) URNOutput {
func inheritedChildAlias(childName, parentName, childType, project, stack string, parentURN URNOutput) URNOutput {
aliasName := StringInput(String(childName))
if strings.HasPrefix(childName, parentName) {
aliasName = parentURN.ApplyString(func(urn URN) string {
aliasName = parentURN.ApplyT(func(urn URN) string {
parentPrefix := urn[strings.LastIndex(string(urn), "::")+2:]
return string(parentPrefix) + childName[len(parentName):]
})
}).(StringOutput)
}
return CreateURN(aliasName, String(childType), parentURN, String(project), String(stack))
}
8 changes: 4 additions & 4 deletions sdk/go/pulumi/stack_reference.go
Expand Up @@ -23,20 +23,20 @@ func (s *StackReference) GetOutput(name StringInput) AnyOutput {

// GetStringOutput returns a stack output keyed by the given name as an StringOutput
func (s *StackReference) GetStringOutput(name StringInput) StringOutput {
return s.GetOutput(name).ApplyString(func(out interface{}) string {
return s.GetOutput(name).ApplyT(func(out interface{}) string {
var res string
if out != nil {
res = out.(string)
}
return res
})
}).(StringOutput)
}

// GetIDOutput returns a stack output keyed by the given name as an IDOutput
func (s *StackReference) GetIDOutput(name StringInput) IDOutput {
return s.GetStringOutput(name).ApplyID(func(out string) ID {
return s.GetStringOutput(name).ApplyT(func(out string) ID {
return ID(out)
})
}).(IDOutput)
}

type stackReferenceArgs struct {
Expand Down
13 changes: 0 additions & 13 deletions sdk/go/pulumi/templates/types_builtins.go.template
Expand Up @@ -20,19 +20,6 @@ import (
"reflect"
)

{{range .Builtins}}
// Apply{{.Name}} is like ApplyT, but returns a {{.Name}}Output.
func (o *OutputState) Apply{{.Name}}(applier interface{}) {{.Name}}Output {
return o.ApplyT(applier).({{.Name}}Output)
}

// Apply{{.Name}}WithContext is like ApplyTWithContext, but returns a {{.Name}}Output.
func (o *OutputState) Apply{{.Name}}WithContext(ctx context.Context, applier interface{}) {{.Name}}Output {
return o.ApplyTWithContext(ctx, applier).({{.Name}}Output)
}
{{end}}


{{with $builtins := .Builtins}}
{{range $builtins}}
var {{.Name | Unexported}}Type = reflect.TypeOf((*{{.ElementType}})(nil)).Elem()
Expand Down
21 changes: 1 addition & 20 deletions sdk/go/pulumi/templates/types_builtins_test.go.template
Expand Up @@ -126,25 +126,6 @@ func TestOutputApply(t *testing.T) {
assert.NotNil(t, err)
assert.Nil(t, v)
}
// Test builtin applies.
{
out := newIntOutput()
go func() { out.resolve(42, true, false, nil) }()

{{range .Builtins}}
t.Run("Apply{{.Name}}", func(t *testing.T) {
o2 := out.Apply{{.Name}}(func(v int) {{.ElementType}} { return *new({{.ElementType}}) })
_, known, _, _, err := await(o2)
assert.True(t, known)
assert.NoError(t, err)

o2 = out.Apply{{.Name}}WithContext(context.Background(), func(_ context.Context, v int) {{.ElementType}} { return *new({{.ElementType}}) })
_, known, _, _, err = await(o2)
assert.True(t, known)
assert.NoError(t, err)
})
{{end}}
}
// Test that applies return appropriate concrete implementations of Output based on the callback type
{
out := newIntOutput()
Expand Down Expand Up @@ -220,7 +201,7 @@ func TestOutputApply(t *testing.T) {
}
})

res5 := All(res3, res4).Apply(func (v interface{}) (interface{}, error) {
res5 := All(res3, res4).ApplyT(func (v interface{}) (interface{}, error) {
vs := v.([]interface{})
res3 := vs[0].(string)
res4 := vs[1].(*myStructType)
Expand Down
21 changes: 1 addition & 20 deletions sdk/go/pulumi/types.go
Expand Up @@ -32,13 +32,10 @@ import (
type Output interface {
ElementType() reflect.Type

Apply(applier func(interface{}) (interface{}, error)) AnyOutput
ApplyWithContext(ctx context.Context, applier func(context.Context, interface{}) (interface{}, error)) AnyOutput
ApplyT(applier interface{}) Output
ApplyTWithContext(ctx context.Context, applier interface{}) Output

getState() *OutputState
IsSecret() bool
}

var outputType = reflect.TypeOf((*Output)(nil)).Elem()
Expand Down Expand Up @@ -308,22 +305,6 @@ func checkApplier(fn interface{}, elementType reflect.Type) reflect.Value {
return fv
}

// Apply transforms the data of the output property using the applier func. The result remains an output
// property, and accumulates all implicated dependencies, so that resources can be properly tracked using a DAG.
// This function does not block awaiting the value; instead, it spawns a Goroutine that will await its availability.
func (o *OutputState) Apply(applier func(interface{}) (interface{}, error)) AnyOutput {
return o.ApplyWithContext(context.Background(), func(_ context.Context, v interface{}) (interface{}, error) {
return applier(v)
})
}

// ApplyWithContext transforms the data of the output property using the applier func. The result remains an output
// property, and accumulates all implicated dependencies, so that resources can be properly tracked using a DAG.
// This function does not block awaiting the value; instead, it spawns a Goroutine that will await its availability.
func (o *OutputState) ApplyWithContext(ctx context.Context, applier func(context.Context, interface{}) (interface{}, error)) AnyOutput {
return o.ApplyTWithContext(ctx, applier).(AnyOutput)
}

// ApplyT transforms the data of the output property using the applier func. The result remains an output
// property, and accumulates all implicated dependencies, so that resources can be properly tracked using a DAG.
// This function does not block awaiting the value; instead, it spawns a Goroutine that will await its availability.
Expand Down Expand Up @@ -413,7 +394,7 @@ func (o *OutputState) ApplyTWithContext(ctx context.Context, applier interface{}
}

// IsSecret returns a bool representing the secretness of the Output
func (o *OutputState) IsSecret() bool {
func IsSecret(o Output) bool {
return o.getState().secret
}

Expand Down