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 24, 2022
1 parent 7576c0a commit b55c8c1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG_PENDING.md
Expand Up @@ -45,3 +45,6 @@

- [cli] - Stack names correctly take `org set-default` into account when printing.
[#9240](https://github.com/pulumi/pulumi/pull/9240)

- [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 b55c8c1

Please sign in to comment.