pulumi/pkg
Zaid Ajaj e88cd05f52
[sdk/program-gen] Implementing DeferredOutput for Go SDK and program-gen (#17885)
### Description

This PR addresses https://github.com/pulumi/pulumi/issues/17788 by
implementing a `pulumi.DeferredOutput(ctx)` function which can be used
for generating Go programs with mutually dependant components. The
implementation is as follows:
```go
// <sdk/go/internal/types.go>
// DeferredOutput creates an Output whose value can be later resolved from another Output instance.
func DeferredOutput[T any](ctx context.Context) (pulumix.Output[T], func(Output)) {
	var zero T
	rt := reflect.TypeOf(zero)
	state := internal.NewOutputState(nil, rt)
	out := pulumix.Output[T]{OutputState: state}
	resolve := func(o Output) {
		go func() {
			v, known, secret, deps, err := internal.AwaitOutput(ctx, o)
			if err != nil {
				internal.RejectOutput(state, err)
				return
			}
			internal.ResolveOutput(out, v, known, secret, deps)
		}()
	}
	return out, resolve
}
```

This PR implements the usage of this function in Go program-gen but the
results are far from ideal:
- ~Usage of deferred output variables requires an explicit cast from
`AnyOutput` to the target output type where it was used~ Now correctly
casting from the generic output type to target component inputs
- Generating lists of referenced components via a `ForExpression` emits
`"TODO: For expression"`

Example generated code in programs:
```go
secondPasswordLength, resolveSecondPasswordLength := pulumi.DeferredOutput[int](ctx)
first, err := NewFirst(ctx, "first", &FirstArgs{
	PasswordLength: pulumix.Cast[pulumi.IntOutput](secondPasswordLength),
})
```

Fixes #17788

---------

Co-authored-by: Julien <julien@caffeine.lu>
2025-01-04 18:47:30 +00:00
..
asset Move some asset code to pkg (#15162) 2024-01-17 11:30:37 +00:00
authhelpers Enable goheader rule and add missing license headers (#15473) 2024-09-09 12:05:45 +00:00
backend retry deletes in a bucket if they fail (#18059) 2024-12-18 09:38:13 +00:00
channel Move `sdk/go/common/channel` to `pkg` (#17895) 2024-12-02 16:25:50 +00:00
cmd/pulumi Only use gRPC to communicate with the Java host (#18138) 2025-01-03 16:48:13 +00:00
codegen [sdk/program-gen] Implementing DeferredOutput for Go SDK and program-gen (#17885) 2025-01-04 18:47:30 +00:00
display Move sdk/go/common/display to /pkg/display (#13954) 2023-09-18 11:01:28 +00:00
engine Enable `pulumi:pulumi:getResource` to hydrate `Read` resources (#18070) 2024-12-22 20:13:43 +00:00
graph Support topologically sorting snapshots (#17403) 2024-10-01 08:45:35 +00:00
importer [cli/import] Fix undefined variable errors in code generation when imported resources use a parent or provider (#16786) 2024-07-25 13:53:44 +00:00
operations Move resource.URN to urn.URN (#15689) 2024-03-14 15:28:32 +00:00
resource Enable `pulumi:pulumi:getResource` to hydrate `Read` resources (#18070) 2024-12-22 20:13:43 +00:00
secrets Enable some more linting rules (#17456) 2024-10-03 17:37:13 +00:00
testing/integration Honor opts.PreviewCommandlineFlags in preview after refresh (#18063) 2024-12-17 19:04:27 +00:00
util Update golangci-lint (#17972) 2024-12-10 11:50:38 +00:00
workspace Implement plugin download cancellation (#17621) 2024-11-12 18:04:25 +00:00
README.md export codegen tests for internal use (#8928) 2022-02-07 12:10:04 +01:00
go.mod Only use gRPC to communicate with the Java host (#18138) 2025-01-03 16:48:13 +00:00
go.sum Only use gRPC to communicate with the Java host (#18138) 2025-01-03 16:48:13 +00:00

README.md

pulumi/pkg

While pulumi/sdk maintains strict backwards compatibility guarantees, code under pkg/ is handled more informally: while breaking changes are still discouraged they may happen when they make sense.