pulumi/pkg/codegen/go
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
..
gen_program_test Don't publish test code in `pkg/codegen` (#17517) 2024-10-09 11:09:54 +00:00
doc.go Enable perfsprint linter (#14813) 2023-12-12 12:19:42 +00:00
doc_test.go sdk/go: Remove 'nolint' directives from package docs 2023-01-06 09:06:47 -08:00
embeddedUtilities.go Support returning plain values from methods (#13592) 2023-11-18 06:02:06 +00:00
gen.go Automatically bridge eligible dependencies on pulumi convert from terraform (#17992) 2024-12-23 13:35:55 +00:00
gen_program.go [sdk/program-gen] Implementing DeferredOutput for Go SDK and program-gen (#17885) 2025-01-04 18:47:30 +00:00
gen_program_expression_test.go Enable goheader rule and add missing license headers (#15473) 2024-09-09 12:05:45 +00:00
gen_program_expressions.go [sdk/program-gen] Implementing DeferredOutput for Go SDK and program-gen (#17885) 2025-01-04 18:47:30 +00:00
gen_program_inline_invoke.go Enable goheader rule and add missing license headers (#15473) 2024-09-09 12:05:45 +00:00
gen_program_json.go Enable goheader rule and add missing license headers (#15473) 2024-09-09 12:05:45 +00:00
gen_program_optionals.go Enable goheader rule and add missing license headers (#15473) 2024-09-09 12:05:45 +00:00
gen_program_read_dir.go Enable goheader rule and add missing license headers (#15473) 2024-09-09 12:05:45 +00:00
gen_program_splat.go Enable goheader rule and add missing license headers (#15473) 2024-09-09 12:05:45 +00:00
gen_program_ternaries.go Enable goheader rule and add missing license headers (#15473) 2024-09-09 12:05:45 +00:00
gen_program_test.go [sdk/program-gen] Implementing DeferredOutput for Go SDK and program-gen (#17885) 2025-01-04 18:47:30 +00:00
gen_program_utils.go Enable goheader rule and add missing license headers (#15473) 2024-09-09 12:05:45 +00:00
gen_spill.go Enable goheader rule and add missing license headers (#15473) 2024-09-09 12:05:45 +00:00
gen_test.go Enable goheader rule and add missing license headers (#15473) 2024-09-09 12:05:45 +00:00
importer.go [go/program-gen] Implement importPathPattern in Go language options to override emitted paths in generated Go programs (#16267) 2024-05-30 19:51:12 +00:00
utilities.go Enable perfsprint linter (#14813) 2023-12-12 12:19:42 +00:00
utilities_test.go Enable goheader rule and add missing license headers (#15473) 2024-09-09 12:05:45 +00:00