-
Notifications
You must be signed in to change notification settings - Fork 19
/
formam_test.go
1236 lines (1110 loc) · 29.1 KB
/
formam_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 formam_test
import (
"encoding/hex"
"fmt"
"net/url"
"strconv"
"strings"
"testing"
"time"
"github.com/monoculum/formam/v3"
)
type Text string
func (s *Text) UnmarshalText(text []byte) error {
var n Text
n = "the string has changed by UnmarshalText method"
*s = n
return nil
}
type UUID [16]byte
func (u *UUID) UnmarshalText(text []byte) error {
if len(text) != 32 {
return fmt.Errorf("text must be exactly 16 bytes long, got %d bytes", len(text))
}
_, err := hex.Decode(u[:], text)
if err != nil {
return err
}
return nil
}
func (u UUID) String() string {
buf := make([]byte, 32)
hex.Encode(buf[:], u[:])
return string(buf)
}
const unmarshalTextString = "If you see this text, then it's a bug"
type AnonymousID struct {
ID string
}
type Anonymous struct {
AnonymousField string
FieldOverride string
*AnonymousID
}
type FieldString string
type TestStruct struct {
Anonymous
FieldOverride string
// traverse
TraverseStruct struct {
Field1 [][]struct {
Field string
}
Field2 struct {
Field int
}
}
TraverseMapByBracket map[string]map[int]map[uint]map[bool]*string
TraverseMapByPoint map[string]map[int]map[uint]map[bool]string
// slices/arrays
SlicesWithIndex []string
SlicesWithoutIndex []float32
SlicesMultiDimension [][][][]uintptr
ArrayWithIndex [2]interface{}
ArrayWithoutIndex [2]bool
ArrayMultiDimension [2][2]bool
// int
Int int
Int8 int8
Int16 int16
Int32 int32
Int64 int64
// uint
Uint uint
Uint8 uint8
Uint16 uint16
Uint32 uint32
Uint64 uint64
Uintptr uintptr
// bool
Bool bool
// string
String string
// byte
Byte byte
// pointer
Pointer *string
// pointer to struct
PointerToStruct *struct{ Field float64 }
// pointer to map
PointerToMap *map[string]string
// pointer to anonymous struct
PointerToSlice []Anonymous
// map
Map map[string]string
// mp with slice
MapWithSlice map[string][]string
// map with multi dimension slice
MapWithMultiDimensionSlice map[string][][]string
// map with array
MapWithArray map[string][2]int
// map with int key
MapWithIntKey map[int]string
// map with int8 key
MapWithInt8Key map[int8]string
// map with *int64 key
MapWithInt64PtrKey map[*int64]string
// map with uint key
MapWithUintKey map[uint]string
// map with uint key
MapWithUint8Key map[uint8]string
// map with uint key
MapWithUint32PtrKey map[*uint32]string
// map with float key
MapWithFloatKey map[float32]string
// map with boolean key
MapWithBooleanKey map[bool]string
// map with custom key and decode key by unmarshal key
MapWithCustomKey map[UUID]string
// map with custom key pointer and decode key by unmarshal key
MapWithCustomKeyPointer map[*UUID]string
// map with time.Time Key
MapWithStruct1Key map[time.Time]string
// map with url.URL Key
MapWithStruct2Key map[url.URL]string
//MapWithStruct3Key map[struct {ID struct {ID string}]string
// unmarshal text
UnmarshalTextString Text
UnmarshalTextUUID UUID
// tag
Tag string `formam:"tag"`
TagOpt string `formam:"tagopt,x"`
// time
Time time.Time
// url
URL url.URL
// interface
Interface interface{}
// interface with struct as data
InterfaceStruct interface{}
// custom type
CustomType FieldString
// custom type by field
Time1 time.Time
Time2 time.Time
TimeDefault time.Time
}
type InterfaceStruct struct {
ID int
Name string
}
var vals = url.Values{
// anonymous
"AnonymousField": []string{"anonymous field"},
"FieldOverride": []string{"field not override"},
// traverse
"TraverseStruct.Field1[0][2].Field": []string{"traverse over structs is recursive"},
"TraverseStruct.Field2.Field": []string{"2"},
"TraverseMapByBracket[by-bracket][1][2][true]": []string{"traverse over map by bracket is recursive too"},
"TraverseMapByPoint.by-point.1.2.true": []string{"traverse over map by point is recursive too"},
// slices/arrays
"SlicesWithIndex[0]": []string{"slice index 0"},
"SlicesWithIndex[2]": []string{"slice index 2"},
"SlicesWithIndex[4]": []string{"slice index 4"},
"SlicesWithoutIndex": []string{"1.111", "2.222", "3.333"},
"SlicesMultiDimension[0][1][2][3]": []string{"8"},
"ArrayWithIndex[0]": []string{"array index 0"},
"ArrayWithIndex[1]": []string{"array index 1"},
"ArrayWithoutIndex": []string{"true", "true"},
"ArrayMultiDimension[0][0]": []string{"true"},
"ArrayMultiDimension[0][1]": []string{"true"},
"ArrayMultiDimension[1][0]": []string{"true"},
"ArrayMultiDimension[1][1]": []string{"true"},
// int
"Int": []string{"-1"},
"Int8": []string{"-1"},
"Int16": []string{"-1"},
"Int32": []string{"-1"},
"Int64": []string{"-1"},
// uint
"Uint": []string{"1"},
"Uint8": []string{"1"},
"Uint16": []string{"1"},
"Uint32": []string{"1"},
"Uint64": []string{"1"},
"Uintptr": []string{"10"},
// bool
"Bool": []string{"true"},
// string
"String": []string{"string"},
// byte
"Byte": []string{"20"},
// pointer
"Pointer": []string{"20"},
"PointerToStruct.Field": []string{"20"},
"PointerToMap[es]": []string{"20"},
"PointerToSlice[0].ID": []string{"20"},
// map
"Map[by.bracket.with.point]": []string{"by bracket"},
"Map.by_point": []string{"by point"},
"MapWithSlice[slice][0]": []string{"map with slice"},
"MapWithMultiDimensionSlice[slice][0][1]": []string{"map with multidimension slice"},
"MapWithArray[array][0]": []string{"0"},
"MapWithArray[array][1]": []string{"1"},
"MapWithIntKey[-1]": []string{"int key in map"},
"MapWithInt8Key[-1]": []string{"int8 key in map"},
"MapWithInt64PtrKey[-1]": []string{"int64 ptr key in map"},
"MapWithUint8Key[1]": []string{"uint8 ptr key in map"},
"MapWithUint32PtrKey[1]": []string{"uint32 ptr key in map"},
"MapWithUintKey[1]": []string{"uint key in map"},
"MapWithFloatKey[3.14]": []string{"float key in map"},
"MapWithBooleanKey[true]": []string{"bool key in map"},
"MapWithCustomKey[11e5bf2d3e403a8c86740023dffe5350]": []string{"UUID key in map"},
"MapWithCustomKeyPointer[11e5bf2d3e403a8c86740023dffe5350]": []string{"UUID key pointer in map"},
"MapWithStruct1Key[2006-01-02]": []string{"time.Time key in map"},
"MapWithStruct2Key[http://www.monoculum.com]": []string{"url.URL key in map"},
// unmarshal text
"UnmarshalTextString": []string{"If you see this text, then it's a bug"},
"UnmarshalTextUUID": []string{"11e5bf2d3e403a8c86740023dffe5350"},
// tag
"tag": []string{"string placed by tag"},
"tagopt": []string{"string placed by tagopt"},
// time
"Time": []string{"2016-06-12"},
// url
"URL": []string{"http://www.monoculum.com"},
// interface
"Interface": []string{"Germany"},
"InterfaceStruct.ID": []string{"1"},
"InterfaceStruct.Name": []string{"Germany"},
// custom type
"CustomType": []string{"if you see this text, then it's a bug"},
// custom type by field
"Time1": []string{"2001-01-01"},
"Time2": []string{"2001-01-01"},
"TimeDefault": []string{"2001-01-01"},
}
func TestDecodeInStruct(t *testing.T) {
var m TestStruct
m.InterfaceStruct = &InterfaceStruct{}
dec := formam.NewDecoder(nil).RegisterCustomType(func(vals []string) (interface{}, error) {
return FieldString("value changed by custom type"), nil
}, []interface{}{FieldString("")}, nil)
dec.RegisterCustomType(func(vals []string) (interface{}, error) {
return time.Parse("2006-01-02", "2016-01-02")
}, []interface{}{time.Time{}}, []interface{}{&m.Time1})
dec.RegisterCustomType(func(vals []string) (interface{}, error) {
return time.Parse("2006-01-02", "2017-01-02")
}, []interface{}{time.Time{}}, []interface{}{&m.Time2})
dec.RegisterCustomType(func(vals []string) (interface{}, error) {
return time.Parse("2006-01-02", "2018-01-02")
}, []interface{}{time.Time{}}, []interface{}{})
err := dec.Decode(vals, &m)
if err != nil {
t.Error(err)
t.FailNow()
}
// anonymous struct
if m.Anonymous.AnonymousField == "" {
t.Error("AnonymousField is empty")
}
if m.Anonymous.FieldOverride != "" {
t.Error("FieldOverride is full")
}
if m.FieldOverride == "" {
t.Error("FieldOverride is empty")
}
// traverse
// traverse > struct
if len(m.TraverseStruct.Field1) == 0 {
t.Error("TraverseStruct.Field1 is empty")
} else {
if len(m.TraverseStruct.Field1[0]) != 3 {
t.Errorf("TraverseStruct.Field1[0] must has 3 as length but has %v", len(m.TraverseStruct.Field1[0]))
} else {
if m.TraverseStruct.Field1[0][2].Field == "" {
t.Error("TraverseStruct.Field1[0][2].Field is empty")
}
}
}
// traverse > maps
n, ok := m.TraverseMapByBracket["by-bracket"]
if ok {
m, ok := n[1]
if ok {
j, ok := m[2]
if ok {
g, ok := j[true]
if ok {
if *g == "" {
t.Error("the value of TraverseMapByBracket in the last field is empty")
}
} else {
t.Error("the key \"true\" in TraverseMapByBracket not exists")
}
} else {
t.Error("the key \"2\" in TraverseMapByBracket not exists")
}
} else {
t.Error("the key \"1\" in TraverseMapByBracket not exists")
}
} else {
t.Error("the key \"bracket\" in TraverseMapByBracket not exists")
}
u, ok := m.TraverseMapByPoint["by-point"]
if ok {
m, ok := u[1]
if ok {
j, ok := m[2]
if ok {
g, ok := j[true]
if ok {
if g == "" {
t.Error("the value of TraverseMapByPoint in the last field is empty")
}
} else {
t.Error("the key \"true\" in TraverseMapByPoint not exists")
}
} else {
t.Error("the key \"2\" in TraverseMapByPoint not exists")
}
} else {
t.Error("the key \"1\" in TraverseMapByPoint not exists")
}
} else {
t.Error("the key \"by-point\" in TraverseMapByPoint not exists")
}
// slices
if len(m.SlicesWithIndex) != 5 {
t.Error("the length SlicesWithIndex is not 5")
}
if len(m.SlicesWithoutIndex) != 3 {
t.Error("the length SlicesWithoutIndex is not 3")
}
if len(m.SlicesMultiDimension) != 1 {
t.Error("the length SlicesMultiDimension is not 1")
}
if len(m.SlicesMultiDimension[0]) != 2 {
t.Error("the length SlicesMultiDimension[0] is not 2")
}
if len(m.SlicesMultiDimension[0][1]) != 3 {
t.Error("the length SlicesMultiDimension[0] is not 3")
}
// array
if len(m.ArrayWithIndex) != 2 {
t.Error("the length ArrayWithIndex is not 2")
}
if len(m.ArrayWithoutIndex) != 2 {
t.Error("the length ArrayWithoutIndex is not 2")
}
if len(m.ArrayMultiDimension) != 2 {
t.Error("the length ArrayMultiDimension is not 2")
}
if len(m.ArrayMultiDimension[0]) != 2 {
t.Error("the length ArrayMultiDimension[0] is not 2")
}
if len(m.ArrayMultiDimension[1]) != 2 {
t.Error("the length ArrayMultiDimension[1] is not 2")
}
// int
if m.Int != -1 {
t.Error("the length Int is not -1")
}
if m.Int8 != -1 {
t.Error("the length Int8 is not -1")
}
if m.Int16 != -1 {
t.Error("the length Int16 is not -1")
}
if m.Int32 != -1 {
t.Error("the length Int32 is not -1")
}
if m.Int64 != -1 {
t.Error("the length Int64 is not -1")
}
// uint
if m.Uint != 1 {
t.Error("the length Uint is not 1")
}
if m.Uint8 != 1 {
t.Error("the length Uint8 is not 1")
}
if m.Uint16 != 1 {
t.Error("the length Uint16 is not 1")
}
if m.Uint32 != 1 {
t.Error("the length Uint32 is not 1")
}
if m.Uint64 != 1 {
t.Error("the length Uint64 is not 1")
}
if m.Uintptr != 10 {
t.Error("the length Uintptr is not 10")
}
// bool
if !m.Bool {
t.Error("Bool is false")
}
// string
if m.String == "" {
t.Error("String is empty")
}
// byte
if string(m.Byte) == "" {
t.Error("Byte is empty")
}
// pointer
if m.Pointer == nil {
t.Error("Pointer is nil")
} else if *m.Pointer == "" {
t.Error("Pointer is not nil but is empty")
}
if m.PointerToMap == nil {
t.Error("Pointer is nil")
} else if len(*m.PointerToMap) == 0 {
t.Error("PointerToMap is not nil but is empty")
} else {
for k := range *m.PointerToMap {
if (*m.PointerToMap)[k] == "" {
t.Error("PointerToMap[" + k + "] is empty")
}
}
}
if m.PointerToSlice == nil {
t.Error("PointerToSlice is nil")
} else if len(m.PointerToSlice) == 0 {
t.Error("PointerToSlice is not nil but is empty")
} else {
for i := range m.PointerToSlice {
if m.PointerToSlice[i].AnonymousID == nil {
t.Error("PointerToSlice[" + strconv.Itoa(i) + "] is nil")
} else if m.PointerToSlice[i].ID == "" {
t.Error("PointerToSlice[" + strconv.Itoa(i) + "].ID is empty")
}
}
}
// map
f, ok := m.Map["by.bracket.with.point"]
if ok {
if f == "" {
t.Error("The value in key \"by.bracket.with.point\" of Map is empty")
}
} else {
t.Error("The key \"by.bracket.with.point\" in Map not exists")
}
f, ok = m.Map["by_point"]
if ok {
if f == "" {
t.Error("The value in key \"by_point\" of Map is empty")
}
} else {
t.Error("The key \"by_point\" in Map not exists")
}
s, ok := m.MapWithSlice["slice"]
if ok {
if len(s) == 0 {
t.Error("The length of key \"slice\" of MapWithSlice is 0")
} else {
if s[0] == "" {
t.Error("The value of key \"slice\" in MapWithSlice is empty")
}
}
} else {
t.Error("The key \"slice\" in MapWithSlice not exists")
}
a, ok := m.MapWithMultiDimensionSlice["slice"]
if ok {
if len(a) == 0 {
t.Error("The length of key \"slice\" of MapWithSlice is 0")
} else {
if len(a) == 0 {
t.Error("The length of MapWithMultiDimensionSlice[slice] is 0")
} else {
if len(a[0]) != 2 {
t.Error("The length of MapWithMultiDimensionSlice[slice][0] is not 2")
} else {
if a[0][1] == "" {
t.Error("The value in MapWithSlice[slice][0][1] is empty")
}
}
}
}
} else {
t.Error("The key \"slice\" in MapWithSlice not exists")
}
w, ok := m.MapWithArray["array"]
if ok {
if len(w) != 2 {
t.Error("The length of MapWithArray[array] is not 2")
}
} else {
t.Error("The key \"array\" in MapWithArray not exists")
}
q, ok := m.MapWithIntKey[-1]
if ok {
if q == "" {
t.Error("The value of MapWithIntKey[-1] is empty")
}
} else {
t.Error("The key \"-1\" in MapWithIntKey not exists")
}
ñ, ok := m.MapWithInt8Key[-1]
if ok {
if ñ == "" {
t.Error("The value of MapWithInt8Key[-1] is empty")
}
} else {
t.Error("The key \"-1\" in MapWithInt8Key not exists")
}
if len(m.MapWithInt64PtrKey) == 0 {
t.Error("The MapWithInt64PtrKey is empty")
} else {
for _, v := range m.MapWithInt64PtrKey {
if v == "" {
t.Error("The value of MapWithInt64PtrKey[-1] is empty")
}
}
}
y, ok := m.MapWithUintKey[1]
if ok {
if y == "" {
t.Error("The value of MapWithUintKey[1] is empty")
}
} else {
t.Error("The key \"1\" in MapWithUintKey not exists")
}
bb, ok := m.MapWithUint8Key[1]
if ok {
if bb == "" {
t.Error("The value of MapWithUint8Key[1] is empty")
}
} else {
t.Error("The key \"1\" in MapWithUint8Key not exists")
}
if len(m.MapWithUint32PtrKey) == 0 {
t.Error("The MapWithUint32PtrKey is empty")
} else {
for _, v := range m.MapWithUint32PtrKey {
if v == "" {
t.Error("The value of MapWithUint32PtrKey[1] is empty")
}
}
}
o, ok := m.MapWithFloatKey[3.14]
if ok {
if o == "" {
t.Error("The value of MapWithFloatKey[3.14] is empty")
}
} else {
t.Error("The key \"3.14\" in MapWithFloatKey not exists")
}
b, ok := m.MapWithBooleanKey[true]
if ok {
if b == "" {
t.Error("The value of MapWithFloatKey[true] is empty")
}
} else {
t.Error("The key \"true\" in MapWithFloatKey not exists")
}
uuid := UUID{17, 229, 191, 45, 62, 64, 58, 140, 134, 116, 0, 35, 223, 254, 83, 80}
uu, ok := m.MapWithCustomKey[uuid]
if ok {
if uu == "" {
t.Error("The value of MapWithFloatKey[11e5bf2d3e403a8c86740023dffe5350] is empty")
}
} else {
t.Error("The key \"11e5bf2d3e403a8c86740023dffe5350\" in MapWithCustomKey not exists")
}
for k, v := range m.MapWithCustomKeyPointer {
if k.String() != uuid.String() {
t.Error("The key in MapWithCustomKeyPointer is not 11e5bf2d3e403a8c86740023dffe5350")
} else if v == "" {
t.Error("The value of MapWithCustomKeyPointer[11e5bf2d3e403a8c86740023dffe5350] is empty")
}
}
for k, v := range m.MapWithStruct1Key {
if k.IsZero() {
t.Error("The key of MapWithStruct1Key is zero")
}
if v == "" {
t.Error("The value of MapWithStruct1Key[time.Time] is empty")
}
}
for k, v := range m.MapWithStruct2Key {
if k.String() == "" {
t.Error("The key of MapWithStruct2Key is empty")
}
if v == "" {
t.Error("The value of MapWithStruct2Key[ur.URL] is empty")
}
}
// unmarshalText
if m.UnmarshalTextString == unmarshalTextString {
t.Error("The value of UnmarshalTextString is not correct. It should not to contain the text of the const unmarshalTextString")
}
if m.UnmarshalTextUUID.String() != uuid.String() {
t.Errorf("The value of UnmarshalTextUUID is not 11e5bf2d3e403a8c86740023dffe5350 but %s", m.UnmarshalTextUUID.String())
}
// tag
if m.Tag == "" {
t.Error("The value of UnmarshalTextString is empty")
}
// time
if m.Time.IsZero() {
t.Error("The value of Time is zero")
}
// interface
if v, ok := m.Interface.(string); !ok {
t.Error("The Interface is not string")
} else if v == "" {
t.Error("The value of Interface is empty")
}
if v, ok := m.InterfaceStruct.(*InterfaceStruct); !ok {
t.Error("The InterfaceStruct is not InterfaceStruct struct")
} else {
if v.ID == 0 {
t.Error("The value of InterfaceStruct.ID is 0")
}
if v.Name == "" {
t.Error("The value of InterfaceStruct.Name is empty")
}
}
// custom type
if m.CustomType != "value changed by custom type" {
t.Error("The value of CustomType is not correct")
}
if m.Time1.IsZero() {
t.Error("The value of Time1 is not correct")
}
if m.Time2.IsZero() {
t.Error("The value of Time2 is not correct")
}
if m.TimeDefault.IsZero() {
t.Error("The value of TimeDefault is not correct")
}
}
type TestSlice []string
var sliceValues = url.Values{
"[0]": []string{"spanish"},
"[1]": []string{"english"},
}
func TestDecodeInSlice(t *testing.T) {
var t2 TestSlice
err := formam.Decode(sliceValues, &t2)
if err != nil {
t.Error(err)
}
}
func TestIgnoreUnknownKeys(t *testing.T) {
s := struct {
Name string `formam:"Name"`
Map map[string]string
Slice []string
}{}
vals := url.Values{
"Name": []string{"Homer"},
"City": []string{"Springfield"},
"Children.": []string{"Bart", "Lisa"},
"Job[": []string{"Safety inspector"},
}
dec := formam.NewDecoder(&formam.DecoderOptions{
IgnoreUnknownKeys: true,
})
err := dec.Decode(vals, &s)
if err != nil {
t.Error(err)
}
if s.Name != "Homer" {
t.Errorf("Expected Homer got %s", s.Name)
}
}
func TestIgnoreUnknownKeysDoubleEmbedded(t *testing.T) {
type Embedded0 struct {
Foo0 string
Bar0 string
}
type Embedded1 struct {
Foo1 string
Bar1 string
}
s := struct {
Embedded0
Embedded1
Name string `formam:"Name"`
Map map[string]string
Slice []string
}{}
vals := url.Values{
"Name": []string{"Homer"},
"Foo0": []string{"Bar"},
"Bar0": []string{"Foo"},
"Foo1": []string{"Bar"},
"Bar1": []string{"Foo"},
"City": []string{"Springfield"},
"Children.": []string{"Bart", "Lisa"},
"Job[": []string{"Safety inspector"},
}
dec := formam.NewDecoder(&formam.DecoderOptions{
IgnoreUnknownKeys: true,
})
err := dec.Decode(vals, &s)
if err != nil {
t.Error(err)
}
if s.Name != "Homer" {
t.Errorf("Expected Homer got %s", s.Name)
}
if s.Embedded0.Foo0 != "Bar" {
t.Errorf("Expected Bar got %s", s.Embedded0.Foo0)
}
if s.Embedded0.Bar0 != "Foo" {
t.Errorf("Expected Foo got %s", s.Embedded0.Bar0)
}
if s.Embedded1.Foo1 != "Bar" {
t.Errorf("Expected Bar got %s", s.Embedded1.Foo1)
}
if s.Embedded1.Bar1 != "Foo" {
t.Errorf("Expected Foo got %s", s.Embedded1.Bar1)
}
}
func TestIgnoreBracketedKeysError(t *testing.T) {
s := struct {
Name string `formam:"Name"`
}{}
vals := url.Values{
"Name": []string{"Homer"},
"[Wife]": []string{"Marge"},
"His[Wife]": []string{"Marge"},
}
dec := formam.NewDecoder(&formam.DecoderOptions{})
err := dec.Decode(vals, &s)
if err == nil {
t.Error("error is not nil")
}
}
func TestIgnoreBracketedKeysIgnoreError(t *testing.T) {
s := []string{}
vals := url.Values{
"Name": []string{"Homer"},
"[Wife]": []string{"Marge"},
"His[Wife]": []string{"Marge"},
}
dec := formam.NewDecoder(&formam.DecoderOptions{
IgnoreUnknownKeys: true,
})
err := dec.Decode(vals, &s)
if err == nil {
t.Error("error is not nil")
}
}
func TestIgnoreBracketedKeysIgnoreStruct(t *testing.T) {
s := struct {
Name string `formam:"Name"`
}{}
vals := url.Values{
"[Wife]": []string{"Marge"},
"Name": []string{"Homer"},
"His[Wife]": []string{"Marge"},
}
dec := formam.NewDecoder(&formam.DecoderOptions{
IgnoreUnknownKeys: true,
})
err := dec.Decode(vals, &s)
if err != nil {
t.Error(err)
}
if s.Name != "Homer" {
t.Errorf("Expected Homer got %s", s.Name)
}
}
func TestEmptyString(t *testing.T) {
s := struct {
Name string
}{
Name: "Homer",
}
vals := url.Values{
"Name": []string{""},
}
dec := formam.NewDecoder(&formam.DecoderOptions{})
err := dec.Decode(vals, &s)
if err != nil {
t.Error(err)
}
if s.Name == "Homer" {
t.Errorf("Expected empty string got %s", s.Name)
}
}
func TestIgnoredStructTag(t *testing.T) {
s := struct {
Name string `formam:"-"`
Phone string
LastName string `formam:"lastname"`
}{
Name: "Homer",
LastName: "Bouvier",
Phone: "555-555-111",
}
vals := url.Values{
"Name": []string{"Marge"},
"lastname": []string{"Simpson"},
"Phone": []string{"555-333-222"},
}
dec := formam.NewDecoder(&formam.DecoderOptions{})
err := dec.Decode(vals, &s)
if err != nil {
t.Error(err)
}
if s.Name != "Homer" {
t.Errorf("Expected Homer got %s", s.Name)
}
if s.LastName != "Simpson" {
t.Errorf("Expected LastName is Simpson but got %s", s.LastName)
}
if s.Phone != "555-333-222" {
t.Errorf("Expected new phone number '555-333-222' but got %s", s.Phone)
}
}
// Test for #7
func TestPanic(t *testing.T) {
s := struct {
Foo []string `formam:"foo"`
}{}
vals := url.Values{
"foo[]": []string{"5", "6"},
}
dec := formam.NewDecoder(&formam.DecoderOptions{})
err := dec.Decode(vals, &s)
if err != nil {
t.Error(err)
}
if len(s.Foo) != 2 {
t.Fatalf("len(s.Foo) is %d", len(s.Foo))
}
if s.Foo[0] != "5" {
t.Errorf("s.Foo[0] is %#v", s.Foo[0])
}
if s.Foo[1] != "6" {
t.Errorf("s.Foo[1] is %#v", s.Foo[1])
}
}
// #25
func TestOverflow(t *testing.T) {
s := struct {
N uint8
}{}
vals := url.Values{
"N": []string{"300"},
}
dec := formam.NewDecoder(&formam.DecoderOptions{})
err := dec.Decode(vals, &s)
if err == nil {
t.Fatalf("error is nil")
}
if s.N != 0 {
t.Fatalf("s.N is %d", s.N)
}
fErr := err.(*formam.Error)
if fErr.Code() != formam.ErrCodeRange {
t.Fatalf("error code is %d", fErr.Code())
}
}
// #28
func TestArrayIgnore(t *testing.T) {
type Users []struct {
Email string `form:"Email"`
}
vals := url.Values{
"[0].Email": {"[email protected]"},
"[1].Email": {"[email protected]"},
"authenticity_token": {"xxx"},
}
var u Users
dec := formam.NewDecoder(&formam.DecoderOptions{
IgnoreUnknownKeys: true,
})
err := dec.Decode(vals, &u)
if err != nil {
t.Fatal(err)
}
out := fmt.Sprintf("%v", u)
want := `[{[email protected]} {[email protected]}]`
if out != want {
t.Fatalf("\nout: %s\nwant: %s", out, want)
}
}
// #31
func TestArrayLength(t *testing.T) {
tests := []struct {
arrayLen int
maxSize int
wantError string
}{
{15000, 0, ""},
{15999, 0, ""},
{16000, 0, "array size 16001 is longer than MaxSize 16000"},
{8, 8, "array size 9 is longer than MaxSize 8"},
{26000, -1, ""},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
type Malicious []struct {
X string
}