-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathdecode_test.go
1512 lines (1458 loc) · 37.4 KB
/
decode_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 toml
import (
"errors"
"fmt"
"io/ioutil"
"math"
"math/big"
"path/filepath"
"reflect"
"testing"
"time"
"github.com/kylelemons/godebug/pretty"
)
func loadTestData(file string) []byte {
f := filepath.Join("testdata", file)
data, err := ioutil.ReadFile(f)
if err != nil {
panic(err)
}
return data
}
func mustTime(tm time.Time, err error) time.Time {
if err != nil {
panic(err)
}
return tm
}
type Name struct {
First string
Last string
}
type Inline struct {
Name Name
Point map[string]int
}
type Subtable struct {
Key string
}
type Table struct {
Key string
Subtable Subtable
Inline Inline
}
type W struct {
}
type Z struct {
W W
}
type Y struct {
Z Z
}
type X struct {
Y Y
}
type Basic struct {
Basic string
}
type Continued struct {
Key1 string
Key2 string
Key3 string
}
type Multiline struct {
Key1 string
Key2 string
Key3 string
Continued Continued
}
type LiteralMultiline struct {
Regex2 string
Lines string
}
type Literal struct {
Winpath string
Winpath2 string
Quoted string
Regex string
Multiline LiteralMultiline
}
type String struct {
Basic Basic
Multiline Multiline
Literal Literal
}
type IntegerUnderscores struct {
Key1 int
Key2 int
Key3 int
}
type Integer struct {
Key1 int
Key2 int
Key3 int
Key4 int
Underscores IntegerUnderscores
}
type Fractional struct {
Key1 float64
Key2 float64
Key3 float64
}
type Exponent struct {
Key1 float64
Key2 float64
Key3 float64
}
type Both struct {
Key float64
}
type FloatUnderscores struct {
Key1 float64
Key2 float64
}
type Float struct {
Fractional Fractional
Exponent Exponent
Both Both
Underscores FloatUnderscores
}
type Boolean struct {
True bool
False bool
}
type Datetime struct {
Key1 time.Time
Key2 time.Time
Key3 time.Time
}
type Array struct {
Key1 []int
Key2 []string
Key3 [][]int
Key4 [][]interface{}
Key5 []int
Key6 []int
}
type Product struct {
Name string `toml:",omitempty"`
Sku int64 `toml:",omitempty"`
Color string `toml:",omitempty"`
}
type Physical struct {
Color string
Shape string
}
type Variety struct {
Name string
}
type Fruit struct {
Name string
Physical Physical
Variety []Variety
}
type testStruct struct {
Table Table
X X
String String
Integer Integer
Float Float
Boolean Boolean
Datetime Datetime
Array Array
Products []Product
Fruit []Fruit
}
func theTestStruct() *testStruct {
return &testStruct{
Table: Table{
Key: "value",
Subtable: Subtable{
Key: "another value",
},
Inline: Inline{
Name: Name{
First: "Tom",
Last: "Preston-Werner",
},
Point: map[string]int{
"x": 1,
"y": 2,
},
},
},
X: X{},
String: String{
Basic: Basic{
Basic: "I'm a string. \"You can quote me\". Name\tJos\u00E9\nLocation\tSF.",
},
Multiline: Multiline{
Key1: "One\nTwo",
Key2: "One\nTwo",
Key3: "One\nTwo",
Continued: Continued{
Key1: "The quick brown fox jumps over the lazy dog.",
Key2: "The quick brown fox jumps over the lazy dog.",
Key3: "The quick brown fox jumps over the lazy dog.",
},
},
Literal: Literal{
Winpath: `C:\Users\nodejs\templates`,
Winpath2: `\\ServerX\admin$\system32\`,
Quoted: `Tom "Dubs" Preston-Werner`,
Regex: `<\i\c*\s*>`,
Multiline: LiteralMultiline{
Regex2: `I [dw]on't need \d{2} apples`,
Lines: "The first newline is\ntrimmed in raw strings.\n All other whitespace\n is preserved.\n",
},
},
},
Integer: Integer{
Key1: 99,
Key2: 42,
Key3: 0,
Key4: -17,
Underscores: IntegerUnderscores{
Key1: 1000,
Key2: 5349221,
Key3: 12345,
},
},
Float: Float{
Fractional: Fractional{
Key1: 1.0,
Key2: 3.1415,
Key3: -0.01,
},
Exponent: Exponent{
Key1: 5e22,
Key2: 1e6,
Key3: -2e-2,
},
Both: Both{
Key: 6.626e-34,
},
Underscores: FloatUnderscores{
Key1: 9224617.445991228313,
Key2: 1e100,
},
},
Boolean: Boolean{
True: true,
False: false,
},
Datetime: Datetime{
Key1: mustTime(time.Parse(time.RFC3339Nano, "1979-05-27T07:32:00Z")),
Key2: mustTime(time.Parse(time.RFC3339Nano, "1979-05-27T00:32:00-07:00")),
Key3: mustTime(time.Parse(time.RFC3339Nano, "1979-05-27T00:32:00.999999-07:00")),
},
Array: Array{
Key1: []int{1, 2, 3},
Key2: []string{"red", "yellow", "green"},
Key3: [][]int{{1, 2}, {3, 4, 5}},
Key4: [][]interface{}{{int64(1), int64(2)}, {"a", "b", "c"}},
Key5: []int{1, 2, 3},
Key6: []int{1, 2},
},
Products: []Product{
{Name: "Hammer", Sku: 738594937},
{},
{Name: "Nail", Sku: 284758393, Color: "gray"},
},
Fruit: []Fruit{
{
Name: "apple",
Physical: Physical{
Color: "red",
Shape: "round",
},
Variety: []Variety{
{Name: "red delicious"},
{Name: "granny smith"},
},
},
{
Name: "banana",
Variety: []Variety{
{Name: "plantain"},
},
},
},
}
}
func TestUnmarshal(t *testing.T) {
testUnmarshal(t, []testcase{
{
data: string(loadTestData("test.toml")),
expect: theTestStruct(),
},
})
}
type testcase struct {
data string
err error
expect interface{}
}
func testUnmarshal(t *testing.T, testcases []testcase) {
for _, test := range testcases {
// Create a test value of the same type as expect.
typ := reflect.TypeOf(test.expect)
var val interface{}
if typ.Kind() == reflect.Map {
val = reflect.MakeMap(typ).Interface()
} else if typ.Kind() == reflect.Ptr {
val = reflect.New(typ.Elem()).Interface()
} else {
panic("invalid 'expect' type " + typ.String())
}
err := Unmarshal([]byte(test.data), val)
if !reflect.DeepEqual(err, test.err) {
t.Errorf("Error mismatch for input:\n%s\ngot: %+v\nwant: %+v", test.data, err, test.err)
}
if err == nil && !reflect.DeepEqual(val, test.expect) {
t.Errorf("Unmarshal value mismatch for input:\n%s\ndiff:\n%s", test.data, pretty.Compare(val, test.expect))
}
}
}
func TestUnmarshal_WithString(t *testing.T) {
type testStruct struct {
Str string
Key1 string
Key2 string
Key3 string
Winpath string
Winpath2 string
Quoted string
Regex string
Regex2 string
Lines string
}
testUnmarshal(t, []testcase{
{
data: `str = "I'm a string. \"You can quote me\". Name\tJos\u00E9\nLocation\tSF."`,
expect: &testStruct{
Str: "I'm a string. \"You can quote me\". Name\tJos\u00E9\nLocation\tSF.",
},
},
{
data: string(loadTestData("unmarshal-string-1.toml")),
expect: &testStruct{Key1: "One\nTwo", Key2: "One\nTwo", Key3: "One\nTwo"},
},
{
data: string(loadTestData("unmarshal-string-2.toml")),
expect: &testStruct{
Key1: "The quick brown fox jumps over the lazy dog.",
Key2: "The quick brown fox jumps over the lazy dog.",
Key3: "The quick brown fox jumps over the lazy dog.",
},
},
{
data: string(loadTestData("unmarshal-string-3.toml")),
expect: &testStruct{
Winpath: `C:\Users\nodejs\templates`,
Winpath2: `\\ServerX\admin$\system32\`,
Quoted: `Tom "Dubs" Preston-Werner`,
Regex: `<\i\c*\s*>`,
},
},
{
data: string(loadTestData("unmarshal-string-4.toml")),
expect: &testStruct{
Regex2: `I [dw]on't need \d{2} apples`,
Lines: "The first newline is\ntrimmed in raw strings.\n All other whitespace\n is preserved.\n",
},
},
{
data: string(loadTestData("unmarshal-string-5.toml")),
expect: &testStruct{
Key1: `I dare to say "this is valid TOML", and I'm not joking`,
Key2: `I dare to say 'this is valid TOML', and I'm not joking`,
Key3: `I dare to say 'this is valid TOML', and I'm not joking`,
},
},
{
data: string(loadTestData("unmarshal-string-6.toml")),
expect: &testStruct{
Key1: "\t<tab",
Key2: "\t<tab",
},
},
})
}
func TestUnmarshal_WithInteger(t *testing.T) {
type testStruct struct {
Intval int64
}
testUnmarshal(t, []testcase{
{`intval = 0`, nil, &testStruct{0}},
{`intval = +0`, nil, &testStruct{0}},
{`intval = -0`, nil, &testStruct{-0}},
{`intval = 1`, nil, &testStruct{1}},
{`intval = +1`, nil, &testStruct{1}},
{`intval = -1`, nil, &testStruct{-1}},
{`intval = 10`, nil, &testStruct{10}},
{`intval = 777`, nil, &testStruct{777}},
{`intval = 2147483647`, nil, &testStruct{2147483647}},
{`intval = 2147483648`, nil, &testStruct{2147483648}},
{`intval = +2147483648`, nil, &testStruct{2147483648}},
{`intval = -2147483648`, nil, &testStruct{-2147483648}},
{`intval = -2147483649`, nil, &testStruct{-2147483649}},
{`intval = 9223372036854775807`, nil, &testStruct{9223372036854775807}},
{`intval = +9223372036854775807`, nil, &testStruct{9223372036854775807}},
{`intval = -9223372036854775808`, nil, &testStruct{-9223372036854775808}},
{`intval = 1_000`, nil, &testStruct{1000}},
{`intval = 5_349_221`, nil, &testStruct{5349221}},
{`intval = 1_2_3_4_5`, nil, &testStruct{12345}},
// hex
{`intval = 0x0`, nil, &testStruct{0}},
{`intval = 0x010203`, nil, &testStruct{0x010203}},
// octal
{`intval = 0o0`, nil, &testStruct{0}},
{`intval = 0o76543210`, nil, &testStruct{0o76543210}},
// binary
{`intval = 0b0`, nil, &testStruct{0}},
{`intval = 0b1`, nil, &testStruct{1}},
{`intval = 0b01100110`, nil, &testStruct{102}},
{`intval = 0b011_00110`, nil, &testStruct{102}},
// invalid _
{`intval = _1_000`, lineError(1, errParse), &testStruct{}},
{`intval = 1_000_`, lineError(1, errParse), &testStruct{}},
{`intval = 0x_01`, lineError(1, errParse), &testStruct{}},
{`intval = 0x01_`, lineError(1, errParse), &testStruct{}},
{`intval = 0o_01`, lineError(1, errParse), &testStruct{}},
{`intval = 0o01_`, lineError(1, errParse), &testStruct{}},
{`intval = 0b_01`, lineError(1, errParse), &testStruct{}},
{`intval = 0b01_`, lineError(1, errParse), &testStruct{}},
// sign unsupported for non-decimal ints
{`intval = +0x01`, lineError(1, errParse), &testStruct{}},
{`intval = +0o01`, lineError(1, errParse), &testStruct{}},
{`intval = +0b01`, lineError(1, errParse), &testStruct{}},
{`intval = -0x01`, lineError(1, errParse), &testStruct{}},
{`intval = -0o0`, lineError(1, errParse), &testStruct{}},
{`intval = -0b011_00110`, lineError(1, errParse), &testStruct{}},
// overflow
{
data: `intval = 9223372036854775808`,
err: lineErrorField(1, "toml.testStruct.Intval", &overflowError{reflect.Int64, "9223372036854775808"}),
expect: &testStruct{},
},
{
data: `intval = +9223372036854775808`,
err: lineErrorField(1, "toml.testStruct.Intval", &overflowError{reflect.Int64, "+9223372036854775808"}),
expect: &testStruct{},
},
{
data: `intval = -9223372036854775809`,
err: lineErrorField(1, "toml.testStruct.Intval", &overflowError{reflect.Int64, "-9223372036854775809"}),
expect: &testStruct{},
},
})
}
func TestUnmarshal_WithUint(t *testing.T) {
type testStruct struct {
U64 uint64
U32 uint32
U16 uint16
U8 uint8
}
testUnmarshal(t, []testcase{
{`u64 = 12`, nil, &testStruct{U64: 12}},
{`u32 = 12`, nil, &testStruct{U32: 12}},
{`u16 = 12`, nil, &testStruct{U16: 12}},
{`u8 = 12`, nil, &testStruct{U8: 12}},
// error when negative
{
data: `u64 = -12`,
err: lineErrorField(1, "toml.testStruct.U64", &unmarshalTypeError{"integer < 0", "", reflect.TypeOf(uint64(0))}),
expect: &testStruct{},
},
// overflow
{
data: `u8 = 256`,
err: lineErrorField(1, "toml.testStruct.U8", &overflowError{reflect.Uint8, "256"}),
expect: &testStruct{},
},
})
}
func TestUnmarshal_WithFloat(t *testing.T) {
type testStruct struct {
Floatval float64
}
testUnmarshal(t, []testcase{
{`floatval = inf`, nil, &testStruct{math.Inf(1)}},
{`floatval = +inf`, nil, &testStruct{math.Inf(1)}},
{`floatval = -inf`, nil, &testStruct{math.Inf(-1)}},
{`floatval = 0.0`, nil, &testStruct{0.0}},
{`floatval = +0.0`, nil, &testStruct{0.0}},
{`floatval = -0.0`, nil, &testStruct{0.0}},
{`floatval = 0.1`, nil, &testStruct{0.1}},
{`floatval = +0.1`, nil, &testStruct{0.1}},
{`floatval = -0.1`, nil, &testStruct{-0.1}},
{`floatval = 0.2`, nil, &testStruct{0.2}},
{`floatval = +0.2`, nil, &testStruct{0.2}},
{`floatval = -0.2`, nil, &testStruct{-0.2}},
{`floatval = 1.0`, nil, &testStruct{1.0}},
{`floatval = +1.0`, nil, &testStruct{1.0}},
{`floatval = -1.0`, nil, &testStruct{-1.0}},
{`floatval = 1.1`, nil, &testStruct{1.1}},
{`floatval = +1.1`, nil, &testStruct{1.1}},
{`floatval = -1.1`, nil, &testStruct{-1.1}},
{`floatval = 3.1415`, nil, &testStruct{3.1415}},
{`floatval = +3.1415`, nil, &testStruct{3.1415}},
{`floatval = -3.1415`, nil, &testStruct{-3.1415}},
{`floatval = 10.2e5`, nil, &testStruct{10.2e5}},
{`floatval = +10.2e5`, nil, &testStruct{10.2e5}},
{`floatval = -10.2e5`, nil, &testStruct{-10.2e5}},
{`floatval = 10.2E5`, nil, &testStruct{10.2e5}},
{`floatval = +10.2E5`, nil, &testStruct{10.2e5}},
{`floatval = -10.2E5`, nil, &testStruct{-10.2e5}},
{`floatval = 5e+22`, nil, &testStruct{5e+22}},
{`floatval = 1e6`, nil, &testStruct{1e6}},
{`floatval = -2E-2`, nil, &testStruct{-2e-2}},
{`floatval = 6.626e-34`, nil, &testStruct{6.626e-34}},
{`floatval = 9_224_617.445_991_228_313`, nil, &testStruct{9224617.445991228313}},
{`floatval = 1e1_00`, nil, &testStruct{1e100}},
{`floatval = 1e02`, nil, &testStruct{1e2}},
// invalid _
{`floatval = _1e1_00`, lineError(1, errParse), &testStruct{}},
{`floatval = 1e1_00_`, lineError(1, errParse), &testStruct{}},
// invalid encodings from spec
{`floatval = .7`, lineError(1, errParse), &testStruct{}},
{`floatval = 7.`, lineError(1, errParse), &testStruct{}},
{`floatval = 3.e+20`, lineError(1, errParse), &testStruct{}},
// non-decimal base unsupported
{`floatval = 0xff.0`, lineError(1, errParse), &testStruct{}},
{`floatval = 0o71.0`, lineError(1, errParse), &testStruct{}},
{`floatval = 0b01.0`, lineError(1, errParse), &testStruct{}},
})
}
func TestUnmarshal_FloatNaN(t *testing.T) {
tests := []string{"nan", "+nan", "-nan"}
for _, input := range tests {
var dec struct {
Floatval float64
}
if err := Unmarshal([]byte(`floatval = `+input), &dec); err != nil {
t.Fatalf("input %q: unexpected error %q", input, err)
}
if !math.IsNaN(dec.Floatval) {
t.Fatalf("unmarshal result for %q is not NaN: %v", input, dec.Floatval)
}
}
}
func TestUnmarshal_WithBoolean(t *testing.T) {
type testStruct struct {
Boolval bool
}
testUnmarshal(t, []testcase{
{`boolval = true`, nil, &testStruct{true}},
{`boolval = false`, nil, &testStruct{false}},
})
}
func TestUnmarshal_WithDatetime(t *testing.T) {
type testStruct struct {
Datetimeval time.Time
}
testUnmarshal(t, []testcase{
{`datetimeval = 1979-05-27T07:32:00Z`, nil, &testStruct{
mustTime(time.Parse(time.RFC3339Nano, "1979-05-27T07:32:00Z")),
}},
{`datetimeval = 2014-09-13T12:37:39Z`, nil, &testStruct{
mustTime(time.Parse(time.RFC3339Nano, "2014-09-13T12:37:39Z")),
}},
{`datetimeval = 1979-05-27T00:32:00-07:00`, nil, &testStruct{
mustTime(time.Parse(time.RFC3339Nano, "1979-05-27T00:32:00-07:00")),
}},
{`datetimeval = 1979-05-27T00:32:00`, nil, &testStruct{
mustTime(time.Parse(time.RFC3339Nano, "1979-05-27T00:32:00Z")),
}},
{`datetimeval = 1979-05-27 00:32:00`, nil, &testStruct{
mustTime(time.Parse(time.RFC3339Nano, "1979-05-27T00:32:00Z")),
}},
{`datetimeval = 1979-05-27T00:32:00.999999-07:00`, nil, &testStruct{
mustTime(time.Parse(time.RFC3339Nano, "1979-05-27T00:32:00.999999-07:00")),
}},
{`datetimeval = 1979-05-27 00:32:00.999999-07:00`, nil, &testStruct{
mustTime(time.Parse(time.RFC3339Nano, "1979-05-27T00:32:00.999999-07:00")),
}},
{`datetimeval = 1979-05-27`, nil, &testStruct{
mustTime(time.Parse(time.RFC3339, "1979-05-27T00:00:00Z")),
}},
{`datetimeval = 07:32:00`, nil, &testStruct{
mustTime(time.Parse(time.RFC3339, "0000-01-01T07:32:00Z")),
}},
{`datetimeval = 00:32:00.999999`, nil, &testStruct{
mustTime(time.Parse(time.RFC3339Nano, "0000-01-01T00:32:00.999999Z")),
}},
})
}
func TestUnmarshal_WithArray(t *testing.T) {
type arrays struct {
Ints []int
Strings []string
Anys []interface{}
}
testUnmarshal(t, []testcase{
{`ints = []`, nil, &arrays{Ints: []int{}}},
{`ints = [ 1 ]`, nil, &arrays{Ints: []int{1}}},
{`ints = [ 1, 2, 3 ]`, nil, &arrays{Ints: []int{1, 2, 3}}},
{`ints = [ 1, 2, 3, ]`, nil, &arrays{Ints: []int{1, 2, 3}}},
{`strings = ["red", "yellow", "green"]`, nil, &arrays{Strings: []string{"red", "yellow", "green"}}},
{
data: `strings = [ "all", 'strings', """are the same""", '''type''']`,
expect: &arrays{Strings: []string{"all", "strings", "are the same", "type"}},
},
{`arrayval = [[1,2],[3,4,5]]`, nil, &struct{ Arrayval [][]int }{
[][]int{
{1, 2},
{3, 4, 5},
},
}},
{`arrayval = [ [ 1, 2 ], ["a", "b", "c"] ] # this is ok`, nil,
&struct{ Arrayval [][]interface{} }{
[][]interface{}{
{int64(1), int64(2)},
{"a", "b", "c"},
},
}},
{`arrayval = [ [ 1, 2 ], [ [3, 4], [5, 6] ] ] # this is ok`, nil,
&struct{ Arrayval [][]interface{} }{
[][]interface{}{
{int64(1), int64(2)},
{
[]interface{}{int64(3), int64(4)},
[]interface{}{int64(5), int64(6)},
},
},
}},
{`arrayval = [ [ 1, 2 ], [ [3, 4], [5, 6], [7, 8] ] ] # this is ok`, nil,
&struct{ Arrayval [][]interface{} }{
[][]interface{}{
{int64(1), int64(2)},
{
[]interface{}{int64(3), int64(4)},
[]interface{}{int64(5), int64(6)},
[]interface{}{int64(7), int64(8)},
},
},
}},
{`arrayval = [ [[ 1, 2 ]], [3, 4], [5, 6] ] # this is ok`, nil,
&struct{ Arrayval [][]interface{} }{
[][]interface{}{
{
[]interface{}{int64(1), int64(2)},
},
{int64(3), int64(4)},
{int64(5), int64(6)},
},
}},
// heterogenous arrays
{
data: `anys = [1, 2.0]`,
expect: &arrays{Anys: []interface{}{int64(1), float64(2.0)}},
},
{
data: string(loadTestData("unmarshal-array-multitype.toml")),
expect: &arrays{
Anys: []interface{}{
"Foo Bar <[email protected]>",
map[string]interface{}{
"name": "Baz Qux",
"email": "[email protected]",
"url": "https://example.com/bazqux",
},
},
},
},
// // whitespace + comments
{string(loadTestData("unmarshal-array-1.toml")), nil, &arrays{Ints: []int{1, 2, 3}}},
{string(loadTestData("unmarshal-array-2.toml")), nil, &arrays{Ints: []int{1, 2, 3}}},
{string(loadTestData("unmarshal-array-3.toml")), nil, &arrays{Ints: []int{1, 2, 3}}},
{string(loadTestData("unmarshal-array-4.toml")), nil, &arrays{Ints: []int{1, 2, 3}}},
{string(loadTestData("unmarshal-array-5.toml")), nil, &arrays{Ints: []int{1, 2, 3}}},
{string(loadTestData("unmarshal-array-6.toml")), nil, &arrays{Ints: []int{1, 2, 3}}},
// parse errors
{`ints = [ , ]`, lineError(1, errParse), &arrays{}},
{`ints = [ , 1 ]`, lineError(1, errParse), &arrays{}},
{`ints = [ 1 2 ]`, lineError(1, errParse), &arrays{}},
{`ints = [ 1 , , 2 ]`, lineError(1, errParse), &arrays{}},
})
}
func TestUnmarshal_WithTable(t *testing.T) {
type W struct{}
type Z struct {
W W
}
type Y struct {
Z Z
}
type X struct {
Y Y
}
type A struct {
D int
B struct {
C int
}
}
type testStruct struct {
Table struct {
Key string
}
Dog struct {
Tater struct{}
}
X X
A A
}
type testIgnoredFieldStruct struct {
Ignored string `toml:"-"`
}
type testNamedFieldStruct struct {
Named string `toml:"Named_Field"`
}
type testQuotedKeyStruct struct {
Dog struct {
TaterMan struct {
Type string
} `toml:"tater.man"`
}
}
type testQuotedKeyWithWhitespaceStruct struct {
Dog struct {
TaterMan struct {
Type string
} `toml:"tater . man"`
}
}
type testStructWithMap struct {
Servers map[string]struct {
IP string
DC string
}
}
type withTableArray struct {
Tabarray []map[string]string
}
testUnmarshal(t, []testcase{
{`[table]`, nil, &testStruct{}},
{`[table]
key = "value"`, nil,
&testStruct{
Table: struct {
Key string
}{
Key: "value",
},
}},
{`[dog.tater]`, nil,
&testStruct{
Dog: struct {
Tater struct{}
}{
Tater: struct{}{},
},
}},
{`[dog."tater.man"]
type = "pug"`, nil,
&testQuotedKeyStruct{
Dog: struct {
TaterMan struct {
Type string
} `toml:"tater.man"`
}{
TaterMan: struct {
Type string
}{
Type: "pug",
},
},
}},
{`[dog."tater . man"]
type = "pug"`, nil,
&testQuotedKeyWithWhitespaceStruct{
Dog: struct {
TaterMan struct {
Type string
} `toml:"tater . man"`
}{
TaterMan: struct {
Type string
}{
Type: "pug",
},
},
}},
{`[x.y.z.w] # for this to work`, nil,
&testStruct{
X: X{},
}},
{`[ x . y . z . w ]`, nil,
&testStruct{
X: X{},
}},
{`[ x . "y" . z . "w" ]`, nil,
&testStruct{
X: X{},
}},
{`table = {}`, nil, &testStruct{}},
{`table = { key = "value" }`, nil, &testStruct{
Table: struct {
Key string
}{
Key: "value",
},
}},
{`x = { y = { "z" = { w = {} } } }`, nil, &testStruct{X: X{}}},
{`[a.b]
c = 1
[a]
d = 2`, nil,
&testStruct{
A: struct {
D int
B struct {
C int
}
}{
D: 2,
B: struct {
C int
}{
C: 1,
},
},
}},
{
data: `Named_Field = "value"`,
expect: &testNamedFieldStruct{Named: "value"},
},
{
data: string(loadTestData("unmarshal-table-withmap.toml")),
expect: &testStructWithMap{
Servers: map[string]struct {
IP string
DC string
}{
"alpha": {IP: "10.0.0.1", DC: "eqdc10"},
"beta": {IP: "10.0.0.2", DC: "eqdc10"},
},
},
},
{
data: string(loadTestData("unmarshal-table-withinline.toml")),
expect: map[string]withTableArray{
"tab1": {Tabarray: []map[string]string{{"key": "1"}}},
"tab2": {Tabarray: []map[string]string{{"key": "2"}}},
},
},
// errors
{
data: string(loadTestData("unmarshal-table-conflict-1.toml")),
err: lineError(7, fmt.Errorf("table `a' is in conflict with table in line 4")),
expect: &testStruct{},
},
{
data: string(loadTestData("unmarshal-table-conflict-2.toml")),
err: lineError(7, fmt.Errorf("table `a.b' is in conflict with line 5")),
expect: &testStruct{},
},
{
data: string(loadTestData("unmarshal-table-conflict-3.toml")),
err: lineError(8, fmt.Errorf("key `b' is in conflict with table in line 4")),
expect: &testStruct{},
},
{
data: string(loadTestData("unmarshal-table-newline-req-1.toml")),
err: lineError(3, errNewlineRequired),
expect: &testStruct{},
},
{
data: string(loadTestData("unmarshal-table-newline-req-2.toml")),
err: lineError(4, errNewlineRequired),
expect: &testStruct{},
},
{
data: string(loadTestData("unmarshal-table-inline-comma-invalid-1.toml")),
err: lineError(3, errInlineTableCommaAtEnd),
expect: &testStruct{},
},
{
data: string(loadTestData("unmarshal-table-inline-comma-invalid-2.toml")),
err: lineError(3, errInlineTableCommaRequired),
expect: &testStruct{},
},
{`[]`, lineError(1, errParse), &testStruct{}},
{`[a.]`, lineError(1, errParse), &testStruct{}},
{`[a..b]`, lineError(1, errParse), &testStruct{}},
{`[.b]`, lineError(1, errParse), &testStruct{}},
{`[.]`, lineError(1, errParse), &testStruct{}},
{` = "no key name" # not allowed`, lineError(1, errParse), &testStruct{}},
{
data: `ignored = "value"`,
err: lineError(1, fmt.Errorf("field corresponding to `ignored' in toml.testIgnoredFieldStruct cannot be set through TOML")),
expect: &testIgnoredFieldStruct{},
},
{
data: `"-" = "value"`,
err: lineError(1, fmt.Errorf("field corresponding to `-' is not defined in toml.testIgnoredFieldStruct")),
expect: &testIgnoredFieldStruct{},
},
{
data: `named = "value"`,
err: lineError(1, fmt.Errorf("field corresponding to `named' is not defined in toml.testNamedFieldStruct")),
expect: &testNamedFieldStruct{},
},
{
data: `
[a]
d = 2
y = 3
`,
err: lineError(4, fmt.Errorf("field corresponding to `y' is not defined in toml.A")),
expect: &testStruct{},
},
})
}
func TestUnmarshal_WithEmbeddedStruct(t *testing.T) {
type TestEmbStructA struct {
A string
}
testUnmarshal(t, []testcase{
{
data: `a = "value"`,
expect: &struct {
TestEmbStructA
A string
}{
A: "value",
},
},
{
data: `a = "value"`,
expect: &struct {
A string
TestEmbStructA
}{
A: "value",
},
},
})
}
func TestUnmarshal_WithArrayTable(t *testing.T) {
type Product struct {
Name string
SKU int64
Color string
}
type Physical struct {
Color string
Shape string
}
type Variety struct {
Name string
}
type Fruit struct {
Name string
Physical Physical
Variety []Variety
}
type testStruct struct {
Products []Product
Fruit []Fruit
}
type testStructWithMap struct {
Fruit []map[string][]struct {
Name string
}
}
testUnmarshal(t, []testcase{
{
data: string(loadTestData("unmarshal-arraytable.toml")),
expect: &testStruct{
Products: []Product{
{Name: "Hammer", SKU: 738594937},
{},