Skip to content

Commit

Permalink
Merge pull request #113 from magodo/dynamic_to_json_handle_unknown
Browse files Browse the repository at this point in the history
`dynamic.ToJSON` handles unknown underlying value
  • Loading branch information
magodo authored Aug 8, 2024
2 parents 53466ab + 1d6b01e commit 5d5ea42
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 2 additions & 2 deletions internal/dynamic/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func ToJSON(d types.Dynamic) ([]byte, error) {
if d.IsNull() {
if d.IsNull() || d.IsUnknown() {
return nil, nil
}
return attrValueToJSON(d.UnderlyingValue())
Expand Down Expand Up @@ -42,7 +42,7 @@ func attrMapToJSON(in map[string]attr.Value) (map[string]json.RawMessage, error)
}

func attrValueToJSON(val attr.Value) ([]byte, error) {
if val.IsNull() {
if val.IsNull() || val.IsUnknown() {
return json.Marshal(nil)
}
switch value := val.(type) {
Expand Down
9 changes: 8 additions & 1 deletion internal/dynamic/dynamic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -81,6 +82,8 @@ func TestToJSON(t *testing.T) {
"string": types.StringType,
},
},
"dynamic_unknwon": basetypes.DynamicType{},
"dynamic_null": basetypes.DynamicType{},
},
map[string]attr.Value{
"bool": types.BoolValue(true),
Expand Down Expand Up @@ -168,6 +171,8 @@ func TestToJSON(t *testing.T) {
"string": types.StringType,
},
),
"dynamic_unknwon": types.DynamicUnknown(),
"dynamic_null": types.DynamicNull(),
},
),
)
Expand Down Expand Up @@ -203,7 +208,9 @@ func TestToJSON(t *testing.T) {
"string": "a"
},
"object_empty": {},
"object_null": null
"object_null": null,
"dynamic_unknwon": null,
"dynamic_null": null
}`

b, err := ToJSON(input)
Expand Down

0 comments on commit 5d5ea42

Please sign in to comment.