@@ -400,3 +400,134 @@ func TestUnmarshalerErr(t *testing.T) {
400
400
assert .EqualError (t , cfgMap .Unmarshal (tc ), expectErr )
401
401
assert .Empty (t , tc .Err .Foo )
402
402
}
403
+
404
+ func TestUnmarshalerSlices (t * testing.T ) {
405
+ type innerStructWithSlices struct {
406
+ Ints []int `mapstructure:"ints"`
407
+ }
408
+ type structWithSlices struct {
409
+ Strings []string `mapstructure:"strings"`
410
+ Nested innerStructWithSlices `mapstructure:"nested"`
411
+ }
412
+
413
+ tests := []struct {
414
+ name string
415
+ cfg map [string ]any
416
+ provided any
417
+ expected any
418
+ }{
419
+ {
420
+ name : "overridden by slice" ,
421
+ cfg : map [string ]any {
422
+ "strings" : []string {"111" },
423
+ },
424
+ provided : & structWithSlices {
425
+ Strings : []string {"xxx" , "yyyy" , "zzzz" },
426
+ },
427
+ expected : & structWithSlices {
428
+ Strings : []string {"111" },
429
+ },
430
+ },
431
+ {
432
+ name : "overridden by a bigger slice" ,
433
+ cfg : map [string ]any {
434
+ "strings" : []string {"111" , "222" , "333" },
435
+ },
436
+ provided : & structWithSlices {
437
+ Strings : []string {"xxx" , "yyyy" },
438
+ },
439
+ expected : & structWithSlices {
440
+ Strings : []string {"111" , "222" , "333" },
441
+ },
442
+ },
443
+ {
444
+ name : "overridden by an empty slice" ,
445
+ cfg : map [string ]any {
446
+ "strings" : []string {},
447
+ },
448
+ provided : & structWithSlices {
449
+ Strings : []string {"xxx" , "yyyy" },
450
+ },
451
+ expected : & structWithSlices {
452
+ Strings : []string {},
453
+ },
454
+ },
455
+ {
456
+ name : "not overridden by nil slice" ,
457
+ cfg : map [string ]any {
458
+ "strings" : []string (nil ),
459
+ },
460
+ provided : & structWithSlices {
461
+ Strings : []string {"xxx" , "yyyy" },
462
+ },
463
+ expected : & structWithSlices {
464
+ Strings : []string {"xxx" , "yyyy" },
465
+ },
466
+ },
467
+ {
468
+ name : "not overridden by nil" ,
469
+ cfg : map [string ]any {
470
+ "strings" : nil ,
471
+ },
472
+ provided : & structWithSlices {
473
+ Strings : []string {"xxx" , "yyyy" },
474
+ },
475
+ expected : & structWithSlices {
476
+ Strings : []string {"xxx" , "yyyy" },
477
+ },
478
+ },
479
+ {
480
+ name : "not overridden by missing value" ,
481
+ cfg : map [string ]any {},
482
+ provided : & structWithSlices {
483
+ Strings : []string {"xxx" , "yyyy" },
484
+ },
485
+ expected : & structWithSlices {
486
+ Strings : []string {"xxx" , "yyyy" },
487
+ },
488
+ },
489
+ {
490
+ name : "overridden by nested slice" ,
491
+ cfg : map [string ]any {
492
+ "nested" : map [string ]any {
493
+ "ints" : []int {777 },
494
+ },
495
+ },
496
+ provided : & structWithSlices {
497
+ Nested : innerStructWithSlices {
498
+ Ints : []int {1 , 2 , 3 },
499
+ },
500
+ },
501
+ expected : & structWithSlices {
502
+ Nested : innerStructWithSlices {
503
+ Ints : []int {777 },
504
+ },
505
+ },
506
+ },
507
+ {
508
+ name : "overridden by weakly typed input" ,
509
+ cfg : map [string ]any {
510
+ "strings" : "111" ,
511
+ },
512
+ provided : & structWithSlices {
513
+ Strings : []string {"xxx" , "yyyy" , "zzzz" },
514
+ },
515
+ expected : & structWithSlices {
516
+ Strings : []string {"111" },
517
+ },
518
+ },
519
+ }
520
+
521
+ for _ , tt := range tests {
522
+ tt := tt
523
+
524
+ t .Run (tt .name , func (t * testing.T ) {
525
+ cfg := NewFromStringMap (tt .cfg )
526
+
527
+ err := cfg .Unmarshal (tt .provided )
528
+ if assert .NoError (t , err ) {
529
+ assert .Equal (t , tt .expected , tt .provided )
530
+ }
531
+ })
532
+ }
533
+ }
0 commit comments