Skip to content

Commit fd00d4d

Browse files
fix serialization of empty lists and sets
1 parent 9de5088 commit fd00d4d

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

pkg/valueshim/tftype_json.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,14 @@ func jsonMarshalList(v tftypes.Value, elementType tftypes.Type, p *tftypes.Attri
111111
if err != nil {
112112
return nil, p.NewError(err)
113113
}
114-
var res []interface{}
114+
res := make([]interface{}, len(vs))
115115
for i, v := range vs {
116116
ep := p.WithElementKeyInt(i)
117117
e, err := jsonMarshal(v, elementType, ep)
118118
if err != nil {
119119
return nil, ep.NewError(err)
120120
}
121-
res = append(res, e)
121+
res[i] = e
122122
}
123123
return res, nil
124124
}
@@ -129,14 +129,14 @@ func jsonMarshalSet(v tftypes.Value, elementType tftypes.Type, p *tftypes.Attrib
129129
if err != nil {
130130
return nil, p.NewError(err)
131131
}
132-
var res []interface{}
133-
for _, v := range vs {
132+
res := make([]interface{}, len(vs))
133+
for i, v := range vs {
134134
ep := p.WithElementKeyValue(v)
135135
e, err := jsonMarshal(v, elementType, ep)
136136
if err != nil {
137137
return nil, ep.NewError(err)
138138
}
139-
res = append(res, e)
139+
res[i] = e
140140
}
141141
return res, nil
142142
}
@@ -147,7 +147,7 @@ func jsonMarshalMap(v tftypes.Value, elementType tftypes.Type, p *tftypes.Attrib
147147
if err != nil {
148148
return nil, p.NewError(err)
149149
}
150-
res := map[string]interface{}{}
150+
res := make(map[string]interface{}, len(vs))
151151
for k, v := range vs {
152152
ep := p.WithElementKeyValue(v)
153153
e, err := jsonMarshal(v, elementType, ep)

pkg/valueshim/tftype_json_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ func TestValueToJSON(t *testing.T) {
124124
name: "list empty",
125125
typ: tftypes.List{ElementType: tftypes.String},
126126
value: tftypes.NewValue(tftypes.List{ElementType: tftypes.String}, []tftypes.Value{}),
127-
// Empty lists serialize to null, not []
128-
expectJSON: autogold.Expect("null"),
127+
expectJSON: autogold.Expect("[]"),
129128
},
130129
{
131130
name: "list of strings",
@@ -169,8 +168,7 @@ func TestValueToJSON(t *testing.T) {
169168
name: "set empty",
170169
typ: tftypes.Set{ElementType: tftypes.String},
171170
value: tftypes.NewValue(tftypes.Set{ElementType: tftypes.String}, []tftypes.Value{}),
172-
// Empty sets serialize to null, not []
173-
expectJSON: autogold.Expect("null"),
171+
expectJSON: autogold.Expect("[]"),
174172
},
175173
{
176174
name: "set of strings",

0 commit comments

Comments
 (0)