-
Notifications
You must be signed in to change notification settings - Fork 50
Avoid roundtrip to json when converting Cty to map[string]any #3023
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,60 @@ | ||||
| package sdkv2 | ||||
|
|
||||
| import ( | ||||
| "github.com/hashicorp/go-cty/cty" | ||||
| "github.com/pulumi/pulumi/sdk/v3/go/common/util/contract" | ||||
| ) | ||||
|
|
||||
| func objectFromCtyValue(val cty.Value) map[string]any { | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a pre-existing problem but if we're editing it let's annotate how this functionality is used in the project. Doing a quick search, it is used to implement
So the ultimate purpose of this code is to convert cty.Value received from TF to the PropertyMap Pulumi domain. Couple of questions:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The other bit that makes this code somewhat more difficult to maintain than necessary is that we are using a
|
||||
| res := objectFromCtyValueInner(val) | ||||
| if res == nil { | ||||
| return nil | ||||
| } | ||||
| return res.(map[string]any) | ||||
| } | ||||
|
|
||||
| func objectFromCtyValueInner(val cty.Value) any { | ||||
| contract.Assertf(!val.IsMarked(), "value has marks, so it cannot be serialized") | ||||
| if val.IsNull() { | ||||
| return nil | ||||
| } | ||||
|
|
||||
| if !val.IsKnown() { | ||||
| return terraformUnknownVariableValue | ||||
| } | ||||
|
|
||||
| switch { | ||||
| case val.Type().IsPrimitiveType(): | ||||
| switch val.Type() { | ||||
| case cty.String: | ||||
| return val.AsString() | ||||
| case cty.Number: | ||||
| return val.AsBigFloat().Text('f', -1) | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an observable change right, or it isn't? I like this I've found the |
||||
| case cty.Bool: | ||||
| return val.True() | ||||
| default: | ||||
| contract.Failf("unsupported primitive type: %s", val.Type().FriendlyName()) | ||||
| } | ||||
| case val.Type().IsListType(), val.Type().IsSetType(), val.Type().IsTupleType(): | ||||
| l := make([]interface{}, 0, val.LengthInt()) | ||||
| it := val.ElementIterator() | ||||
| for it.Next() { | ||||
| _, ev := it.Element() | ||||
| elem := objectFromCtyValueInner(ev) | ||||
| l = append(l, elem) | ||||
| } | ||||
| return l | ||||
| case val.Type().IsObjectType(), val.Type().IsMapType(): | ||||
| l := make(map[string]interface{}) | ||||
| it := val.ElementIterator() | ||||
| for it.Next() { | ||||
| ek, ev := it.Element() | ||||
| cv := objectFromCtyValueInner(ev) | ||||
| l[ek.AsString()] = cv | ||||
| } | ||||
| return l | ||||
| } | ||||
|
|
||||
| contract.Failf("unsupported type: %s", val.Type().FriendlyName()) | ||||
| return nil | ||||
| } | ||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this comment removed?