This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 679
/
mapstructure_test.go
2796 lines (2358 loc) · 55.7 KB
/
mapstructure_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package mapstructure
import (
"encoding/json"
"io"
"reflect"
"sort"
"strings"
"testing"
"time"
)
type Basic struct {
Vstring string
Vint int
Vint8 int8
Vint16 int16
Vint32 int32
Vint64 int64
Vuint uint
Vbool bool
Vfloat float64
Vextra string
vsilent bool
Vdata interface{}
VjsonInt int
VjsonUint uint
VjsonUint64 uint64
VjsonFloat float64
VjsonNumber json.Number
}
type BasicPointer struct {
Vstring *string
Vint *int
Vuint *uint
Vbool *bool
Vfloat *float64
Vextra *string
vsilent *bool
Vdata *interface{}
VjsonInt *int
VjsonFloat *float64
VjsonNumber *json.Number
}
type BasicSquash struct {
Test Basic `mapstructure:",squash"`
}
type Embedded struct {
Basic
Vunique string
}
type EmbeddedPointer struct {
*Basic
Vunique string
}
type EmbeddedSquash struct {
Basic `mapstructure:",squash"`
Vunique string
}
type EmbeddedPointerSquash struct {
*Basic `mapstructure:",squash"`
Vunique string
}
type BasicMapStructure struct {
Vunique string `mapstructure:"vunique"`
Vtime *time.Time `mapstructure:"time"`
}
type NestedPointerWithMapstructure struct {
Vbar *BasicMapStructure `mapstructure:"vbar"`
}
type EmbeddedPointerSquashWithNestedMapstructure struct {
*NestedPointerWithMapstructure `mapstructure:",squash"`
Vunique string
}
type EmbeddedAndNamed struct {
Basic
Named Basic
Vunique string
}
type SliceAlias []string
type EmbeddedSlice struct {
SliceAlias `mapstructure:"slice_alias"`
Vunique string
}
type ArrayAlias [2]string
type EmbeddedArray struct {
ArrayAlias `mapstructure:"array_alias"`
Vunique string
}
type SquashOnNonStructType struct {
InvalidSquashType int `mapstructure:",squash"`
}
type Map struct {
Vfoo string
Vother map[string]string
}
type MapOfStruct struct {
Value map[string]Basic
}
type Nested struct {
Vfoo string
Vbar Basic
}
type NestedPointer struct {
Vfoo string
Vbar *Basic
}
type NilInterface struct {
W io.Writer
}
type NilPointer struct {
Value *string
}
type Slice struct {
Vfoo string
Vbar []string
}
type SliceOfByte struct {
Vfoo string
Vbar []byte
}
type SliceOfAlias struct {
Vfoo string
Vbar SliceAlias
}
type SliceOfStruct struct {
Value []Basic
}
type SlicePointer struct {
Vbar *[]string
}
type Array struct {
Vfoo string
Vbar [2]string
}
type ArrayOfStruct struct {
Value [2]Basic
}
type Func struct {
Foo func() string
}
type Tagged struct {
Extra string `mapstructure:"bar,what,what"`
Value string `mapstructure:"foo"`
}
type Remainder struct {
A string
Extra map[string]interface{} `mapstructure:",remain"`
}
type StructWithOmitEmpty struct {
VisibleStringField string `mapstructure:"visible-string"`
OmitStringField string `mapstructure:"omittable-string,omitempty"`
VisibleIntField int `mapstructure:"visible-int"`
OmitIntField int `mapstructure:"omittable-int,omitempty"`
VisibleFloatField float64 `mapstructure:"visible-float"`
OmitFloatField float64 `mapstructure:"omittable-float,omitempty"`
VisibleSliceField []interface{} `mapstructure:"visible-slice"`
OmitSliceField []interface{} `mapstructure:"omittable-slice,omitempty"`
VisibleMapField map[string]interface{} `mapstructure:"visible-map"`
OmitMapField map[string]interface{} `mapstructure:"omittable-map,omitempty"`
NestedField *Nested `mapstructure:"visible-nested"`
OmitNestedField *Nested `mapstructure:"omittable-nested,omitempty"`
}
type TypeConversionResult struct {
IntToFloat float32
IntToUint uint
IntToBool bool
IntToString string
UintToInt int
UintToFloat float32
UintToBool bool
UintToString string
BoolToInt int
BoolToUint uint
BoolToFloat float32
BoolToString string
FloatToInt int
FloatToUint uint
FloatToBool bool
FloatToString string
SliceUint8ToString string
StringToSliceUint8 []byte
ArrayUint8ToString string
StringToInt int
StringToUint uint
StringToBool bool
StringToFloat float32
StringToStrSlice []string
StringToIntSlice []int
StringToStrArray [1]string
StringToIntArray [1]int
SliceToMap map[string]interface{}
MapToSlice []interface{}
ArrayToMap map[string]interface{}
MapToArray [1]interface{}
}
func TestBasicTypes(t *testing.T) {
t.Parallel()
input := map[string]interface{}{
"vstring": "foo",
"vint": 42,
"vint8": 42,
"vint16": 42,
"vint32": 42,
"vint64": 42,
"Vuint": 42,
"vbool": true,
"Vfloat": 42.42,
"vsilent": true,
"vdata": 42,
"vjsonInt": json.Number("1234"),
"vjsonUint": json.Number("1234"),
"vjsonUint64": json.Number("9223372036854775809"), // 2^63 + 1
"vjsonFloat": json.Number("1234.5"),
"vjsonNumber": json.Number("1234.5"),
}
var result Basic
err := Decode(input, &result)
if err != nil {
t.Errorf("got an err: %s", err.Error())
t.FailNow()
}
if result.Vstring != "foo" {
t.Errorf("vstring value should be 'foo': %#v", result.Vstring)
}
if result.Vint != 42 {
t.Errorf("vint value should be 42: %#v", result.Vint)
}
if result.Vint8 != 42 {
t.Errorf("vint8 value should be 42: %#v", result.Vint)
}
if result.Vint16 != 42 {
t.Errorf("vint16 value should be 42: %#v", result.Vint)
}
if result.Vint32 != 42 {
t.Errorf("vint32 value should be 42: %#v", result.Vint)
}
if result.Vint64 != 42 {
t.Errorf("vint64 value should be 42: %#v", result.Vint)
}
if result.Vuint != 42 {
t.Errorf("vuint value should be 42: %#v", result.Vuint)
}
if result.Vbool != true {
t.Errorf("vbool value should be true: %#v", result.Vbool)
}
if result.Vfloat != 42.42 {
t.Errorf("vfloat value should be 42.42: %#v", result.Vfloat)
}
if result.Vextra != "" {
t.Errorf("vextra value should be empty: %#v", result.Vextra)
}
if result.vsilent != false {
t.Error("vsilent should not be set, it is unexported")
}
if result.Vdata != 42 {
t.Error("vdata should be valid")
}
if result.VjsonInt != 1234 {
t.Errorf("vjsonint value should be 1234: %#v", result.VjsonInt)
}
if result.VjsonUint != 1234 {
t.Errorf("vjsonuint value should be 1234: %#v", result.VjsonUint)
}
if result.VjsonUint64 != 9223372036854775809 {
t.Errorf("vjsonuint64 value should be 9223372036854775809: %#v", result.VjsonUint64)
}
if result.VjsonFloat != 1234.5 {
t.Errorf("vjsonfloat value should be 1234.5: %#v", result.VjsonFloat)
}
if !reflect.DeepEqual(result.VjsonNumber, json.Number("1234.5")) {
t.Errorf("vjsonnumber value should be '1234.5': %T, %#v", result.VjsonNumber, result.VjsonNumber)
}
}
func TestBasic_IntWithFloat(t *testing.T) {
t.Parallel()
input := map[string]interface{}{
"vint": float64(42),
}
var result Basic
err := Decode(input, &result)
if err != nil {
t.Fatalf("got an err: %s", err)
}
}
func TestBasic_Merge(t *testing.T) {
t.Parallel()
input := map[string]interface{}{
"vint": 42,
}
var result Basic
result.Vuint = 100
err := Decode(input, &result)
if err != nil {
t.Fatalf("got an err: %s", err)
}
expected := Basic{
Vint: 42,
Vuint: 100,
}
if !reflect.DeepEqual(result, expected) {
t.Fatalf("bad: %#v", result)
}
}
// Test for issue #46.
func TestBasic_Struct(t *testing.T) {
t.Parallel()
input := map[string]interface{}{
"vdata": map[string]interface{}{
"vstring": "foo",
},
}
var result, inner Basic
result.Vdata = &inner
err := Decode(input, &result)
if err != nil {
t.Fatalf("got an err: %s", err)
}
expected := Basic{
Vdata: &Basic{
Vstring: "foo",
},
}
if !reflect.DeepEqual(result, expected) {
t.Fatalf("bad: %#v", result)
}
}
func TestBasic_interfaceStruct(t *testing.T) {
t.Parallel()
input := map[string]interface{}{
"vstring": "foo",
}
var iface interface{} = &Basic{}
err := Decode(input, &iface)
if err != nil {
t.Fatalf("got an err: %s", err)
}
expected := &Basic{
Vstring: "foo",
}
if !reflect.DeepEqual(iface, expected) {
t.Fatalf("bad: %#v", iface)
}
}
// Issue 187
func TestBasic_interfaceStructNonPtr(t *testing.T) {
t.Parallel()
input := map[string]interface{}{
"vstring": "foo",
}
var iface interface{} = Basic{}
err := Decode(input, &iface)
if err != nil {
t.Fatalf("got an err: %s", err)
}
expected := Basic{
Vstring: "foo",
}
if !reflect.DeepEqual(iface, expected) {
t.Fatalf("bad: %#v", iface)
}
}
func TestDecode_BasicSquash(t *testing.T) {
t.Parallel()
input := map[string]interface{}{
"vstring": "foo",
}
var result BasicSquash
err := Decode(input, &result)
if err != nil {
t.Fatalf("got an err: %s", err.Error())
}
if result.Test.Vstring != "foo" {
t.Errorf("vstring value should be 'foo': %#v", result.Test.Vstring)
}
}
func TestDecodeFrom_BasicSquash(t *testing.T) {
t.Parallel()
var v interface{}
var ok bool
input := BasicSquash{
Test: Basic{
Vstring: "foo",
},
}
var result map[string]interface{}
err := Decode(input, &result)
if err != nil {
t.Fatalf("got an err: %s", err.Error())
}
if _, ok = result["Test"]; ok {
t.Error("test should not be present in map")
}
v, ok = result["Vstring"]
if !ok {
t.Error("vstring should be present in map")
} else if !reflect.DeepEqual(v, "foo") {
t.Errorf("vstring value should be 'foo': %#v", v)
}
}
func TestDecode_Embedded(t *testing.T) {
t.Parallel()
input := map[string]interface{}{
"vstring": "foo",
"Basic": map[string]interface{}{
"vstring": "innerfoo",
},
"vunique": "bar",
}
var result Embedded
err := Decode(input, &result)
if err != nil {
t.Fatalf("got an err: %s", err.Error())
}
if result.Vstring != "innerfoo" {
t.Errorf("vstring value should be 'innerfoo': %#v", result.Vstring)
}
if result.Vunique != "bar" {
t.Errorf("vunique value should be 'bar': %#v", result.Vunique)
}
}
func TestDecode_EmbeddedPointer(t *testing.T) {
t.Parallel()
input := map[string]interface{}{
"vstring": "foo",
"Basic": map[string]interface{}{
"vstring": "innerfoo",
},
"vunique": "bar",
}
var result EmbeddedPointer
err := Decode(input, &result)
if err != nil {
t.Fatalf("err: %s", err)
}
expected := EmbeddedPointer{
Basic: &Basic{
Vstring: "innerfoo",
},
Vunique: "bar",
}
if !reflect.DeepEqual(result, expected) {
t.Fatalf("bad: %#v", result)
}
}
func TestDecode_EmbeddedSlice(t *testing.T) {
t.Parallel()
input := map[string]interface{}{
"slice_alias": []string{"foo", "bar"},
"vunique": "bar",
}
var result EmbeddedSlice
err := Decode(input, &result)
if err != nil {
t.Fatalf("got an err: %s", err.Error())
}
if !reflect.DeepEqual(result.SliceAlias, SliceAlias([]string{"foo", "bar"})) {
t.Errorf("slice value: %#v", result.SliceAlias)
}
if result.Vunique != "bar" {
t.Errorf("vunique value should be 'bar': %#v", result.Vunique)
}
}
func TestDecode_EmbeddedArray(t *testing.T) {
t.Parallel()
input := map[string]interface{}{
"array_alias": [2]string{"foo", "bar"},
"vunique": "bar",
}
var result EmbeddedArray
err := Decode(input, &result)
if err != nil {
t.Fatalf("got an err: %s", err.Error())
}
if !reflect.DeepEqual(result.ArrayAlias, ArrayAlias([2]string{"foo", "bar"})) {
t.Errorf("array value: %#v", result.ArrayAlias)
}
if result.Vunique != "bar" {
t.Errorf("vunique value should be 'bar': %#v", result.Vunique)
}
}
func TestDecode_decodeSliceWithArray(t *testing.T) {
t.Parallel()
var result []int
input := [1]int{1}
expected := []int{1}
if err := Decode(input, &result); err != nil {
t.Fatalf("got an err: %s", err.Error())
}
if !reflect.DeepEqual(expected, result) {
t.Errorf("wanted %+v, got %+v", expected, result)
}
}
func TestDecode_EmbeddedNoSquash(t *testing.T) {
t.Parallel()
input := map[string]interface{}{
"vstring": "foo",
"vunique": "bar",
}
var result Embedded
err := Decode(input, &result)
if err != nil {
t.Fatalf("got an err: %s", err.Error())
}
if result.Vstring != "" {
t.Errorf("vstring value should be empty: %#v", result.Vstring)
}
if result.Vunique != "bar" {
t.Errorf("vunique value should be 'bar': %#v", result.Vunique)
}
}
func TestDecode_EmbeddedPointerNoSquash(t *testing.T) {
t.Parallel()
input := map[string]interface{}{
"vstring": "foo",
"vunique": "bar",
}
result := EmbeddedPointer{
Basic: &Basic{},
}
err := Decode(input, &result)
if err != nil {
t.Fatalf("err: %s", err)
}
if result.Vstring != "" {
t.Errorf("vstring value should be empty: %#v", result.Vstring)
}
if result.Vunique != "bar" {
t.Errorf("vunique value should be 'bar': %#v", result.Vunique)
}
}
func TestDecode_EmbeddedSquash(t *testing.T) {
t.Parallel()
input := map[string]interface{}{
"vstring": "foo",
"vunique": "bar",
}
var result EmbeddedSquash
err := Decode(input, &result)
if err != nil {
t.Fatalf("got an err: %s", err.Error())
}
if result.Vstring != "foo" {
t.Errorf("vstring value should be 'foo': %#v", result.Vstring)
}
if result.Vunique != "bar" {
t.Errorf("vunique value should be 'bar': %#v", result.Vunique)
}
}
func TestDecodeFrom_EmbeddedSquash(t *testing.T) {
t.Parallel()
var v interface{}
var ok bool
input := EmbeddedSquash{
Basic: Basic{
Vstring: "foo",
},
Vunique: "bar",
}
var result map[string]interface{}
err := Decode(input, &result)
if err != nil {
t.Fatalf("got an err: %s", err.Error())
}
if _, ok = result["Basic"]; ok {
t.Error("basic should not be present in map")
}
v, ok = result["Vstring"]
if !ok {
t.Error("vstring should be present in map")
} else if !reflect.DeepEqual(v, "foo") {
t.Errorf("vstring value should be 'foo': %#v", v)
}
v, ok = result["Vunique"]
if !ok {
t.Error("vunique should be present in map")
} else if !reflect.DeepEqual(v, "bar") {
t.Errorf("vunique value should be 'bar': %#v", v)
}
}
func TestDecode_EmbeddedPointerSquash_FromStructToMap(t *testing.T) {
t.Parallel()
input := EmbeddedPointerSquash{
Basic: &Basic{
Vstring: "foo",
},
Vunique: "bar",
}
var result map[string]interface{}
err := Decode(input, &result)
if err != nil {
t.Fatalf("got an err: %s", err.Error())
}
if result["Vstring"] != "foo" {
t.Errorf("vstring value should be 'foo': %#v", result["Vstring"])
}
if result["Vunique"] != "bar" {
t.Errorf("vunique value should be 'bar': %#v", result["Vunique"])
}
}
func TestDecode_EmbeddedPointerSquash_FromMapToStruct(t *testing.T) {
t.Parallel()
input := map[string]interface{}{
"Vstring": "foo",
"Vunique": "bar",
}
result := EmbeddedPointerSquash{
Basic: &Basic{},
}
err := Decode(input, &result)
if err != nil {
t.Fatalf("got an err: %s", err.Error())
}
if result.Vstring != "foo" {
t.Errorf("vstring value should be 'foo': %#v", result.Vstring)
}
if result.Vunique != "bar" {
t.Errorf("vunique value should be 'bar': %#v", result.Vunique)
}
}
func TestDecode_EmbeddedPointerSquashWithNestedMapstructure_FromStructToMap(t *testing.T) {
t.Parallel()
vTime := time.Now()
input := EmbeddedPointerSquashWithNestedMapstructure{
NestedPointerWithMapstructure: &NestedPointerWithMapstructure{
Vbar: &BasicMapStructure{
Vunique: "bar",
Vtime: &vTime,
},
},
Vunique: "foo",
}
var result map[string]interface{}
err := Decode(input, &result)
if err != nil {
t.Fatalf("got an err: %s", err.Error())
}
expected := map[string]interface{}{
"vbar": map[string]interface{}{
"vunique": "bar",
"time": &vTime,
},
"Vunique": "foo",
}
if !reflect.DeepEqual(result, expected) {
t.Errorf("result should be %#v: got %#v", expected, result)
}
}
func TestDecode_EmbeddedPointerSquashWithNestedMapstructure_FromMapToStruct(t *testing.T) {
t.Parallel()
vTime := time.Now()
input := map[string]interface{}{
"vbar": map[string]interface{}{
"vunique": "bar",
"time": &vTime,
},
"Vunique": "foo",
}
result := EmbeddedPointerSquashWithNestedMapstructure{
NestedPointerWithMapstructure: &NestedPointerWithMapstructure{},
}
err := Decode(input, &result)
if err != nil {
t.Fatalf("got an err: %s", err.Error())
}
expected := EmbeddedPointerSquashWithNestedMapstructure{
NestedPointerWithMapstructure: &NestedPointerWithMapstructure{
Vbar: &BasicMapStructure{
Vunique: "bar",
Vtime: &vTime,
},
},
Vunique: "foo",
}
if !reflect.DeepEqual(result, expected) {
t.Errorf("result should be %#v: got %#v", expected, result)
}
}
func TestDecode_EmbeddedSquashConfig(t *testing.T) {
t.Parallel()
input := map[string]interface{}{
"vstring": "foo",
"vunique": "bar",
"Named": map[string]interface{}{
"vstring": "baz",
},
}
var result EmbeddedAndNamed
config := &DecoderConfig{
Squash: true,
Result: &result,
}
decoder, err := NewDecoder(config)
if err != nil {
t.Fatalf("err: %s", err)
}
err = decoder.Decode(input)
if err != nil {
t.Fatalf("got an err: %s", err)
}
if result.Vstring != "foo" {
t.Errorf("vstring value should be 'foo': %#v", result.Vstring)
}
if result.Vunique != "bar" {
t.Errorf("vunique value should be 'bar': %#v", result.Vunique)
}
if result.Named.Vstring != "baz" {
t.Errorf("Named.vstring value should be 'baz': %#v", result.Named.Vstring)
}
}
func TestDecodeFrom_EmbeddedSquashConfig(t *testing.T) {
t.Parallel()
input := EmbeddedAndNamed{
Basic: Basic{Vstring: "foo"},
Named: Basic{Vstring: "baz"},
Vunique: "bar",
}
result := map[string]interface{}{}
config := &DecoderConfig{
Squash: true,
Result: &result,
}
decoder, err := NewDecoder(config)
if err != nil {
t.Fatalf("got an err: %s", err.Error())
}
err = decoder.Decode(input)
if err != nil {
t.Fatalf("got an err: %s", err.Error())
}
if _, ok := result["Basic"]; ok {
t.Error("basic should not be present in map")
}
v, ok := result["Vstring"]
if !ok {
t.Error("vstring should be present in map")
} else if !reflect.DeepEqual(v, "foo") {
t.Errorf("vstring value should be 'foo': %#v", v)
}
v, ok = result["Vunique"]
if !ok {
t.Error("vunique should be present in map")
} else if !reflect.DeepEqual(v, "bar") {
t.Errorf("vunique value should be 'bar': %#v", v)
}
v, ok = result["Named"]
if !ok {
t.Error("Named should be present in map")
} else {
named := v.(map[string]interface{})
v, ok := named["Vstring"]
if !ok {
t.Error("Named: vstring should be present in map")
} else if !reflect.DeepEqual(v, "baz") {
t.Errorf("Named: vstring should be 'baz': %#v", v)
}
}
}
func TestDecodeFrom_EmbeddedSquashConfig_WithTags(t *testing.T) {
t.Parallel()
var v interface{}
var ok bool
input := EmbeddedSquash{
Basic: Basic{
Vstring: "foo",
},
Vunique: "bar",
}
result := map[string]interface{}{}
config := &DecoderConfig{
Squash: true,
Result: &result,
}
decoder, err := NewDecoder(config)
if err != nil {
t.Fatalf("got an err: %s", err.Error())
}
err = decoder.Decode(input)
if err != nil {
t.Fatalf("got an err: %s", err.Error())
}
if _, ok = result["Basic"]; ok {
t.Error("basic should not be present in map")
}
v, ok = result["Vstring"]
if !ok {
t.Error("vstring should be present in map")
} else if !reflect.DeepEqual(v, "foo") {
t.Errorf("vstring value should be 'foo': %#v", v)
}
v, ok = result["Vunique"]
if !ok {
t.Error("vunique should be present in map")
} else if !reflect.DeepEqual(v, "bar") {
t.Errorf("vunique value should be 'bar': %#v", v)
}
}
func TestDecode_SquashOnNonStructType(t *testing.T) {
t.Parallel()
input := map[string]interface{}{
"InvalidSquashType": 42,
}
var result SquashOnNonStructType
err := Decode(input, &result)
if err == nil {
t.Fatal("unexpected success decoding invalid squash field type")
} else if !strings.Contains(err.Error(), "unsupported type for squash") {
t.Fatalf("unexpected error message for invalid squash field type: %s", err)
}
}
func TestDecode_DecodeHook(t *testing.T) {
t.Parallel()
input := map[string]interface{}{
"vint": "WHAT",
}
decodeHook := func(from reflect.Kind, to reflect.Kind, v interface{}) (interface{}, error) {
if from == reflect.String && to != reflect.String {
return 5, nil
}
return v, nil
}
var result Basic
config := &DecoderConfig{
DecodeHook: decodeHook,
Result: &result,
}