Skip to content

Commit 96a33e0

Browse files
committed
rename empty value method, check is value is Unknown and expand testing
1 parent 0ce3e98 commit 96a33e0

File tree

2 files changed

+162
-44
lines changed

2 files changed

+162
-44
lines changed

helper/convert/value.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ package convert
66
import (
77
"github.com/hashicorp/go-cty/cty"
88
"github.com/hashicorp/terraform-plugin-go/tftypes"
9+
"log"
910
)
1011

1112
func PrimitiveTfValue(in cty.Value) tftypes.Value {
12-
if in.IsNull() {
13-
return emptyTfValue(ToTfType(in.Type()))
13+
if in.IsNull() || !in.IsKnown() {
14+
return nullTfValue(ToTfType(in.Type()))
1415
}
1516

1617
var val tftypes.Value
@@ -29,8 +30,8 @@ func PrimitiveTfValue(in cty.Value) tftypes.Value {
2930
func ListTfValue(in cty.Value) tftypes.Value {
3031
listType := ToTfType(in.Type())
3132

32-
if in.IsNull() || in.LengthInt() == 0 {
33-
return emptyTfValue(listType)
33+
if in.IsNull() || !in.IsKnown() {
34+
return nullTfValue(listType)
3435
}
3536

3637
vals := make([]tftypes.Value, 0)
@@ -45,8 +46,8 @@ func ListTfValue(in cty.Value) tftypes.Value {
4546
func MapTfValue(in cty.Value) tftypes.Value {
4647
mapType := ToTfType(in.Type())
4748

48-
if in.IsNull() || in.LengthInt() == 0 {
49-
return emptyTfValue(mapType)
49+
if in.IsNull() || !in.IsKnown() {
50+
return nullTfValue(mapType)
5051
}
5152

5253
vals := make(map[string]tftypes.Value)
@@ -61,8 +62,8 @@ func MapTfValue(in cty.Value) tftypes.Value {
6162
func SetTfValue(in cty.Value) tftypes.Value {
6263
setType := ToTfType(in.Type())
6364

64-
if in.IsNull() || in.LengthInt() == 0 {
65-
return emptyTfValue(setType)
65+
if in.IsNull() || !in.IsKnown() {
66+
return nullTfValue(setType)
6667
}
6768

6869
vals := make([]tftypes.Value, 0)
@@ -77,8 +78,8 @@ func SetTfValue(in cty.Value) tftypes.Value {
7778
func ObjectTfValue(in cty.Value) tftypes.Value {
7879
objType := ToTfType(in.Type())
7980

80-
if in.IsNull() || in.LengthInt() == 0 {
81-
return emptyTfValue(objType)
81+
if in.IsNull() || !in.IsKnown() {
82+
return nullTfValue(objType)
8283
}
8384

8485
vals := make(map[string]tftypes.Value)
@@ -93,8 +94,8 @@ func ObjectTfValue(in cty.Value) tftypes.Value {
9394
func TupleTfValue(in cty.Value) tftypes.Value {
9495
tupleType := ToTfType(in.Type())
9596

96-
if in.IsNull() || in.LengthInt() == 0 {
97-
return emptyTfValue(tupleType)
97+
if in.IsNull() || !in.IsKnown() {
98+
return nullTfValue(tupleType)
9899
}
99100

100101
vals := make([]tftypes.Value, 0)
@@ -121,11 +122,13 @@ func ToTfValue(in cty.Value) tftypes.Value {
121122
return SetTfValue(in)
122123
case ty.IsTupleType():
123124
return TupleTfValue(in)
125+
default:
126+
log.Panicf("unknown type: %s", ty)
124127
}
125128

126-
return emptyTfValue(ToTfType(in.Type()))
129+
return nullTfValue(ToTfType(in.Type()))
127130
}
128131

129-
func emptyTfValue(ty tftypes.Type) tftypes.Value {
132+
func nullTfValue(ty tftypes.Type) tftypes.Value {
130133
return tftypes.NewValue(ty, nil)
131134
}

helper/convert/value_test.go

Lines changed: 145 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestPrimitiveTfType(t *testing.T) {
4040
t.Run(test.Value.GoString(), func(t *testing.T) {
4141
t.Parallel()
4242

43-
got := PrimitiveTfValue(test.Value)
43+
got := ToTfValue(test.Value)
4444

4545
if diff := cmp.Diff(test.Want, got); diff != "" {
4646
t.Errorf("unexpected differences: %s", diff)
@@ -147,35 +147,13 @@ func TestListTfType(t *testing.T) {
147147
}),
148148
}),
149149
},
150-
{
151-
Value: cty.ListVal([]cty.Value{
152-
cty.ObjectVal(map[string]cty.Value{
153-
"enforcement": cty.NullVal(cty.String),
154-
}),
155-
}),
156-
Want: tftypes.NewValue(tftypes.List{
157-
ElementType: tftypes.Object{
158-
AttributeTypes: map[string]tftypes.Type{
159-
"enforcement": tftypes.String,
160-
},
161-
},
162-
}, []tftypes.Value{
163-
tftypes.NewValue(tftypes.Object{
164-
AttributeTypes: map[string]tftypes.Type{
165-
"enforcement": tftypes.String,
166-
},
167-
}, map[string]tftypes.Value{
168-
"enforcement": tftypes.NewValue(tftypes.String, nil),
169-
}),
170-
}),
171-
},
172150
}
173151

174152
for _, test := range tests {
175153
t.Run(test.Value.GoString(), func(t *testing.T) {
176154
t.Parallel()
177155

178-
got := ListTfValue(test.Value)
156+
got := ToTfValue(test.Value)
179157

180158
if diff := cmp.Diff(test.Want, got); diff != "" {
181159
t.Errorf("unexpected differences: %s", diff)
@@ -223,13 +201,150 @@ func TestSetTfType(t *testing.T) {
223201
tftypes.NewValue(tftypes.Number, 200),
224202
}),
225203
},
204+
{
205+
Value: cty.SetVal([]cty.Value{
206+
cty.ObjectVal(map[string]cty.Value{
207+
"name": cty.StringVal("Alice"),
208+
"breed": cty.StringVal("Beagle"),
209+
"weight": cty.NumberIntVal(20),
210+
"toys": cty.SetVal([]cty.Value{cty.StringVal("ball"), cty.StringVal("rope")}),
211+
}),
212+
cty.ObjectVal(map[string]cty.Value{
213+
"name": cty.StringVal("Bobby"),
214+
"breed": cty.StringVal("Golden"),
215+
"weight": cty.NumberIntVal(30),
216+
"toys": cty.SetVal([]cty.Value{cty.StringVal("dummy"), cty.StringVal("frisbee")}),
217+
}),
218+
}),
219+
Want: tftypes.NewValue(tftypes.Set{
220+
ElementType: tftypes.Object{
221+
AttributeTypes: map[string]tftypes.Type{
222+
"name": tftypes.String,
223+
"breed": tftypes.String,
224+
"weight": tftypes.Number,
225+
"toys": tftypes.Set{ElementType: tftypes.String},
226+
},
227+
},
228+
}, []tftypes.Value{
229+
tftypes.NewValue(tftypes.Object{
230+
AttributeTypes: map[string]tftypes.Type{
231+
"name": tftypes.String,
232+
"breed": tftypes.String,
233+
"weight": tftypes.Number,
234+
"toys": tftypes.Set{ElementType: tftypes.String},
235+
},
236+
}, map[string]tftypes.Value{
237+
"name": tftypes.NewValue(tftypes.String, "Alice"),
238+
"breed": tftypes.NewValue(tftypes.String, "Beagle"),
239+
"weight": tftypes.NewValue(tftypes.Number, 20),
240+
"toys": tftypes.NewValue(tftypes.Set{ElementType: tftypes.String}, []tftypes.Value{
241+
tftypes.NewValue(tftypes.String, "ball"),
242+
tftypes.NewValue(tftypes.String, "rope"),
243+
}),
244+
}),
245+
tftypes.NewValue(tftypes.Object{
246+
AttributeTypes: map[string]tftypes.Type{
247+
"name": tftypes.String,
248+
"breed": tftypes.String,
249+
"weight": tftypes.Number,
250+
"toys": tftypes.Set{ElementType: tftypes.String},
251+
},
252+
}, map[string]tftypes.Value{
253+
"name": tftypes.NewValue(tftypes.String, "Bobby"),
254+
"breed": tftypes.NewValue(tftypes.String, "Golden"),
255+
"weight": tftypes.NewValue(tftypes.Number, 30),
256+
"toys": tftypes.NewValue(tftypes.Set{ElementType: tftypes.String}, []tftypes.Value{
257+
tftypes.NewValue(tftypes.String, "dummy"),
258+
tftypes.NewValue(tftypes.String, "frisbee"),
259+
}),
260+
}),
261+
}),
262+
},
226263
}
227264

228265
for _, test := range tests {
229266
t.Run(test.Value.GoString(), func(t *testing.T) {
230267
t.Parallel()
231268

232-
got := SetTfValue(test.Value)
269+
got := ToTfValue(test.Value)
270+
271+
if diff := cmp.Diff(test.Want, got); diff != "" {
272+
t.Errorf("unexpected differences: %s", diff)
273+
}
274+
})
275+
}
276+
}
277+
278+
func TestMapTfType(t *testing.T) {
279+
t.Parallel()
280+
281+
tests := []struct {
282+
Value cty.Value
283+
Want tftypes.Value
284+
}{
285+
{
286+
Value: cty.MapVal(map[string]cty.Value{
287+
"foo": cty.StringVal("bar"),
288+
"baz": cty.StringVal("qux"),
289+
}),
290+
Want: tftypes.NewValue(tftypes.Map{
291+
ElementType: tftypes.String,
292+
}, map[string]tftypes.Value{
293+
"foo": tftypes.NewValue(tftypes.String, "bar"),
294+
"baz": tftypes.NewValue(tftypes.String, "qux"),
295+
}),
296+
},
297+
{
298+
Value: cty.MapVal(map[string]cty.Value{
299+
"foo": cty.MapVal(map[string]cty.Value{
300+
"foo": cty.StringVal("bar"),
301+
"baz": cty.StringVal("qux"),
302+
}),
303+
}),
304+
Want: tftypes.NewValue(tftypes.Map{
305+
ElementType: tftypes.Map{
306+
ElementType: tftypes.String,
307+
}}, map[string]tftypes.Value{
308+
"foo": tftypes.NewValue(tftypes.Map{
309+
ElementType: tftypes.String,
310+
}, map[string]tftypes.Value{
311+
"foo": tftypes.NewValue(tftypes.String, "bar"),
312+
"baz": tftypes.NewValue(tftypes.String, "qux"),
313+
}),
314+
}),
315+
},
316+
{
317+
Value: cty.MapVal(map[string]cty.Value{
318+
"foo": cty.ObjectVal(map[string]cty.Value{
319+
"fruits": cty.MapVal(map[string]cty.Value{
320+
"ananas": cty.StringVal("pineapple"),
321+
"erdbeere": cty.StringVal("strawberry"),
322+
}),
323+
}),
324+
}),
325+
Want: tftypes.NewValue(tftypes.Map{
326+
ElementType: tftypes.Object{
327+
AttributeTypes: map[string]tftypes.Type{
328+
"fruits": tftypes.Map{ElementType: tftypes.String},
329+
}}}, map[string]tftypes.Value{
330+
"foo": tftypes.NewValue(tftypes.Object{
331+
AttributeTypes: map[string]tftypes.Type{
332+
"fruits": tftypes.Map{ElementType: tftypes.String},
333+
}}, map[string]tftypes.Value{
334+
"fruits": tftypes.NewValue(tftypes.Map{ElementType: tftypes.String}, map[string]tftypes.Value{
335+
"ananas": tftypes.NewValue(tftypes.String, "pineapple"),
336+
"erdbeere": tftypes.NewValue(tftypes.String, "strawberry"),
337+
}),
338+
}),
339+
}),
340+
},
341+
}
342+
343+
for _, test := range tests {
344+
t.Run(test.Value.GoString(), func(t *testing.T) {
345+
t.Parallel()
346+
347+
got := ToTfValue(test.Value)
233348

234349
if diff := cmp.Diff(test.Want, got); diff != "" {
235350
t.Errorf("unexpected differences: %s", diff)
@@ -255,12 +370,12 @@ func TestTupleTfType(t *testing.T) {
255370
Value: cty.TupleVal([]cty.Value{
256371
cty.StringVal("apple"),
257372
cty.NumberIntVal(5),
258-
cty.StringVal("kangaroo"),
373+
cty.TupleVal([]cty.Value{cty.StringVal("banana"), cty.StringVal("pineapple")}),
259374
}),
260-
Want: tftypes.NewValue(tftypes.Tuple{ElementTypes: []tftypes.Type{tftypes.String, tftypes.Number, tftypes.String}}, []tftypes.Value{
375+
Want: tftypes.NewValue(tftypes.Tuple{ElementTypes: []tftypes.Type{tftypes.String, tftypes.Number, tftypes.Tuple{ElementTypes: []tftypes.Type{tftypes.String, tftypes.String}}}}, []tftypes.Value{
261376
tftypes.NewValue(tftypes.String, "apple"),
262377
tftypes.NewValue(tftypes.Number, 5),
263-
tftypes.NewValue(tftypes.String, "kangaroo"),
378+
tftypes.NewValue(tftypes.Tuple{ElementTypes: []tftypes.Type{tftypes.String, tftypes.String}}, []tftypes.Value{tftypes.NewValue(tftypes.String, "banana"), tftypes.NewValue(tftypes.String, "pineapple")}),
264379
}),
265380
},
266381
}
@@ -269,7 +384,7 @@ func TestTupleTfType(t *testing.T) {
269384
t.Run(test.Value.GoString(), func(t *testing.T) {
270385
t.Parallel()
271386

272-
got := TupleTfValue(test.Value)
387+
got := ToTfValue(test.Value)
273388

274389
if diff := cmp.Diff(test.Want, got); diff != "" {
275390
t.Errorf("unexpected differences: %s", diff)
@@ -410,7 +525,7 @@ func TestObjectTfType(t *testing.T) {
410525
t.Run(test.Value.GoString(), func(t *testing.T) {
411526
t.Parallel()
412527

413-
got := ObjectTfValue(test.Value)
528+
got := ToTfValue(test.Value)
414529

415530
if diff := cmp.Diff(test.Want, got); diff != "" {
416531
t.Errorf("unexpected differences: %s", diff)

0 commit comments

Comments
 (0)