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