Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSONMarshal to go sdk #11609

Merged
merged 1 commit into from Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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