-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathvalue_as_test.go
423 lines (392 loc) · 11.1 KB
/
value_as_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfsdk
import (
"context"
"fmt"
"math/big"
goreflect "reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/reflect"
"github.com/hashicorp/terraform-plugin-framework/internal/testing/testtypes"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
)
func newBoolPointer(in bool) *bool {
return &in
}
func newBoolPointerPointer(in bool) **bool {
boolPointer := &in
return &boolPointer
}
func newFloat32Pointer(in float32) *float32 {
return &in
}
func newFloat32PointerPointer(in float32) **float32 {
floatPointer := &in
return &floatPointer
}
func newFloat64Pointer(in float64) *float64 {
return &in
}
func newFloat64PointerPointer(in float64) **float64 {
floatPointer := &in
return &floatPointer
}
func newInt32Pointer(in int32) *int32 {
return &in
}
func newInt32PointerPointer(in int32) **int32 {
intPointer := &in
return &intPointer
}
func newInt64Pointer(in int64) *int64 {
return &in
}
func newInt64PointerPointer(in int64) **int64 {
intPointer := &in
return &intPointer
}
func newBigFloatPointerPointer(in uint64) **big.Float {
bf := new(big.Float).SetUint64(in)
return &bf
}
func newStringPointer(in string) *string {
return &in
}
func newStringPointerPointer(in string) **string {
stringPointer := &in
return &stringPointer
}
type personFrameworkTypes struct {
Name types.String `tfsdk:"name"`
Age types.Int64 `tfsdk:"age"`
OptedIn types.Bool `tfsdk:"opted_in"`
Address types.Map `tfsdk:"address"`
Colours types.List `tfsdk:"colours"`
}
type personGoTypes struct {
Name string `tfsdk:"name"`
Age int64 `tfsdk:"age"`
OptedIn bool `tfsdk:"opted_in"`
Address map[string]string `tfsdk:"address"`
Colours []string `tfsdk:"colours"`
}
func TestValueAs(t *testing.T) {
t.Parallel()
type testCase struct {
val attr.Value
target interface{}
expected interface{}
expectedDiags diag.Diagnostics
}
tests := map[string]testCase{
"primitive bool pointer": {
val: types.BoolValue(true),
target: newBoolPointer(false),
expected: newBoolPointer(true),
},
"primitive bool pointer pointer": {
val: types.BoolValue(true),
target: newBoolPointerPointer(false),
expected: newBoolPointerPointer(true),
},
"primitive float32 pointer": {
val: types.Float32Value(12.3),
target: newFloat32Pointer(0.0),
expected: newFloat32Pointer(12.3),
},
"primitive float32 pointer pointer": {
val: types.Float32Value(12.3),
target: newFloat32PointerPointer(0.0),
expected: newFloat32PointerPointer(12.3),
},
"primitive float64 pointer": {
val: types.Float64Value(12.3),
target: newFloat64Pointer(0.0),
expected: newFloat64Pointer(12.3),
},
"primitive float64 pointer pointer": {
val: types.Float64Value(12.3),
target: newFloat64PointerPointer(0.0),
expected: newFloat64PointerPointer(12.3),
},
"primitive int32 pointer": {
val: types.Int32Value(12),
target: newInt32Pointer(0),
expected: newInt32Pointer(12),
},
"primitive int32 pointer pointer": {
val: types.Int32Value(12),
target: newInt32PointerPointer(0),
expected: newInt32PointerPointer(12),
},
"primitive int64 pointer": {
val: types.Int64Value(12),
target: newInt64Pointer(0),
expected: newInt64Pointer(12),
},
"primitive int64 pointer pointer": {
val: types.Int64Value(12),
target: newInt64PointerPointer(0),
expected: newInt64PointerPointer(12),
},
// Following test fails as target.Type() is big.Float not *bigFloat.
// See https://github.com/hashicorp/terraform-plugin-framework/blob/main/internal/reflect/into.go#L144
// The switch on target.Kind() then identifies big.Float as reflect.Struct and the reflection fails
// with "cannot reflect tftypes.Number into a struct, must be an object".
// See https://github.com/hashicorp/terraform-plugin-framework/blob/main/internal/reflect/into.go#L148
// "primitive number pointer": {
// val: types.Number{Value: new(big.Float).SetUint64(722770156065510359)},
// target: newBigFloatPointer(0),
// expected: newBigFloatPointer(722770156065510359),
// },
"primitive number pointer pointer": {
val: types.NumberValue(new(big.Float).SetUint64(722770156065510359)),
target: newBigFloatPointerPointer(0),
expected: newBigFloatPointerPointer(722770156065510359),
},
"primitive string pointer": {
val: types.StringValue("hello"),
target: newStringPointer(""),
expected: newStringPointer("hello"),
},
"primitive string pointer pointer": {
val: types.StringValue("hello"),
target: newStringPointerPointer(""),
expected: newStringPointerPointer("hello"),
},
"list": {
val: types.ListValueMust(
types.StringType,
[]attr.Value{
types.StringValue("hello"),
types.StringValue("world"),
},
),
target: &[]string{},
expected: &[]string{
"hello",
"world",
},
},
"map": {
val: types.MapValueMust(
types.StringType,
map[string]attr.Value{
"hello": types.StringValue("world"),
"goodbye": types.StringValue("world"),
},
),
target: &map[string]string{},
expected: &map[string]string{
"hello": "world",
"goodbye": "world",
},
},
"object": {
val: types.ObjectValueMust(
map[string]attr.Type{
"name": types.StringType,
"age": types.Int64Type,
"opted_in": types.BoolType,
},
map[string]attr.Value{
"name": types.StringValue("Boris"),
"age": types.Int64Value(25),
"opted_in": types.BoolValue(true),
},
),
target: &struct {
Name string `tfsdk:"name"`
Age int64 `tfsdk:"age"`
OptedIn bool `tfsdk:"opted_in"`
}{},
expected: &struct {
Name string `tfsdk:"name"`
Age int64 `tfsdk:"age"`
OptedIn bool `tfsdk:"opted_in"`
}{
Name: "Boris",
Age: 25,
OptedIn: true,
},
},
"set": {
val: types.SetValueMust(
types.BoolType,
[]attr.Value{
types.BoolValue(true),
types.BoolValue(false),
},
),
target: &[]bool{},
expected: &[]bool{
true,
false,
},
},
"struct framework types": {
val: types.ObjectValueMust(
map[string]attr.Type{
"name": types.StringType,
"age": types.Int64Type,
"opted_in": types.BoolType,
"address": types.MapType{ElemType: types.StringType},
"colours": types.ListType{ElemType: types.StringType},
},
map[string]attr.Value{
"name": types.StringValue("Boris"),
"age": types.Int64Value(25),
"opted_in": types.BoolValue(true),
"address": types.MapValueMust(
types.StringType,
map[string]attr.Value{
"first_line": types.StringValue("10 Downing Street"),
"postcode": types.StringValue("SW1A 2AA"),
},
),
"colours": types.ListValueMust(
types.StringType,
[]attr.Value{
types.StringValue("red"),
types.StringValue("green"),
types.StringValue("blue"),
},
),
},
),
target: &personFrameworkTypes{},
expected: &personFrameworkTypes{
Name: types.StringValue("Boris"),
Age: types.Int64Value(25),
OptedIn: types.BoolValue(true),
Address: types.MapValueMust(
types.StringType,
map[string]attr.Value{
"first_line": types.StringValue("10 Downing Street"),
"postcode": types.StringValue("SW1A 2AA"),
},
),
Colours: types.ListValueMust(
types.StringType,
[]attr.Value{
types.StringValue("red"),
types.StringValue("green"),
types.StringValue("blue"),
},
),
},
},
"struct go types": {
val: types.ObjectValueMust(
map[string]attr.Type{
"name": types.StringType,
"age": types.Int64Type,
"opted_in": types.BoolType,
"address": types.MapType{ElemType: types.StringType},
"colours": types.ListType{ElemType: types.StringType},
},
map[string]attr.Value{
"name": types.StringValue("Boris"),
"age": types.Int64Value(25),
"opted_in": types.BoolValue(true),
"address": types.MapValueMust(
types.StringType,
map[string]attr.Value{
"first_line": types.StringValue("10 Downing Street"),
"postcode": types.StringValue("SW1A 2AA"),
},
),
"colours": types.ListValueMust(
types.StringType,
[]attr.Value{
types.StringValue("red"),
types.StringValue("green"),
types.StringValue("blue"),
},
),
},
),
target: &personGoTypes{},
expected: &personGoTypes{
Name: "Boris",
Age: 25,
OptedIn: true,
Address: map[string]string{
"first_line": "10 Downing Street",
"postcode": "SW1A 2AA",
},
Colours: []string{"red", "green", "blue"},
},
},
"incompatible-type": {
val: types.StringValue("hello"),
target: newInt64Pointer(0),
expectedDiags: diag.Diagnostics{
diag.WithPath(
path.Empty(),
reflect.DiagIntoIncompatibleType{
Val: tftypes.NewValue(tftypes.String, "hello"),
TargetType: goreflect.TypeOf(int64(0)),
Err: fmt.Errorf("can't unmarshal %s into %T, expected *big.Float", tftypes.String, big.NewFloat(0)),
},
),
},
},
"different-type": {
val: types.StringValue("hello"),
target: &testtypes.String{},
expectedDiags: diag.Diagnostics{
diag.WithPath(
path.Empty(),
reflect.DiagNewAttributeValueIntoWrongType{
ValType: goreflect.TypeOf(types.StringValue("hello")),
TargetType: goreflect.TypeOf(testtypes.String{}),
SchemaType: types.StringType,
},
),
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
diags := ValueAs(context.Background(), tc.val, tc.target)
if diff := cmp.Diff(tc.expectedDiags, diags); diff != "" {
t.Fatalf("Unexpected diff in diagnostics (-wanted, +got): %s", diff)
}
if diags.HasError() {
return
}
// cmp.Comparer is used to generate a type-specific comparison for big.Float as cmp.Diff
// cannot be used owing to the unexported (*big.Float).prec field.
opt := cmp.Comparer(func(expected **big.Float, target **big.Float) bool {
//nolint:forcetypeassert // Type assertion is guaranteed by the above `cmp.Comparer` function
if diff := (*tc.expected.(**big.Float)).Cmp(*tc.target.(**big.Float)); diff != 0 {
return false
}
return true
})
if diff := cmp.Diff(tc.expected, tc.target, opt); diff != "" {
t.Fatalf("Unexpected diff in results (-wanted, +got): %s", diff)
}
})
}
}
func TestValueAs_generic(t *testing.T) {
t.Parallel()
var target attr.Value
val := types.StringValue("hello")
diags := ValueAs(context.Background(), val, &target)
if len(diags) > 0 {
t.Fatalf("Unexpected diagnostics: %s", diags)
}
if !val.Equal(target) {
t.Errorf("Expected target to be %v, got %v", val, target)
}
}