Skip to content

Commit

Permalink
Merge #11609
Browse files Browse the repository at this point in the history
11609: Add JSONMarshal to go sdk r=Frassle a=Frassle

<!--- 
Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation.
-->

# Description

<!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. -->

This is not as useful for Go as it is for other languages, but it's consistent and it saves an Apply call.

Ideally we would handle nested Output types during marshalling, but to do that correctly we need some way to get access to per-call-to-JSONMarshal flags to note if we see any unknowns, or secrets.

Go just doesn't (AFAIK) have any way to support doing that.

So we do the next best thing which is we at least error that nested Outputs can't be marshalled. Note that the `Output` interface doesn't embed the `Marshal` interface because there _may_ be users who have wrote their own `Output` types and that would be a breaking change.

## Checklist

<!--- Please provide details if the checkbox below is to be left unchecked. -->
- [x] I have added tests that prove my fix is effective or that my feature works
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the Pulumi Service,
then the service should honor older versions of the CLI where this change would not exist.
You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Service API version
  <!-- `@Pulumi` employees: If yes, you must submit corresponding changes in the service repo. -->


Co-authored-by: Fraser Waters <fraser@pulumi.com>
  • Loading branch information
bors[bot] and Frassle committed Dec 10, 2022
2 parents bc8f604 + 9ca4627 commit caf5e17
Show file tree
Hide file tree
Showing 5 changed files with 386 additions and 0 deletions.
@@ -0,0 +1,4 @@
changes:
- type: feat
scope: sdk/go
description: Add JSONMarshal to go sdk.
6 changes: 6 additions & 0 deletions sdk/go/pulumi/generate/templates/types_builtins.go.template
Expand Up @@ -19,6 +19,7 @@ package pulumi

import (
"context"
"fmt"
"reflect"
)

Expand Down Expand Up @@ -55,6 +56,7 @@ func {{.Name}}FromPtr(v *{{.ElemElementType}}) {{.Name}}Input {
}
{{end}}
{{if .DefineInputMethods}}

// ElementType returns the element type of this Input ({{.ElementType}}).
func ({{.InputType}}) ElementType() reflect.Type {
return {{.Name | Unexported}}Type
Expand Down Expand Up @@ -92,6 +94,10 @@ func (in {{.InputType}}) To{{.Name}}PtrOutputWithContext(ctx context.Context) {{
// {{.Name}}Output is an Output that returns {{.ElementType}} values.
type {{.Name}}Output struct { *OutputState }

func ({{.Name}}Output) MarshalJSON() ([]byte, error) {
return nil, fmt.Errorf("Outputs can not be marshaled to JSON")
}

// ElementType returns the element type of this Output ({{.ElementType}}).
func ({{.Name}}Output) ElementType() reflect.Type {
return {{.Name | Unexported}}Type
Expand Down
30 changes: 30 additions & 0 deletions sdk/go/pulumi/types.go
Expand Up @@ -17,6 +17,7 @@ package pulumi

import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
Expand Down Expand Up @@ -602,6 +603,23 @@ func AllWithContext(ctx context.Context, inputs ...interface{}) ArrayOutput {
return ToOutputWithContext(ctx, inputs).(ArrayOutput)
}

// JSONMarshal uses "encoding/json".Marshal to serialize the given Output value into a JSON string.
func JSONMarshal(v interface{}) StringOutput {
return JSONMarshalWithContext(context.Background(), v)
}

// JSONMarshalWithContext uses "encoding/json".Marshal to serialize the given Output value into a JSON string.
func JSONMarshalWithContext(ctx context.Context, v interface{}) StringOutput {
o := ToOutputWithContext(ctx, v)
return o.ApplyTWithContext(ctx, func(_ context.Context, v interface{}) (string, error) {
json, err := json.Marshal(v)
if err != nil {
return "", err
}
return string(json), nil
}).(StringOutput)
}

func gatherJoins(v interface{}) workGroups {
if v == nil {
return nil
Expand Down Expand Up @@ -1065,6 +1083,10 @@ func anyWithContext(ctx context.Context, join *workGroup, v interface{}) AnyOutp

type AnyOutput struct{ *OutputState }

func (AnyOutput) MarshalJSON() ([]byte, error) {
return nil, fmt.Errorf("Outputs can not be marshaled to JSON")
}

func (AnyOutput) ElementType() reflect.Type {
return anyType
}
Expand Down Expand Up @@ -1129,6 +1151,10 @@ func convert(v interface{}, to reflect.Type) interface{} {
// TODO: ResourceOutput and the init() should probably be code generated.
type ResourceOutput struct{ *OutputState }

func (ResourceOutput) MarshalJSON() ([]byte, error) {
return nil, fmt.Errorf("Outputs can not be marshaled to JSON")
}

// ElementType returns the element type of this Output (Resource).
func (ResourceOutput) ElementType() reflect.Type {
return reflect.TypeOf((*Resource)(nil)).Elem()
Expand Down Expand Up @@ -1192,6 +1218,10 @@ func (in ResourceArray) ToResourceArrayOutputWithContext(ctx context.Context) Re
// ResourceArrayOutput is an Output that returns []Resource values.
type ResourceArrayOutput struct{ *OutputState }

func (ResourceArrayOutput) MarshalJSON() ([]byte, error) {
return nil, fmt.Errorf("Outputs can not be marshaled to JSON")
}

// ElementType returns the element type of this Output ([]Resource).
func (ResourceArrayOutput) ElementType() reflect.Type {
return resourceArrayType
Expand Down

0 comments on commit caf5e17

Please sign in to comment.