Skip to content

Commit

Permalink
[codegen/go] Update Go SDK function output to check for errors
Browse files Browse the repository at this point in the history
Fixes pulumi/pulumi-aws#1872.

This should result in the following sample output in the Go SDK:

```
func GetPolicyDocumentOutput(ctx *pulumi.Context, args GetPolicyDocumentOutputArgs, opts ...pulumi.InvokeOption) GetPolicyDocumentResultOutput {
	return pulumi.ToOutputWithContext(context.Background(), args).
		ApplyT(func(v interface{}) (GetPolicyDocumentResult, error) {
			args := v.(GetPolicyDocumentArgs)
			r, err := GetPolicyDocument(ctx, &args, opts...)
			if err != nil {
				return nil, err
			}
			if r == nil {
				return nil, fmt.Errorf("expected either result or error to be nil, not both")
			}
			return *r, err
		}).(GetPolicyDocumentResultOutput)
}
```
  • Loading branch information
guineveresaenger committed Mar 23, 2022
1 parent d7393a3 commit 30e6949
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion CHANGELOG_PENDING.md
Expand Up @@ -33,4 +33,7 @@
[#9243](https://github.com/pulumi/pulumi/pull/9243)

- [cli/engine] - Fix a panic due to `Check` failing while using update plans.
[#9254](https://github.com/pulumi/pulumi/pull/9254)
[#9254](https://github.com/pulumi/pulumi/pull/9254)

- [codegen/go] - Fix Go SDK function output to check for errors
[pulumi-aws#1872](https://github.com/pulumi/pulumi-aws/issues/1872)
7 changes: 7 additions & 0 deletions pkg/codegen/go/gen.go
Expand Up @@ -2073,11 +2073,18 @@ func ${fn}Output(ctx *pulumi.Context, args ${fn}OutputArgs, opts ...pulumi.Invok
ApplyT(func(v interface{}) (${fn}Result, error) {
args := v.(${fn}Args)
r, err := ${fn}(ctx, &args, opts...)
if err != nil {
return nil, err
}
if r == nil {
return nil, fmt.Errorf("expected either result or error to be nil, not both")
}
return *r, err
}).(${outputType})
}
`

code = strings.ReplaceAll(code, "${fn}", originalName)
code = strings.ReplaceAll(code, "${outputType}", resultTypeName)
fmt.Fprintf(w, code)
Expand Down

0 comments on commit 30e6949

Please sign in to comment.