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

[sdk/go] pulumi.JSONMarshal doesn't support unknowns. #12460

Open
dixler opened this issue Mar 20, 2023 · 2 comments
Open

[sdk/go] pulumi.JSONMarshal doesn't support unknowns. #12460

dixler opened this issue Mar 20, 2023 · 2 comments
Labels
area/sdks Pulumi language SDKs impact/quality kind/bug Some behavior is incorrect or out of spec

Comments

@dixler
Copy link
Contributor

dixler commented Mar 20, 2023

What happened?

I'm writing the documentation for inputs-outputs and adding the json support and I'm currently on Go. Go seems like the prickliest of our SDKs and I've run into some issues (potentially because I'm not familiar). It looks like from the PR description there is some outstanding work to make the experience with JSONMarshal better.
#11609

Expected Behavior

Doesn't crash.

Steps to reproduce

package main

import (
	"github.com/pulumi/pulumi-random/sdk/v4/go/random"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		pet, err := random.NewRandomPet(ctx, "parent", &random.RandomPetArgs{},
			pulumi.Protect(true))
		_ = err
		petOut := pulumi.JSONMarshal(pet.ToRandomPetOutput()) // offending line

		ctx.Export("petOut", petOut)
		return nil
	})
}

I'm getting the following error:

  pulumi:pulumi:Stack (prog-aws-go-dev):
    error: an unhandled error occurred: program failed:
    waiting for RPCs: json: error calling MarshalJSON for type pulumi.MapOutput: Outputs can not be marshaled to JSON

Output of pulumi about

% pulumi about
CLI          
Version      3.56.1-dev.0
Go Version   go1.20
Go Compiler  gc

Plugins
NAME    VERSION
go      unknown
random  4.8.2

Host     
OS       arch
Version  22.0.4
Arch     x86_64

This project is written in go: executable='/usr/bin/go' version='go version go1.20 linux/amd64'

Current Stack: pulumi/prog-aws-go/dev

TYPE                              URN
pulumi:pulumi:Stack               urn:pulumi:dev::prog-aws-go::pulumi:pulumi:Stack::prog-aws-go-dev
pulumi:providers:random           urn:pulumi:dev::prog-aws-go::pulumi:providers:random::default
random:index/randomPet:RandomPet  urn:pulumi:dev::prog-aws-go::random:index/randomPet:RandomPet::parent


Found no pending operations associated with pulumi/dev

Additional context

No response

Contributing

Vote on this issue by adding a 👍 reaction.
To contribute a fix for this issue, leave a comment (and link to your pull request, if you've opened one already).

@dixler dixler added kind/bug Some behavior is incorrect or out of spec needs-triage Needs attention from the triage team labels Mar 20, 2023
@abhinav
Copy link
Contributor

abhinav commented Mar 21, 2023

We don't have a solution yet, but copying part of the Slack discussion between me and @Frassle here:

A: [one] option is: don't convert [to map[string]any] or try to replicate json.Marshal behavior. Instead, traverse the struct with reflection once, track all Outputs, build a pulumi.All from them, and inside its Apply, call json.Marshal. All the MarshalJSON methods just take the actual value and serialize it (erroring if the output is not done, in case someone used json.Marshal instead of pulumi.JSONMarshal).

F: So have JSONMarshal handle the secretness/unknownness via reflection walk, and then have the standard Marshal calls just be blocking calls that either return the value marshalled or error if it was unknown (at which point our JSONMarshal wouldn't have called it at all).
F: Bit odd that this gives you an "out" from output space via json.Marshal if you happen to only have known values, I can see that tripping some users up.

A: If we want extra safety around it, OutputState could have a bit on it indicating that it had been touched by pulumi.JSONMarshal, only after which will it serialize to MarshalJSON.
A: Long-term, it wouldn't be the worst idea to have an Output-aware JSON marshaller. It could be a [fork] of encoding/json's serialization code, or something else.

@RobbieMcKinstry RobbieMcKinstry added area/sdks Pulumi language SDKs impact/quality and removed needs-triage Needs attention from the triage team labels Mar 22, 2023
@dixler dixler changed the title [sdk/go] pulumi.JSONMarshal doesn't marshal resource [sdk/go] pulumi.JSON(Un)Marshal doesn't support Map types. Mar 28, 2023
@dixler dixler changed the title [sdk/go] pulumi.JSON(Un)Marshal doesn't support Map types. [sdk/go] pulumi.JSONMarshal doesn't support Map types. Mar 28, 2023
@dixler dixler changed the title [sdk/go] pulumi.JSONMarshal doesn't support Map types. [sdk/go] pulumi.JSONMarshal doesn't support unknowns. Mar 28, 2023
@justinvp
Copy link
Member

justinvp commented Apr 4, 2023

As part of fixing this, also need to remove the note for the Go example in the docs at https://www.pulumi.com/docs/intro/concepts/inputs-outputs/#converting-outputs-to-json

@justinvp justinvp mentioned this issue Jul 14, 2023
19 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/sdks Pulumi language SDKs impact/quality kind/bug Some behavior is incorrect or out of spec
Projects
None yet
Development

No branches or pull requests

4 participants