@@ -2,11 +2,14 @@ package tfsdk
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
6
+ "reflect"
5
7
"testing"
6
8
7
9
"github.com/google/go-cmp/cmp"
8
10
"github.com/hashicorp/terraform-plugin-framework/attr"
9
11
"github.com/hashicorp/terraform-plugin-framework/diag"
12
+ intreflect "github.com/hashicorp/terraform-plugin-framework/internal/reflect"
10
13
testtypes "github.com/hashicorp/terraform-plugin-framework/internal/testing/types"
11
14
"github.com/hashicorp/terraform-plugin-framework/types"
12
15
"github.com/hashicorp/terraform-plugin-go/tftypes"
@@ -155,6 +158,210 @@ func TestConfigGet_testTypes(t *testing.T) {
155
158
func TestConfigGetAttribute (t * testing.T ) {
156
159
t .Parallel ()
157
160
161
+ type testCase struct {
162
+ config Config
163
+ target interface {}
164
+ expected interface {}
165
+ expectedDiags diag.Diagnostics
166
+ }
167
+
168
+ testCases := map [string ]testCase {
169
+ "string" : {
170
+ config : Config {
171
+ Raw : tftypes .NewValue (tftypes.Object {
172
+ AttributeTypes : map [string ]tftypes.Type {
173
+ "name" : tftypes .String ,
174
+ },
175
+ }, map [string ]tftypes.Value {
176
+ "name" : tftypes .NewValue (tftypes .String , "namevalue" ),
177
+ }),
178
+ Schema : Schema {
179
+ Attributes : map [string ]Attribute {
180
+ "name" : {
181
+ Type : types .StringType ,
182
+ Required : true ,
183
+ },
184
+ },
185
+ },
186
+ },
187
+ target : new (string ),
188
+ expected : newStringPointer ("namevalue" ),
189
+ },
190
+ "*string" : {
191
+ config : Config {
192
+ Raw : tftypes .NewValue (tftypes.Object {
193
+ AttributeTypes : map [string ]tftypes.Type {
194
+ "name" : tftypes .String ,
195
+ },
196
+ }, map [string ]tftypes.Value {
197
+ "name" : tftypes .NewValue (tftypes .String , "namevalue" ),
198
+ }),
199
+ Schema : Schema {
200
+ Attributes : map [string ]Attribute {
201
+ "name" : {
202
+ Type : types .StringType ,
203
+ Required : true ,
204
+ },
205
+ },
206
+ },
207
+ },
208
+ target : new (* string ),
209
+ expected : newStringPointerPointer ("namevalue" ),
210
+ },
211
+ "types.String" : {
212
+ config : Config {
213
+ Raw : tftypes .NewValue (tftypes.Object {
214
+ AttributeTypes : map [string ]tftypes.Type {
215
+ "name" : tftypes .String ,
216
+ },
217
+ }, map [string ]tftypes.Value {
218
+ "name" : tftypes .NewValue (tftypes .String , "namevalue" ),
219
+ }),
220
+ Schema : Schema {
221
+ Attributes : map [string ]Attribute {
222
+ "name" : {
223
+ Type : types .StringType ,
224
+ Required : true ,
225
+ },
226
+ },
227
+ },
228
+ },
229
+ target : new (types.String ),
230
+ expected : & types.String {Value : "namevalue" },
231
+ },
232
+ "incompatible-target" : {
233
+ config : Config {
234
+ Raw : tftypes .NewValue (tftypes.Object {
235
+ AttributeTypes : map [string ]tftypes.Type {
236
+ "name" : tftypes .String ,
237
+ },
238
+ }, map [string ]tftypes.Value {
239
+ "name" : tftypes .NewValue (tftypes .String , "namevalue" ),
240
+ }),
241
+ Schema : Schema {
242
+ Attributes : map [string ]Attribute {
243
+ "name" : {
244
+ Type : types .StringType ,
245
+ Required : true ,
246
+ },
247
+ },
248
+ },
249
+ },
250
+ target : new (testtypes.String ),
251
+ expected : new (testtypes.String ),
252
+ expectedDiags : diag.Diagnostics {
253
+ diag .NewAttributeErrorDiagnostic (
254
+ tftypes .NewAttributePath ().WithAttributeName ("name" ),
255
+ "Value Conversion Error" ,
256
+ intreflect.DiagNewAttributeValueIntoWrongType {
257
+ ValType : reflect .TypeOf (types.String {Value : "namevalue" }),
258
+ TargetType : reflect .TypeOf (testtypes.String {}),
259
+ AttrPath : tftypes .NewAttributePath ().WithAttributeName ("name" ),
260
+ SchemaType : types .StringType ,
261
+ }.Detail (),
262
+ ),
263
+ },
264
+ },
265
+ "incompatible-type" : {
266
+ config : Config {
267
+ Raw : tftypes .NewValue (tftypes.Object {
268
+ AttributeTypes : map [string ]tftypes.Type {
269
+ "name" : tftypes .String ,
270
+ },
271
+ }, map [string ]tftypes.Value {
272
+ "name" : tftypes .NewValue (tftypes .String , "namevalue" ),
273
+ }),
274
+ Schema : Schema {
275
+ Attributes : map [string ]Attribute {
276
+ "name" : {
277
+ Type : types .StringType ,
278
+ Required : true ,
279
+ },
280
+ },
281
+ },
282
+ },
283
+ target : new (bool ),
284
+ expected : new (bool ),
285
+ expectedDiags : diag.Diagnostics {
286
+ diag .NewAttributeErrorDiagnostic (
287
+ tftypes .NewAttributePath ().WithAttributeName ("name" ),
288
+ "Value Conversion Error" ,
289
+ intreflect.DiagIntoIncompatibleType {
290
+ Val : tftypes .NewValue (tftypes .String , "namevalue" ),
291
+ TargetType : reflect .TypeOf (false ),
292
+ Err : fmt .Errorf ("can't unmarshal %s into *%T, expected boolean" , tftypes .String , false ),
293
+ AttrPath : tftypes .NewAttributePath ().WithAttributeName ("name" ),
294
+ }.Detail (),
295
+ ),
296
+ },
297
+ },
298
+ "AttrTypeWithValidateError" : {
299
+ config : Config {
300
+ Raw : tftypes .NewValue (tftypes.Object {
301
+ AttributeTypes : map [string ]tftypes.Type {
302
+ "name" : tftypes .String ,
303
+ },
304
+ }, map [string ]tftypes.Value {
305
+ "name" : tftypes .NewValue (tftypes .String , "namevalue" ),
306
+ }),
307
+ Schema : Schema {
308
+ Attributes : map [string ]Attribute {
309
+ "name" : {
310
+ Type : testtypes.StringTypeWithValidateError {},
311
+ Required : true ,
312
+ },
313
+ },
314
+ },
315
+ },
316
+ target : new (testtypes.String ),
317
+ expected : new (testtypes.String ),
318
+ expectedDiags : diag.Diagnostics {testtypes .TestErrorDiagnostic (tftypes .NewAttributePath ().WithAttributeName ("name" ))},
319
+ },
320
+ "AttrTypeWithValidateWarning" : {
321
+ config : Config {
322
+ Raw : tftypes .NewValue (tftypes.Object {
323
+ AttributeTypes : map [string ]tftypes.Type {
324
+ "name" : tftypes .String ,
325
+ },
326
+ }, map [string ]tftypes.Value {
327
+ "name" : tftypes .NewValue (tftypes .String , "namevalue" ),
328
+ }),
329
+ Schema : Schema {
330
+ Attributes : map [string ]Attribute {
331
+ "name" : {
332
+ Type : testtypes.StringTypeWithValidateWarning {},
333
+ Required : true ,
334
+ },
335
+ },
336
+ },
337
+ },
338
+ target : new (testtypes.String ),
339
+ expected : & testtypes.String {String : types.String {Value : "namevalue" }, CreatedBy : testtypes.StringTypeWithValidateWarning {}},
340
+ expectedDiags : diag.Diagnostics {testtypes .TestWarningDiagnostic (tftypes .NewAttributePath ().WithAttributeName ("name" ))},
341
+ },
342
+ }
343
+
344
+ for name , tc := range testCases {
345
+ name , tc := name , tc
346
+ t .Run (name , func (t * testing.T ) {
347
+ t .Parallel ()
348
+
349
+ diags := tc .config .GetAttribute (context .Background (), tftypes .NewAttributePath ().WithAttributeName ("name" ), tc .target )
350
+
351
+ if diff := cmp .Diff (diags , tc .expectedDiags ); diff != "" {
352
+ t .Errorf ("unexpected diagnostics (+wanted, -got): %s" , diff )
353
+ }
354
+
355
+ if diff := cmp .Diff (tc .target , tc .expected , cmp .Transformer ("testtypes" , func (in * testtypes.String ) testtypes.String { return * in }), cmp .Transformer ("types" , func (in * types.String ) types.String { return * in })); diff != "" {
356
+ t .Errorf ("unexpected value (+wanted, -got): %s" , diff )
357
+ }
358
+ })
359
+ }
360
+ }
361
+
362
+ func TestConfigGetAttributeValue (t * testing.T ) {
363
+ t .Parallel ()
364
+
158
365
type testCase struct {
159
366
config Config
160
367
path * tftypes.AttributePath
@@ -1025,7 +1232,8 @@ func TestConfigGetAttribute(t *testing.T) {
1025
1232
t .Run (name , func (t * testing.T ) {
1026
1233
t .Parallel ()
1027
1234
1028
- val , diags := tc .config .GetAttribute (context .Background (), tc .path )
1235
+ val , diags := tc .config .getAttributeValue (context .Background (), tc .path )
1236
+
1029
1237
if diff := cmp .Diff (diags , tc .expectedDiags ); diff != "" {
1030
1238
t .Errorf ("unexpected diagnostics (+wanted, -got): %s" , diff )
1031
1239
}
0 commit comments