@@ -8,12 +8,14 @@ import (
8
8
"github.com/hashicorp/terraform-plugin-framework/attr"
9
9
"github.com/hashicorp/terraform-plugin-framework/diag"
10
10
"github.com/hashicorp/terraform-plugin-framework/types"
11
+ "github.com/hashicorp/terraform-plugin-go/tftypes"
11
12
)
12
13
13
14
type person struct {
14
15
Name types.String `tfsdk:"name"`
15
16
Age types.Int64 `tfsdk:"age"`
16
17
OptedIn types.Bool `tfsdk:"opted_in"`
18
+ Address types.List `tfsdk:"address"`
17
19
}
18
20
19
21
func TestValueFrom (t * testing.T ) {
@@ -23,41 +25,76 @@ func TestValueFrom(t *testing.T) {
23
25
"name" : types .StringType ,
24
26
"age" : types .Int64Type ,
25
27
"opted_in" : types .BoolType ,
28
+ "address" : types.ListType {
29
+ ElemType : types .StringType ,
30
+ },
26
31
}
27
32
28
- x := person {
33
+ mrX := person {
29
34
Name : types.String {Value : "x" },
30
35
Age : types.Int64 {Value : 30 },
31
36
OptedIn : types.Bool {Value : true },
37
+ Address : types.List {
38
+ ElemType : types .StringType ,
39
+ Elems : []attr.Value {
40
+ types.String {Value : "1" },
41
+ types.String {Value : "Beckford Close" },
42
+ types.String {Value : "Gotham" },
43
+ },
44
+ },
32
45
}
33
46
34
- y := person {
47
+ mrsY := person {
35
48
Name : types.String {Value : "y" },
36
49
Age : types.Int64 {Value : 23 },
37
50
OptedIn : types.Bool {Value : false },
51
+ Address : types.List {
52
+ ElemType : types .StringType ,
53
+ Elems : []attr.Value {
54
+ types.String {Value : "2" },
55
+ types.String {Value : "Windmill Close" },
56
+ types.String {Value : "Smallville" },
57
+ },
58
+ },
38
59
}
39
60
40
- xObj := types.Object {
61
+ expectedMrXObj := types.Object {
41
62
AttrTypes : personAttrTypes ,
42
63
Attrs : map [string ]attr.Value {
43
64
"name" : types.String {Value : "x" , Unknown : false , Null : false },
44
65
"age" : types.Int64 {Value : 30 , Unknown : false , Null : false },
45
66
"opted_in" : types.Bool {Value : true , Unknown : false , Null : false },
67
+ "address" : types.List {
68
+ ElemType : types .StringType ,
69
+ Elems : []attr.Value {
70
+ types.String {Value : "1" },
71
+ types.String {Value : "Beckford Close" },
72
+ types.String {Value : "Gotham" },
73
+ },
74
+ },
46
75
},
47
76
}
48
77
49
- yObj := types.Object {
78
+ expectedMrsYObj := types.Object {
50
79
AttrTypes : personAttrTypes ,
51
80
Attrs : map [string ]attr.Value {
52
81
"name" : types.String {Value : "y" , Unknown : false , Null : false },
53
82
"age" : types.Int64 {Value : 23 , Unknown : false , Null : false },
54
83
"opted_in" : types.Bool {Value : false , Unknown : false , Null : false },
84
+ "address" : types.List {
85
+ ElemType : types .StringType ,
86
+ Elems : []attr.Value {
87
+ types.String {Value : "2" },
88
+ types.String {Value : "Windmill Close" },
89
+ types.String {Value : "Smallville" },
90
+ },
91
+ },
55
92
},
56
93
}
57
94
58
95
type testCase struct {
59
- target attr.Value
60
96
val interface {}
97
+ target attr.Value
61
98
expected attr.Value
62
99
expectedDiags diag.Diagnostics
63
100
}
@@ -69,14 +106,14 @@ func TestValueFrom(t *testing.T) {
69
106
expected : types.String {Value : "hello" , Unknown : false , Null : false },
70
107
},
71
108
"struct" : {
72
- val : x ,
109
+ val : mrX ,
73
110
target : types.Object {
74
111
AttrTypes : personAttrTypes ,
75
112
},
76
- expected : xObj ,
113
+ expected : expectedMrXObj ,
77
114
},
78
115
"list" : {
79
- val : []person {x , y },
116
+ val : []person {mrX , mrsY },
80
117
target : types.List {
81
118
ElemType : types.ObjectType {
82
119
AttrTypes : personAttrTypes ,
@@ -86,31 +123,30 @@ func TestValueFrom(t *testing.T) {
86
123
ElemType : types.ObjectType {
87
124
AttrTypes : personAttrTypes ,
88
125
},
89
- Elems : []attr.Value {xObj , yObj },
126
+ Elems : []attr.Value {expectedMrXObj , expectedMrsYObj },
127
+ },
128
+ },
129
+ "incompatible-type" : {
130
+ val : 0 ,
131
+ target : types.String {},
132
+ expectedDiags : diag.Diagnostics {
133
+ diag .WithPath (
134
+ tftypes .NewAttributePath (),
135
+ diag .NewErrorDiagnostic (
136
+ "Value Conversion Error" ,
137
+ "An unexpected error was encountered trying to convert the Terraform value. This is always an error in the provider. Please report the following to the provider developer:\n \n can't unmarshal tftypes.Number into *string, expected string" ,
138
+ ),
139
+ ),
90
140
},
91
141
},
92
- //"incompatible-type": {
93
- // val: 0,
94
- // target: types.String{},
95
- // expectedDiags: diag.Diagnostics{
96
- // diag.WithPath(
97
- // tftypes.NewAttributePath(),
98
- // reflect.DiagIntoIncompatibleType{
99
- // Val: tftypes.NewValue(tftypes.String, ""),
100
- // TargetType: goreflect.TypeOf(int64(0)),
101
- // Err: fmt.Errorf("unexpected error was encountered trying to convert from value"),
102
- // },
103
- // ),
104
- // },
105
- //},
106
142
}
107
143
108
144
for name , tc := range tests {
109
145
name , tc := name , tc
110
146
t .Run (name , func (t * testing.T ) {
111
147
t .Parallel ()
112
148
113
- diags := ValueFrom (context .Background (), tc .target .Type (context .Background ()), & tc .target , & tc . val )
149
+ diags := ValueFrom (context .Background (), tc .val , tc . target .Type (context .Background ()), & tc .target )
114
150
115
151
if diff := cmp .Diff (tc .expectedDiags , diags ); diff != "" {
116
152
t .Fatalf ("Unexpected diff in diagnostics (-wanted, +got): %s" , diff )
0 commit comments