Skip to content

Commit 478e3cb

Browse files
fix marshalling of dynamic type values
1 parent 9908589 commit 478e3cb

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

pkg/valueshim/tftype_json.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package valueshim
1616

1717
import (
1818
"encoding/json"
19+
"fmt"
1920
"math/big"
2021

2122
"github.com/hashicorp/terraform-plugin-go/tftypes"
@@ -31,21 +32,22 @@ func tftypeValueToJSON(typ tftypes.Type, v tftypes.Value) ([]byte, error) {
3132
}
3233

3334
func jsonMarshal(v tftypes.Value, typ tftypes.Type, p *tftypes.AttributePath) (interface{}, error) {
34-
if v.IsNull() {
35-
return nil, nil
36-
}
3735
if !v.IsKnown() {
3836
return nil, p.NewErrorf("unknown values cannot be serialized to JSON")
3937
}
38+
if typ.Is(tftypes.DynamicPseudoType) {
39+
return jsonMarshalDynamicPseudoType(v, typ, p)
40+
}
41+
if v.IsNull() {
42+
return nil, nil
43+
}
4044
switch {
4145
case typ.Is(tftypes.String):
4246
return jsonMarshalString(v, typ, p)
4347
case typ.Is(tftypes.Number):
4448
return jsonMarshalNumber(v, typ, p)
4549
case typ.Is(tftypes.Bool):
4650
return jsonMarshalBool(v, typ, p)
47-
case typ.Is(tftypes.DynamicPseudoType):
48-
return jsonMarshalDynamicPseudoType(v, typ, p)
4951
case typ.Is(tftypes.List{}):
5052
return jsonMarshalList(v, typ.(tftypes.List).ElementType, p)
5153
case typ.Is(tftypes.Set{}):
@@ -99,10 +101,11 @@ func jsonMarshalDynamicPseudoType(v tftypes.Value, _ tftypes.Type, p *tftypes.At
99101
if err != nil {
100102
return nil, p.NewError(err)
101103
}
102-
return map[string]interface{}{
103-
"type": string(typeJSON),
104-
"value": valJSON,
105-
}, nil
104+
marshalledValJSON, err := json.Marshal(valJSON)
105+
if err != nil {
106+
return nil, p.NewError(err)
107+
}
108+
return json.RawMessage(fmt.Sprintf(`{"value": %s, "type": %s}`, marshalledValJSON, typeJSON)), nil
106109
}
107110

108111
func jsonMarshalList(v tftypes.Value, elementType tftypes.Type, p *tftypes.AttributePath) (interface{}, error) {

pkg/valueshim/tftype_json_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,22 +367,22 @@ func TestValueToJSON_DynamicPseudoType(t *testing.T) {
367367
require.NoError(t, err)
368368

369369
// The result should be a JSON object with type and value fields
370-
expected := `{"type":"\"string\"","value":"dynamic content"}`
370+
expected := `{"type":"string","value":"dynamic content"}`
371371
assert.JSONEq(t, expected, string(result))
372372

373373
// Also test with other types
374374
numberValue := tftypes.NewValue(tftypes.Number, 42)
375375
result, err = tftypeValueToJSON(tftypes.DynamicPseudoType, numberValue)
376376
require.NoError(t, err)
377377

378-
expected = `{"type":"\"number\"","value":42}`
378+
expected = `{"type":"number","value":42}`
379379
assert.JSONEq(t, expected, string(result))
380380

381381
boolValue := tftypes.NewValue(tftypes.Bool, true)
382382
result, err = tftypeValueToJSON(tftypes.DynamicPseudoType, boolValue)
383383
require.NoError(t, err)
384384

385-
expected = `{"type":"\"bool\"","value":true}`
385+
expected = `{"type":"bool","value":true}`
386386
assert.JSONEq(t, expected, string(result))
387387
}
388388

0 commit comments

Comments
 (0)