@@ -299,72 +299,133 @@ func TestReflectStructMutate(t *testing.T) {
299
299
}
300
300
}
301
301
302
- // TestReflectMutateNestedStruct ensures a structs nested within various typed can be modified.
302
+ // TestReflectMutateNestedStruct ensures a structs field within various typed can be modified.
303
303
func TestReflectMutateNestedStruct (t * testing.T ) {
304
- type nested struct {
304
+ type field struct {
305
305
S string `json:"s,omitempty"`
306
306
}
307
- type root struct {
308
- Field nested `json:"field"`
309
- List []nested `json:"list"`
310
- Map map [string ]nested `json:"map"`
311
- MapOfMaps map [string ]map [string ]nested `json:"mapOfMaps"`
312
- MapOfLists map [string ][]nested `json:"mapOfLists"`
313
- }
314
- rv := MustReflect (& root {
315
- Field : nested {S : "field" },
316
- List : []nested {{S : "listItem" }},
317
- Map : map [string ]nested {"mapKey" : {S : "mapItem" }},
318
- MapOfMaps : map [string ]map [string ]nested {"outer" : {"inner" : {S : "mapOfMapItem" }}},
319
- MapOfLists : map [string ][]nested {"outer" : {{S : "mapOfListsItem" }}},
320
- })
321
- rootMap := rv .AsMap ()
322
- field , _ := rootMap .Get ("field" )
323
- list , _ := rootMap .Get ("list" )
324
- listItem := list .AsList ().At (0 )
325
- m , _ := rootMap .Get ("map" )
326
- mapItem , _ := m .AsMap ().Get ("mapKey" )
327
- mapOfMaps , _ := rootMap .Get ("mapOfMaps" )
328
- innerMap , _ := mapOfMaps .AsMap ().Get ("outer" )
329
- mapOfMapsItem , _ := innerMap .AsMap ().Get ("inner" )
330
- mapOfLists , _ := rootMap .Get ("mapOfLists" )
331
- innerList , _ := mapOfLists .AsMap ().Get ("outer" )
332
- mapOfListsItem := innerList .AsList ().At (0 )
333
-
334
- field .AsMap ().Set ("s" , NewValueInterface ("field2" ))
335
- listItem .AsMap ().Set ("s" , NewValueInterface ("listItem2" ))
336
- mapItem .AsMap ().Set ("s" , NewValueInterface ("mapItem2" ))
337
- mapOfMapsItem .AsMap ().Set ("s" , NewValueInterface ("mapOfMapItem2" ))
338
- mapOfListsItem .AsMap ().Set ("s" , NewValueInterface ("mapOfListsItem2" ))
339
307
340
- unstructured := rv .Unstructured ()
341
- expectedMap := map [string ]interface {}{
342
- "field" : map [string ]interface {}{"s" : "field2" },
343
- "list" : []interface {}{map [string ]interface {}{"s" : "listItem2" }},
344
- "map" : map [string ]interface {}{"mapKey" : map [string ]interface {}{"s" : "mapItem2" }},
345
- "mapOfMaps" : map [string ]interface {}{"outer" : map [string ]interface {}{"inner" : map [string ]interface {}{"s" : "mapOfMapItem2" }}},
346
- "mapOfLists" : map [string ]interface {}{"outer" : []interface {}{map [string ]interface {}{"s" : "mapOfListsItem2" }}},
347
- }
348
- if ! reflect .DeepEqual (unstructured , expectedMap ) {
349
- t .Errorf ("expected %v but got: %v" , expectedMap , unstructured )
308
+ cases := []struct {
309
+ fieldName string
310
+ root Value
311
+ lookupField func (root Value ) Value
312
+ expectUpdated interface {}
313
+ expectDeleted interface {}
314
+ }{
315
+ {
316
+ fieldName : "field" ,
317
+ root : MustReflect (& struct {
318
+ Field field `json:"field,omitempty"`
319
+ }{
320
+ Field : field {S : "field" },
321
+ }),
322
+ lookupField : func (rv Value ) Value {
323
+ field , _ := rv .AsMap ().Get ("field" )
324
+ return field
325
+ },
326
+ expectUpdated : map [string ]interface {}{
327
+ "field" : map [string ]interface {}{"s" : "updatedValue" },
328
+ },
329
+ expectDeleted : map [string ]interface {}{
330
+ "field" : map [string ]interface {}{},
331
+ },
332
+ },
333
+ {
334
+ fieldName : "map" ,
335
+ root : MustReflect (& struct {
336
+ Map map [string ]field `json:"map,omitempty"`
337
+ }{
338
+ Map : map [string ]field {"mapKey" : {S : "mapItem" }},
339
+ }),
340
+ lookupField : func (rv Value ) Value {
341
+ m , _ := rv .AsMap ().Get ("map" )
342
+ mapItem , _ := m .AsMap ().Get ("mapKey" )
343
+ return mapItem
344
+ },
345
+ expectUpdated : map [string ]interface {}{
346
+ "map" : map [string ]interface {}{"mapKey" : map [string ]interface {}{"s" : "updatedValue" }},
347
+ },
348
+ expectDeleted : map [string ]interface {}{
349
+ "map" : map [string ]interface {}{"mapKey" : map [string ]interface {}{}},
350
+ },
351
+ },
352
+ {
353
+ fieldName : "list" ,
354
+ root : MustReflect (& struct {
355
+ List []field `json:"list,omitempty"`
356
+ }{
357
+ List : []field {{S : "listItem" }},
358
+ }),
359
+ lookupField : func (rv Value ) Value {
360
+ list , _ := rv .AsMap ().Get ("list" )
361
+ return list .AsList ().At (0 )
362
+ },
363
+ expectUpdated : map [string ]interface {}{
364
+ "list" : []interface {}{map [string ]interface {}{"s" : "updatedValue" }},
365
+ },
366
+ expectDeleted : map [string ]interface {}{
367
+ "list" : []interface {}{map [string ]interface {}{}},
368
+ },
369
+ },
370
+ {
371
+ fieldName : "mapOfMaps" ,
372
+ root : MustReflect (& struct {
373
+ MapOfMaps map [string ]map [string ]field `json:"mapOfMaps,omitempty"`
374
+ }{
375
+ MapOfMaps : map [string ]map [string ]field {"outer" : {"inner" : {S : "mapOfMapItem" }}},
376
+ }),
377
+ lookupField : func (rv Value ) Value {
378
+ mapOfMaps , _ := rv .AsMap ().Get ("mapOfMaps" )
379
+ innerMap , _ := mapOfMaps .AsMap ().Get ("outer" )
380
+ mapOfMapsItem , _ := innerMap .AsMap ().Get ("inner" )
381
+ return mapOfMapsItem
382
+ },
383
+ expectUpdated : map [string ]interface {}{
384
+ "mapOfMaps" : map [string ]interface {}{"outer" : map [string ]interface {}{"inner" : map [string ]interface {}{"s" : "updatedValue" }}},
385
+ },
386
+ expectDeleted : map [string ]interface {}{
387
+ "mapOfMaps" : map [string ]interface {}{"outer" : map [string ]interface {}{"inner" : map [string ]interface {}{}}},
388
+ },
389
+ },
390
+ {
391
+ fieldName : "mapOfLists" ,
392
+ root : MustReflect (& struct {
393
+ MapOfLists map [string ][]field `json:"mapOfLists,omitempty"`
394
+ }{
395
+ MapOfLists : map [string ][]field {"outer" : {{S : "mapOfListsItem" }}},
396
+ }),
397
+ lookupField : func (rv Value ) Value {
398
+ mapOfLists , _ := rv .AsMap ().Get ("mapOfLists" )
399
+ innerList , _ := mapOfLists .AsMap ().Get ("outer" )
400
+ mapOfListsItem := innerList .AsList ().At (0 )
401
+ return mapOfListsItem
402
+ },
403
+
404
+ expectUpdated : map [string ]interface {}{
405
+ "mapOfLists" : map [string ]interface {}{"outer" : []interface {}{map [string ]interface {}{"s" : "updatedValue" }}},
406
+ },
407
+ expectDeleted : map [string ]interface {}{
408
+ "mapOfLists" : map [string ]interface {}{"outer" : []interface {}{map [string ]interface {}{}}},
409
+ },
410
+ },
350
411
}
351
412
352
- field .AsMap ().Delete ("s" )
353
- listItem .AsMap ().Delete ("s" )
354
- mapItem .AsMap ().Delete ("s" )
355
- mapOfMapsItem .AsMap ().Delete ("s" )
356
- mapOfListsItem .AsMap ().Delete ("s" )
413
+ for _ , tc := range cases {
414
+ t .Run (tc .fieldName , func (t * testing.T ) {
415
+ root := tc .root
416
+ field := tc .lookupField (root )
417
+ field .AsMap ().Set ("s" , NewValueInterface ("updatedValue" ))
418
+ unstructured := root .Unstructured ()
419
+ if ! reflect .DeepEqual (unstructured , tc .expectUpdated ) {
420
+ t .Errorf ("expected %v but got: %v" , tc .expectUpdated , unstructured )
421
+ }
357
422
358
- unstructured = rv .Unstructured ()
359
- expectedMap = map [string ]interface {}{
360
- "field" : map [string ]interface {}{},
361
- "list" : []interface {}{map [string ]interface {}{}},
362
- "map" : map [string ]interface {}{"mapKey" : map [string ]interface {}{}},
363
- "mapOfMaps" : map [string ]interface {}{"outer" : map [string ]interface {}{"inner" : map [string ]interface {}{}}},
364
- "mapOfLists" : map [string ]interface {}{"outer" : []interface {}{map [string ]interface {}{}}},
365
- }
366
- if ! reflect .DeepEqual (unstructured , expectedMap ) {
367
- t .Errorf ("expected %v but got: %v" , expectedMap , unstructured )
423
+ field .AsMap ().Delete ("s" )
424
+ unstructured = root .Unstructured ()
425
+ if ! reflect .DeepEqual (unstructured , tc .expectDeleted ) {
426
+ t .Errorf ("expected %v but got: %v" , tc .expectDeleted , unstructured )
427
+ }
428
+ })
368
429
}
369
430
}
370
431
0 commit comments