1
1
package reflect_test
2
2
3
- // TODO: uncomment when Object is merged
4
- /*
5
- func TestReflectObjectIntoStruct_notAnObject(t *testing.T) {
3
+ import (
4
+ "context"
5
+ "math/big"
6
+ "reflect"
7
+ "testing"
8
+
9
+ "github.com/hashicorp/terraform-plugin-framework/attr"
10
+ refl "github.com/hashicorp/terraform-plugin-framework/internal/reflect"
11
+ "github.com/hashicorp/terraform-plugin-framework/types"
12
+
13
+ "github.com/google/go-cmp/cmp"
14
+ "github.com/hashicorp/terraform-plugin-go/tftypes"
15
+ )
16
+
17
+ func TestStruct_notAnObject (t * testing.T ) {
6
18
t .Parallel ()
7
19
8
20
var s struct {}
9
- _, err := refl.ReflectStructFromObject (context.Background(), types.StringType, tftypes.NewValue(tftypes.String, "hello"), reflect.ValueOf(s), refl.Options{}, tftypes.NewAttributePath())
21
+ _ , err := refl .Struct (context .Background (), types .StringType , tftypes .NewValue (tftypes .String , "hello" ), reflect .ValueOf (s ), refl.Options {}, tftypes .NewAttributePath ())
10
22
if err == nil {
11
23
t .Error ("Expected error, didn't get one" )
12
24
}
@@ -15,11 +27,11 @@ func TestReflectObjectIntoStruct_notAnObject(t *testing.T) {
15
27
}
16
28
}
17
29
18
- func TestReflectObjectIntoStruct_notAStruct (t *testing.T) {
30
+ func TestStruct_notAStruct (t * testing.T ) {
19
31
t .Parallel ()
20
32
21
33
var s string
22
- _, err := refl.ReflectStructFromObject (context.Background(), types.ObjectType{
34
+ _ , err := refl .Struct (context .Background (), types.ObjectType {
23
35
AttrTypes : map [string ]attr.Type {
24
36
"a" : types .StringType ,
25
37
},
@@ -38,13 +50,13 @@ func TestReflectObjectIntoStruct_notAStruct(t *testing.T) {
38
50
}
39
51
}
40
52
41
- func TestReflectObjectIntoStruct_objectMissingFields (t *testing.T) {
53
+ func TestStruct_objectMissingFields (t * testing.T ) {
42
54
t .Parallel ()
43
55
44
56
var s struct {
45
57
A string `tfsdk:"a"`
46
58
}
47
- _, err := refl.ReflectStructFromObject (context.Background(), types.ObjectType{}, tftypes.NewValue(tftypes.Object{
59
+ _ , err := refl .Struct (context .Background (), types.ObjectType {}, tftypes .NewValue (tftypes.Object {
48
60
AttributeTypes : map [string ]tftypes.Type {},
49
61
}, map [string ]tftypes.Value {}), reflect .ValueOf (s ), refl.Options {}, tftypes .NewAttributePath ())
50
62
if err == nil {
@@ -55,11 +67,11 @@ func TestReflectObjectIntoStruct_objectMissingFields(t *testing.T) {
55
67
}
56
68
}
57
69
58
- func TestReflectObjectIntoStruct_structMissingProperties (t *testing.T) {
70
+ func TestStruct_structMissingProperties (t * testing.T ) {
59
71
t .Parallel ()
60
72
61
73
var s struct {}
62
- _, err := refl.ReflectStructFromObject (context.Background(), types.ObjectType{
74
+ _ , err := refl .Struct (context .Background (), types.ObjectType {
63
75
AttrTypes : map [string ]attr.Type {
64
76
"a" : types .StringType ,
65
77
},
@@ -78,13 +90,13 @@ func TestReflectObjectIntoStruct_structMissingProperties(t *testing.T) {
78
90
}
79
91
}
80
92
81
- func TestReflectObjectIntoStruct_objectMissingFieldsAndStructMissingProperties (t *testing.T) {
93
+ func TestStruct_objectMissingFieldsAndStructMissingProperties (t * testing.T ) {
82
94
t .Parallel ()
83
95
84
96
var s struct {
85
97
A string `tfsdk:"a"`
86
98
}
87
- _, err := refl.ReflectStructFromObject (context.Background(), types.ObjectType{
99
+ _ , err := refl .Struct (context .Background (), types.ObjectType {
88
100
AttrTypes : map [string ]attr.Type {
89
101
"a" : types .StringType ,
90
102
},
@@ -103,19 +115,19 @@ func TestReflectObjectIntoStruct_objectMissingFieldsAndStructMissingProperties(t
103
115
}
104
116
}
105
117
106
- func TestReflectObjectIntoStruct_primitives (t *testing.T) {
118
+ func TestStruct_primitives (t * testing.T ) {
107
119
t .Parallel ()
108
120
109
121
var s struct {
110
122
A string `tfsdk:"a"`
111
123
B * big.Float `tfsdk:"b"`
112
124
C bool `tfsdk:"c"`
113
125
}
114
- result, err := refl.ReflectStructFromObject (context.Background(), types.ObjectType{
126
+ result , err := refl .Struct (context .Background (), types.ObjectType {
115
127
AttrTypes : map [string ]attr.Type {
116
128
"a" : types .StringType ,
117
- "b": testNumberType{} ,
118
- "c": testBoolType{} ,
129
+ "b" : types . NumberType ,
130
+ "c" : types . BoolType ,
119
131
},
120
132
}, tftypes .NewValue (tftypes.Object {
121
133
AttributeTypes : map [string ]tftypes.Type {
@@ -143,7 +155,7 @@ func TestReflectObjectIntoStruct_primitives(t *testing.T) {
143
155
}
144
156
}
145
157
146
- func TestReflectObjectIntoStruct_complex (t *testing.T) {
158
+ func TestStruct_complex (t * testing.T ) {
147
159
t .Parallel ()
148
160
149
161
type myStruct struct {
@@ -156,42 +168,46 @@ func TestReflectObjectIntoStruct_complex(t *testing.T) {
156
168
A bool `tfsdk:"a"`
157
169
Slice []float64 `tfsdk:"slice"`
158
170
} `tfsdk:"struct"`
159
- Map map[string][]string `tfsdk:"map"`
160
- Pointer *string `tfsdk:"pointer"`
161
- Unknownable *unknownableString `tfsdk:"unknownable"`
162
- Nullable *nullableString `tfsdk:"nullable"`
163
- AttributeValue *testStringValue `tfsdk:"attribute_value"`
164
- ValueConverter *valueConverter `tfsdk:"value_converter"`
165
- UnhandledNull string `tfsdk:"unhandled_null"`
166
- UnhandledUnknown string `tfsdk:"unhandled_unknown"`
171
+ // TODO: uncomment when maps are supported
172
+ // Map map[string][]string `tfsdk:"map"`
173
+ Pointer * string `tfsdk:"pointer"`
174
+ Unknownable * unknownableString `tfsdk:"unknownable"`
175
+ Nullable * nullableString `tfsdk:"nullable"`
176
+ AttributeValue types.String `tfsdk:"attribute_value"`
177
+ ValueConverter * valueConverter `tfsdk:"value_converter"`
178
+ UnhandledNull string `tfsdk:"unhandled_null"`
179
+ UnhandledUnknown string `tfsdk:"unhandled_unknown"`
167
180
}
168
181
var s myStruct
169
- result, err := refl.ReflectStructFromObject (context.Background(), types.ObjectType{
182
+ result , err := refl .Struct (context .Background (), types.ObjectType {
170
183
AttrTypes : map [string ]attr.Type {
171
- "slice": testListType {
184
+ "slice" : types. ListType {
172
185
ElemType : types .StringType ,
173
186
},
174
- "slice_of_structs": testListType {
187
+ "slice_of_structs" : types. ListType {
175
188
ElemType : types.ObjectType {
176
189
AttrTypes : map [string ]attr.Type {
177
190
"a" : types .StringType ,
178
- "b": testNumberType{} ,
191
+ "b" : types . NumberType ,
179
192
},
180
193
},
181
194
},
182
195
"struct" : types.ObjectType {
183
196
AttrTypes : map [string ]attr.Type {
184
- "a": testBoolType{} ,
185
- "slice": testListType {
186
- ElemType: testNumberType{} ,
197
+ "a" : types . BoolType ,
198
+ "slice" : types. ListType {
199
+ ElemType : types . NumberType ,
187
200
},
188
201
},
189
202
},
190
- "map": testMapType{
191
- ElemType: testListType{
192
- ElemType: types.StringType,
203
+ // TODO: uncomment when maps are supported
204
+ /*
205
+ "map": testMapType{
206
+ ElemType: types.ListType{
207
+ ElemType: types.StringType,
208
+ },
193
209
},
194
- },
210
+ */
195
211
"pointer" : types .StringType ,
196
212
"unknownable" : types .StringType ,
197
213
"nullable" : types .StringType ,
@@ -221,11 +237,14 @@ func TestReflectObjectIntoStruct_complex(t *testing.T) {
221
237
},
222
238
},
223
239
},
224
- "map": tftypes.Map{
225
- AttributeType: tftypes.List{
226
- ElementType: tftypes.String,
240
+ // TODO: uncomment when maps are supported
241
+ /*
242
+ "map": tftypes.Map{
243
+ AttributeType: tftypes.List{
244
+ ElementType: tftypes.String,
245
+ },
227
246
},
228
- },
247
+ */
229
248
"pointer" : tftypes .String ,
230
249
"unknownable" : tftypes .String ,
231
250
"nullable" : tftypes .String ,
@@ -286,25 +305,28 @@ func TestReflectObjectIntoStruct_complex(t *testing.T) {
286
305
tftypes .NewValue (tftypes .Number , 789 ),
287
306
}),
288
307
}),
289
- "map": tftypes.NewValue(tftypes.Map{
290
- AttributeType: tftypes.List{
291
- ElementType: tftypes.String,
292
- },
293
- }, map[string]tftypes.Value{
294
- "colors": tftypes.NewValue(tftypes.List{
295
- ElementType: tftypes.String,
296
- }, []tftypes.Value{
297
- tftypes.NewValue(tftypes.String, "red"),
298
- tftypes.NewValue(tftypes.String, "orange"),
299
- tftypes.NewValue(tftypes.String, "yellow"),
300
- }),
301
- "fruits": tftypes.NewValue(tftypes.List{
302
- ElementType: tftypes.String,
303
- }, []tftypes.Value{
304
- tftypes.NewValue(tftypes.String, "apple"),
305
- tftypes.NewValue(tftypes.String, "banana"),
308
+ // TODO: uncomment when maps are supported
309
+ /*
310
+ "map": tftypes.NewValue(tftypes.Map{
311
+ AttributeType: tftypes.List{
312
+ ElementType: tftypes.String,
313
+ },
314
+ }, map[string]tftypes.Value{
315
+ "colors": tftypes.NewValue(tftypes.List{
316
+ ElementType: tftypes.String,
317
+ }, []tftypes.Value{
318
+ tftypes.NewValue(tftypes.String, "red"),
319
+ tftypes.NewValue(tftypes.String, "orange"),
320
+ tftypes.NewValue(tftypes.String, "yellow"),
321
+ }),
322
+ "fruits": tftypes.NewValue(tftypes.List{
323
+ ElementType: tftypes.String,
324
+ }, []tftypes.Value{
325
+ tftypes.NewValue(tftypes.String, "apple"),
326
+ tftypes.NewValue(tftypes.String, "banana"),
327
+ }),
306
328
}),
307
- }),
329
+ */
308
330
"pointer" : tftypes .NewValue (tftypes .String , "pointed" ),
309
331
"unknownable" : tftypes .NewValue (tftypes .String , tftypes .UnknownValue ),
310
332
"nullable" : tftypes .NewValue (tftypes .String , nil ),
@@ -343,18 +365,21 @@ func TestReflectObjectIntoStruct_complex(t *testing.T) {
343
365
A : true ,
344
366
Slice : []float64 {123 , 456 , 789 },
345
367
},
346
- Map: map[string][]string{
347
- "colors": {"red", "orange", "yellow"},
348
- "fruits": {"apple", "banana"},
349
- },
368
+ // TODO: uncomment when maps are supported
369
+ /*
370
+ Map: map[string][]string{
371
+ "colors": {"red", "orange", "yellow"},
372
+ "fruits": {"apple", "banana"},
373
+ },
374
+ */
350
375
Pointer : & str ,
351
376
Unknownable : & unknownableString {
352
377
Unknown : true ,
353
378
},
354
379
Nullable : & nullableString {
355
380
Null : true ,
356
381
},
357
- AttributeValue: &testStringValue {
382
+ AttributeValue : types. String {
358
383
Unknown : true ,
359
384
},
360
385
ValueConverter : & valueConverter {
@@ -367,4 +392,3 @@ func TestReflectObjectIntoStruct_complex(t *testing.T) {
367
392
t .Errorf ("Didn't get expected value. Diff (+ is expected, - is result): %s" , diff )
368
393
}
369
394
}
370
- */
0 commit comments