Skip to content

Commit

Permalink
Add JSONMarshal to go sdk
Browse files Browse the repository at this point in the history
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 (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.
  • Loading branch information
Frassle committed Dec 9, 2022
1 parent 68308f5 commit 76beddd
Show file tree
Hide file tree
Showing 5 changed files with 381 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
25 changes: 25 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,18 @@ 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(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 +1078,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 +1146,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 +1213,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 76beddd

Please sign in to comment.