-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder_test.go
906 lines (829 loc) · 22.8 KB
/
encoder_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
package csvlib
import (
"bytes"
"encoding/csv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tiendc/gofn"
)
func doEncode(v any, options ...EncodeOption) ([]byte, error) {
var buf bytes.Buffer
w := csv.NewWriter(&buf)
encoder := NewEncoder(w, options...)
if err := encoder.Encode(v); err != nil {
return nil, err
}
w.Flush()
if err := w.Error(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func makeEncoder(options ...EncodeOption) (*Encoder, *csv.Writer, *bytes.Buffer) {
var buf bytes.Buffer
w := csv.NewWriter(&buf)
e := NewEncoder(w, options...)
return e, w, &buf
}
func Test_Encode_configOption(t *testing.T) {
type Item struct {
ColX bool `csv:",optional"`
ColY bool
Col1 int16 `csv:"col1"`
Col2 StrType `csv:"col2"`
}
t.Run("#1: column option not found", func(t *testing.T) {
v := []Item{}
_, err := doEncode(v, func(cfg *EncodeConfig) {
cfg.ConfigureColumn("ColX", func(cfg *EncodeColumnConfig) {})
cfg.ConfigureColumn("col1", func(cfg *EncodeColumnConfig) {})
cfg.ConfigureColumn("colX", func(cfg *EncodeColumnConfig) {})
})
assert.ErrorIs(t, err, ErrConfigOptionInvalid)
})
t.Run("#2: localize header without localization func", func(t *testing.T) {
v := []Item{}
_, err := doEncode(v, func(cfg *EncodeConfig) {
cfg.LocalizeHeader = true
})
assert.ErrorIs(t, err, ErrConfigOptionInvalid)
})
t.Run("#3: invalid input var", func(t *testing.T) {
var v []Item
_, err := doEncode(v)
assert.ErrorIs(t, err, ErrValueNil)
})
t.Run("#4: invalid input var", func(t *testing.T) {
v := []string{}
_, err := doEncode(&v)
assert.ErrorIs(t, err, ErrTypeInvalid)
})
t.Run("#5: invalid input var", func(t *testing.T) {
var v Item
_, err := doEncode(&v)
assert.ErrorIs(t, err, ErrTypeInvalid)
})
}
func Test_Encode_withHeader(t *testing.T) {
type Item struct {
ColX bool `csv:",optional"`
ColY bool
Col1 int `csv:"col1"`
Col2 float32 `csv:"col2"`
}
t.Run("#1: column optional", func(t *testing.T) {
v := []Item{
{Col1: 1, Col2: 2.123},
{Col1: 100, Col2: 200},
}
data, err := doEncode(v)
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`ColX,col1,col2
false,1,2.123
false,100,200
`), string(data))
})
t.Run("#2: no header mode", func(t *testing.T) {
v := []Item{
{Col1: 1, Col2: 2.123},
{Col1: 100, Col2: 200},
}
data, err := doEncode(v, func(cfg *EncodeConfig) {
cfg.NoHeaderMode = true
})
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`false,1,2.123
false,100,200
`), string(data))
})
}
func Test_Encode_withPostprocessor(t *testing.T) {
type Item struct {
ColX bool `csv:",optional,omitempty"`
ColY bool
Col1 int `csv:"col1"`
Col2 string `csv:"col2"`
}
t.Run("#1: trim/upper specific column after encoding", func(t *testing.T) {
v := []Item{
{Col1: 1, Col2: "\tabcXYZ "},
{Col1: 100, Col2: " xYz123 "},
}
data, err := doEncode(v, func(cfg *EncodeConfig) {
cfg.ConfigureColumn("col2", func(cfg *EncodeColumnConfig) {
cfg.PostprocessorFuncs = []ProcessorFunc{ProcessorTrim, ProcessorUpper}
})
})
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`ColX,col1,col2
,1,ABCXYZ
,100,XYZ123
`), string(data))
})
t.Run("#2: add comma to numbers after encoding", func(t *testing.T) {
type Item struct {
ColX bool `csv:",optional,omitempty"`
ColY bool
Col1 int `csv:"col1"`
Col2 float64 `csv:"col2"`
}
v := []Item{
{Col1: 12345, Col2: 1234.1234567},
{Col1: 1234567, Col2: 1.1234},
}
data, err := doEncode(v, func(cfg *EncodeConfig) {
cfg.ConfigureColumn("col1", func(cfg *EncodeColumnConfig) {
cfg.PostprocessorFuncs = []ProcessorFunc{ProcessorNumberGroupComma}
})
cfg.ConfigureColumn("col2", func(cfg *EncodeColumnConfig) {
cfg.PostprocessorFuncs = []ProcessorFunc{ProcessorNumberGroupComma}
})
})
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`ColX,col1,col2
,"12,345","1,234.1234567"
,"1,234,567",1.1234
`), string(data))
})
}
func Test_Encode_withSpecialChars(t *testing.T) {
type Item struct {
ColX bool `csv:",optional,omitempty"`
ColY bool
Col1 int `csv:"col1"`
Col2 string `csv:"col2"`
}
t.Run("#1: values have CRLF and TAB", func(t *testing.T) {
v := []Item{
{Col1: 1, Col2: " a\tbc\nXYZ "},
{Col1: 100, Col2: " xYz\n123 "},
}
data, err := doEncode(v, func(cfg *EncodeConfig) {
cfg.ConfigureColumn("col2", func(cfg *EncodeColumnConfig) {
cfg.PostprocessorFuncs = []ProcessorFunc{ProcessorTrim, ProcessorUpper}
})
})
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`ColX,col1,col2
,1,"A BC
XYZ"
,100,"XYZ
123"
`), string(data))
})
t.Run("#2: values have bare double-quote and single quote", func(t *testing.T) {
v := []Item{
{Col1: 1, Col2: "abc\"XYZ"},
{Col1: 100, Col2: "xYz'123"},
}
data, err := doEncode(v)
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`ColX,col1,col2
,1,"abc""XYZ"
,100,xYz'123
`), string(data))
})
t.Run("#3: values have valid double-quote", func(t *testing.T) {
v := []Item{
{Col1: 1, Col2: "\"abcXYZ\""},
{Col1: 100, Col2: "xYz'123"},
}
data, err := doEncode(v)
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`ColX,col1,col2
,1,"""abcXYZ"""
,100,xYz'123
`), string(data))
})
}
func Test_Encode_withOmitEmpty(t *testing.T) {
type Item struct {
ColX bool `csv:",optional,omitempty"`
Col1 int `csv:"col1,omitempty"`
Col2 string `csv:"col2,omitempty"`
Col3 *int `csv:"col3,omitempty"`
}
t.Run("#1: success", func(t *testing.T) {
v := []*Item{
{},
nil,
{Col3: gofn.New(0)},
{Col1: 123},
}
data, err := doEncode(v)
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`ColX,col1,col2,col3
,,,
,,,
,123,,
`), string(data))
})
}
func Test_Encode_multipleCalls(t *testing.T) {
type Item struct {
ColY bool
Col1 int `csv:"col1"`
Col2 float32 `csv:"col2"`
}
type Item2 struct {
ColY bool
Col1 int `csv:"col1"`
Col2 float32 `csv:"col2"`
}
t.Run("#1: encode multiple times", func(t *testing.T) {
v := []Item{
{Col1: 1, Col2: 1.12345},
{Col1: 2, Col2: 6.543210},
}
e, w, buf := makeEncoder()
err := e.Encode(v)
w.Flush()
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`col1,col2
1,1.12345
2,6.54321
`), buf.String())
// Second call
v = []Item{
{Col1: 3, Col2: 1.12345},
{Col1: 4, Col2: 6.543},
}
err = e.Encode(v)
w.Flush()
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`col1,col2
1,1.12345
2,6.54321
3,1.12345
4,6.543
`), buf.String())
// Finish encoding, then try to encode more
_ = e.Finish()
err = e.Encode(v)
assert.ErrorIs(t, err, ErrFinished)
})
t.Run("#2: encode different types of data", func(t *testing.T) {
v := []Item{
{Col1: 1, Col2: 1.12345},
{Col1: 2, Col2: 6.543210},
}
e, w, buf := makeEncoder()
err := e.Encode(v)
w.Flush()
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`col1,col2
1,1.12345
2,6.54321
`), buf.String())
// Second call use different data type
v2 := []Item2{
{Col1: 3, Col2: 1.12345},
{Col1: 4, Col2: 6.543},
}
err = e.Encode(v2)
assert.ErrorIs(t, err, ErrTypeUnmatched)
})
}
func Test_Encode_withFixedInlineColumn(t *testing.T) {
type Sub struct {
ColZ bool `csv:",optional"`
ColY bool
Col1 int16 `csv:"sub1"`
Col2 string `csv:"sub2,optional"`
}
type Item struct {
ColX bool `csv:",optional,omitempty"`
ColY bool
Col1 int `csv:"col1"`
Sub1 Sub `csv:"sub1,inline"`
Col2 string `csv:"col2"`
}
type Item2 struct {
ColX bool `csv:",optional,omitempty"`
ColY bool
Col1 int `csv:"col1"`
Sub1 *Sub `csv:"sub1,inline"`
Col2 string `csv:"col2"`
}
t.Run("#1: success", func(t *testing.T) {
v := []Item{
{Col1: 1, Sub1: Sub{Col1: 111}, Col2: "abcxyz123"},
{Col1: 1000, Sub1: Sub{Col1: 222}, Col2: "abc123"},
}
data, err := doEncode(v)
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`ColX,col1,ColZ,sub1,sub2,col2
,1,false,111,,abcxyz123
,1000,false,222,,abc123
`), string(data))
})
t.Run("#2: success with pointer type", func(t *testing.T) {
v := []Item2{
{Col1: 1, Sub1: &Sub{Col1: 111}, Col2: "abcxyz123"},
{Col1: 1000, Sub1: &Sub{Col1: 222}, Col2: "abc123"},
}
data, err := doEncode(v)
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`ColX,col1,ColZ,sub1,sub2,col2
,1,false,111,,abcxyz123
,1000,false,222,,abc123
`), string(data))
})
t.Run("#3: with no header mode", func(t *testing.T) {
v := []Item{
{Col1: 1, Sub1: Sub{Col1: 111}, Col2: "abcxyz123"},
{Col1: 1000, Sub1: Sub{Col1: 222}, Col2: "abc123"},
}
data, err := doEncode(v, func(cfg *EncodeConfig) {
cfg.NoHeaderMode = true
})
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`,1,false,111,,abcxyz123
,1000,false,222,,abc123
`), string(data))
})
t.Run("#4: with column prefix", func(t *testing.T) {
type Item struct {
ColX bool `csv:",optional,omitempty"`
ColY bool
Col1 int `csv:"col1"`
Sub1 *Sub `csv:"sub1,inline,prefix=sub_"`
Col2 string `csv:"col2"`
}
v := []Item{
{Col1: 1, Sub1: &Sub{Col1: 111}, Col2: "abcxyz123"},
{Col1: 1000, Sub1: &Sub{Col1: 222}, Col2: "abc123"},
}
data, err := doEncode(v)
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`ColX,col1,sub_ColZ,sub_sub1,sub_sub2,col2
,1,false,111,,abcxyz123
,1000,false,222,,abc123
`), string(data))
})
t.Run("#5: invalid inline column", func(t *testing.T) {
type Item struct {
ColX bool `csv:",optional"`
ColY bool
Col1 int `csv:"col1,inline"`
Col2 string `csv:"col2"`
}
v := []Item{}
_, err := doEncode(v)
assert.ErrorIs(t, err, ErrHeaderDynamicTypeInvalid)
})
t.Run("#6: with prefix and custom postprocessor", func(t *testing.T) {
type Item struct {
Col1 int `csv:"col1"`
Sub1 Sub `csv:"sub1,inline,prefix=sub_"`
Col2 string `csv:"col2"`
}
v := []Item{
{Col1: 1, Sub1: Sub{Col1: 12345, Col2: "abC"}, Col2: "abcxyz123"},
{Col1: 1000, Sub1: Sub{Col1: 4321, Col2: "xYz123"}, Col2: "abc123"},
}
data, err := doEncode(v, func(cfg *EncodeConfig) {
cfg.ConfigureColumn("sub_sub1", func(cfg *EncodeColumnConfig) {
cfg.PostprocessorFuncs = []ProcessorFunc{ProcessorNumberGroupComma}
})
cfg.ConfigureColumn("sub1", func(cfg *EncodeColumnConfig) {
cfg.PostprocessorFuncs = []ProcessorFunc{ProcessorUpper}
})
})
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`col1,sub_ColZ,sub_sub1,sub_sub2,col2
1,FALSE,"12,345",ABC,abcxyz123
1000,FALSE,"4,321",XYZ123,abc123
`), string(data))
})
}
func Test_Encode_withDynamicInlineColumn(t *testing.T) {
type Item struct {
ColX bool `csv:",optional"`
ColY bool `csv:"-"`
Col1 int `csv:"col1"`
Sub1 InlineColumn[int] `csv:"sub1,inline"`
Col2 *string `csv:"col2"`
ColZ bool `csv:",optional"`
}
type Item2 struct {
ColX bool `csv:",optional"`
ColY bool `csv:"-"`
Col1 int `csv:"col1"`
Sub1 *InlineColumn[int] `csv:"sub1,inline"`
Col2 *string `csv:"col2"`
ColZ bool `csv:",optional"`
}
t.Run("#1: success", func(t *testing.T) {
header := []string{"sub1", "sub2"}
v := []Item{
{Col1: 1, Sub1: InlineColumn[int]{Header: header, Values: []int{111, 11}}, Col2: gofn.New("abcxyz123")},
{Col1: 1000, Sub1: InlineColumn[int]{Header: header, Values: []int{222, 22}}, Col2: gofn.New("abc123")},
}
data, err := doEncode(v)
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`ColX,col1,sub1,sub2,col2,ColZ
false,1,111,11,abcxyz123,false
false,1000,222,22,abc123,false
`), string(data))
})
t.Run("#2: success with using pointer", func(t *testing.T) {
header := []string{"sub1", "sub2"}
v := []Item2{
{Col1: 1, Sub1: &InlineColumn[int]{Header: header, Values: []int{111, 11}}, Col2: gofn.New("abcxyz123")},
{Col1: 1000, Sub1: &InlineColumn[int]{Header: header, Values: []int{222, 22}}, Col2: gofn.New("abc123")},
}
data, err := doEncode(v)
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`ColX,col1,sub1,sub2,col2,ColZ
false,1,111,11,abcxyz123,false
false,1000,222,22,abc123,false
`), string(data))
})
t.Run("#3: no header mode", func(t *testing.T) {
header := []string{"sub1", "sub2"}
v := []Item{
{Col1: 1, Sub1: InlineColumn[int]{Header: header, Values: []int{111, 11}}, Col2: gofn.New("abcxyz123")},
{Col1: 1000, Sub1: InlineColumn[int]{Header: header, Values: []int{222, 22}}, Col2: gofn.New("abc123")},
}
data, err := doEncode(v, func(cfg *EncodeConfig) {
cfg.NoHeaderMode = true
})
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`false,1,111,11,abcxyz123,false
false,1000,222,22,abc123,false
`), string(data))
})
t.Run("#4: with column prefix", func(t *testing.T) {
type Item struct {
ColX bool `csv:",optional"`
ColY bool `csv:"-"`
Col1 *int `csv:"col1"`
Sub1 InlineColumn[int] `csv:"sub1,inline,prefix=sub_"`
Col2 string `csv:"col2"`
}
header := []string{"col1", "col2"}
v := []Item{
{Col1: gofn.New(1), Sub1: InlineColumn[int]{Header: header, Values: []int{1234, 11}}, Col2: "abcxyz123"},
{Col1: gofn.New(1000), Sub1: InlineColumn[int]{Header: header, Values: []int{12345, 22}}, Col2: "abc123"},
}
data, err := doEncode(v, func(cfg *EncodeConfig) {
cfg.LocalizeHeader = true
cfg.LocalizationFunc = localizeViVn
cfg.ConfigureColumn("sub_col1", func(cfg *EncodeColumnConfig) {
cfg.PostprocessorFuncs = []ProcessorFunc{ProcessorNumberGroupComma}
})
})
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`Cột-X,cột-1,cột-phụ-1,cột-phụ-2,cột-2
false,1,"1,234",11,abcxyz123
false,1000,"12,345",22,abc123
`), string(data))
})
t.Run("#5: invalid dynamic inline type", func(t *testing.T) {
type InlineColumn2[T any] struct {
Header []string
}
type Item struct {
Col1 int `csv:"col1"`
Sub1 InlineColumn2[int] `csv:"sub1,inline"`
Col2 string `csv:"col2"`
}
header := []string{"sub1", "sub2"}
v := []*Item{
{Col1: 1, Sub1: InlineColumn2[int]{Header: header}, Col2: "aBc123"},
{Col1: 2, Sub1: InlineColumn2[int]{Header: header}, Col2: "xyzZ"},
}
_, err := doEncode(v)
assert.ErrorIs(t, err, ErrHeaderDynamicTypeInvalid)
})
t.Run("#6: invalid dynamic inline type", func(t *testing.T) {
type InlineColumn2[T any] struct {
Header []string
Values *[]T
}
type Item struct {
Col1 int `csv:"col1"`
Sub1 InlineColumn2[int] `csv:"sub1,inline"`
Col2 string `csv:"col2"`
}
header := []string{"sub1", "sub2"}
v := []*Item{
{Col1: 1, Sub1: InlineColumn2[int]{Header: header}, Col2: "aBc123"},
{Col1: 2, Sub1: InlineColumn2[int]{Header: header}, Col2: "xyzZ"},
}
_, err := doEncode(v)
assert.ErrorIs(t, err, ErrHeaderDynamicTypeInvalid)
})
}
func Test_Encode_withLocalization(t *testing.T) {
type Item struct {
ColX bool `csv:",optional"`
ColY bool `csv:"-"`
Col1 int16 `csv:"col1"`
Col2 StrType `csv:"col2"`
}
t.Run("#1: localization fails", func(t *testing.T) {
v := []Item{}
_, err := doEncode(v, func(cfg *EncodeConfig) {
cfg.LocalizeHeader = true
cfg.LocalizationFunc = localizeFail
})
assert.ErrorIs(t, err, ErrLocalization)
assert.ErrorIs(t, err, errKeyNotFound)
})
t.Run("#2: no header mode, empty data", func(t *testing.T) {
v := []Item{}
data, err := doEncode(v, func(cfg *EncodeConfig) {
cfg.NoHeaderMode = true
cfg.LocalizeHeader = true
cfg.LocalizationFunc = localizeEnUs
})
assert.Nil(t, err)
assert.Equal(t, "", string(data))
})
t.Run("#3: no header mode, have data", func(t *testing.T) {
v := []Item{
{Col1: 111},
}
data, err := doEncode(v, func(cfg *EncodeConfig) {
cfg.NoHeaderMode = true
cfg.LocalizeHeader = true
cfg.LocalizationFunc = localizeEnUs
})
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`false,111,
`), string(data))
})
}
func Test_Encode_withCustomMarshaler(t *testing.T) {
t.Run("#1: no encode func matching", func(t *testing.T) {
type Item struct {
Col1 int `csv:"col1"`
Col2 float32 `csv:"col2"`
Col3 map[int]string `csv:"col3"`
}
v := []Item{}
_, err := doEncode(v)
assert.ErrorIs(t, err, ErrTypeUnsupported)
})
t.Run("#2: ptr of type implements MarshalText/MarshalCSV", func(t *testing.T) {
type Item struct {
ColX bool `csv:",optional"`
ColY bool
Col1 int `csv:"col1"`
Col2 StrUpperType `csv:"col2"`
Col3 StrLowerType `csv:"col3"`
}
v := []Item{
{Col1: 1, Col2: "aBcXyZ123", Col3: "aA"},
{Col1: 1000, Col2: "aBc123", Col3: "bB"},
}
data, err := doEncode(v)
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`ColX,col1,col2,col3
false,1,ABCXYZ123,aa
false,1000,ABC123,bb
`), string(data))
})
t.Run("#3: type implements MarshalText/MarshalCSV", func(t *testing.T) {
type Item struct {
ColX bool `csv:",optional"`
ColY bool
Col1 int `csv:"col1"`
Col2 *StrUpperType `csv:"col2"`
Col3 *StrLowerType `csv:"col3"`
}
v := []Item{
{Col1: 1, Col2: gofn.New[StrUpperType]("aBcXyZ123"), Col3: gofn.New[StrLowerType]("aA")},
{Col1: 1000, Col2: gofn.New[StrUpperType]("aBc123"), Col3: gofn.New[StrLowerType]("bB")},
}
data, err := doEncode(v)
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`ColX,col1,col2,col3
false,1,ABCXYZ123,aa
false,1000,ABC123,bb
`), string(data))
})
}
func Test_Encode_specialCases(t *testing.T) {
type Item struct {
ColX bool `csv:",optional"`
ColY bool
Col1 int `csv:"col1"`
Col2 float32 `csv:"col2"`
}
t.Run("#1: no input data", func(t *testing.T) {
v := []Item{}
data, err := doEncode(v)
assert.Nil(t, err)
assert.Equal(t, 0, len(v))
assert.Equal(t, gofn.MultilineString(
`ColX,col1,col2
`), string(data))
})
t.Run("#2: no input data and no header mode", func(t *testing.T) {
v := []Item{}
data, err := doEncode(v, func(cfg *EncodeConfig) {
cfg.NoHeaderMode = true
})
assert.Nil(t, err)
assert.Equal(t, "", string(data))
})
t.Run("#3: duplicated column from struct", func(t *testing.T) {
type Item2 struct {
ColX bool `csv:",optional"`
ColY bool
Col1 int `csv:"col1"`
Col2 float32 `csv:"col2"`
Col3 int `csv:"col1"`
}
v := []Item2{}
_, err := doEncode(v)
assert.ErrorIs(t, err, ErrHeaderColumnDuplicated)
})
t.Run("#4: invalid header (contains space)", func(t *testing.T) {
type Item struct {
ColX bool `csv:",optional"`
ColY bool
Col1 int `csv:"col1 "`
Col2 float32 `csv:" col2"`
}
v := []Item{}
_, err := doEncode(v)
assert.ErrorIs(t, err, ErrHeaderColumnInvalid)
})
t.Run("#5: item type is pointer and nil item is ignored", func(t *testing.T) {
v := []*Item{
{Col1: 1, Col2: 2.123},
nil,
{Col1: 100, Col2: 200},
}
data, err := doEncode(v)
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`ColX,col1,col2
false,1,2.123
false,100,200
`), string(data))
})
}
func Test_Encode_specialTypes(t *testing.T) {
t.Run("#1: any type", func(t *testing.T) {
type Item struct {
Col1 int `csv:"col1"`
Col2 any `csv:"col2"`
}
v := []Item{
{Col1: 1, Col2: 2.123},
{Col1: 2, Col2: "200"},
{Col1: 3, Col2: true},
{Col1: 4, Col2: nil},
}
data, err := doEncode(v)
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`col1,col2
1,2.123
2,200
3,true
4,
`), string(data))
})
t.Run("#2: ptr to any type", func(t *testing.T) {
type Item struct {
Col1 int `csv:"col1"`
Col2 *any `csv:"col2"`
}
v := []*Item{
{Col1: 1, Col2: gofn.New[any](2.123)},
nil,
{Col1: 100, Col2: gofn.New[any]("200")},
{Col1: 100, Col2: gofn.New[any](true)},
}
data, err := doEncode(v)
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`col1,col2
1,2.123
100,200
100,true
`), string(data))
})
t.Run("#3: all base types", func(t *testing.T) {
type Item struct {
Col1 int `csv:"c1"`
Col2 *int `csv:"c2"`
Col3 int8 `csv:"c3"`
Col4 *int8 `csv:"c4"`
Col5 int16 `csv:"c5"`
Col6 *int16 `csv:"c6"`
Col7 int32 `csv:"c7"`
Col8 *int32 `csv:"c8"`
Col9 int64 `csv:"c9"`
Col10 *int64 `csv:"c10"`
Col11 uint `csv:"c11"`
Col12 *uint `csv:"c12"`
Col13 uint8 `csv:"c13"`
Col14 *uint8 `csv:"c14"`
Col15 uint16 `csv:"c15"`
Col16 *uint16 `csv:"c16"`
Col17 uint32 `csv:"c17"`
Col18 *uint32 `csv:"c18"`
Col19 uint64 `csv:"c19"`
Col20 *uint64 `csv:"c20"`
Col21 float32 `csv:"c21"`
Col22 *float32 `csv:"c22"`
Col23 float64 `csv:"c23"`
Col24 *float64 `csv:"c24"`
Col25 bool `csv:"c25"`
Col26 *bool `csv:"c26"`
Col27 string `csv:"c27"`
Col28 *string `csv:"c28"`
}
v := []Item{
{-1, gofn.New(-1), int8(-1), gofn.New(int8(-1)), int16(-1), gofn.New(int16(-1)),
int32(-1), gofn.New(int32(-1)), int64(-1), gofn.New(int64(-1)), uint(1), gofn.New(uint(1)),
uint8(1), gofn.New(uint8(1)), uint16(1), gofn.New(uint16(1)), uint32(1), gofn.New(uint32(1)),
uint64(1), gofn.New(uint64(1)), float32(1), gofn.New(float32(1)), float64(1), gofn.New(float64(1)),
false, gofn.New(false), "abc", gofn.New("123")},
}
data, err := doEncode(v)
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17,c18,c19,c20,c21,c22,c23,c24,c25,c26,c27,c28
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,false,false,abc,123
`), string(data))
})
}
func Test_EncodeOne(t *testing.T) {
type Item struct {
ColY bool
Col1 int `csv:"col1,omitempty"`
Col2 *float32 `csv:"col2,omitempty"`
}
type Item2 struct {
ColY bool
Col1 int `csv:"col1,omitempty"`
Col2 float32 `csv:"col2,omitempty"`
}
t.Run("#1: encode one multiple times", func(t *testing.T) {
e, w, buf := makeEncoder()
err := e.EncodeOne(Item{Col1: 1, Col2: gofn.New[float32](1.12345)})
w.Flush()
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`col1,col2
1,1.12345
`), buf.String())
// Second call
err = e.EncodeOne(Item{Col1: 2, Col2: gofn.New[float32](0.0)})
w.Flush()
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`col1,col2
1,1.12345
2,
`), buf.String())
// Third call
err = e.EncodeOne(Item{Col1: 0, Col2: gofn.New[float32](2.22)})
w.Flush()
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`col1,col2
1,1.12345
2,
,2.22
`), buf.String())
// Encode a different data type
err = e.EncodeOne(Item2{Col1: 11, Col2: 11})
assert.ErrorIs(t, err, ErrTypeUnmatched)
// Finish encoding, then try to encode more
_ = e.Finish()
err = e.EncodeOne(Item{Col1: 0})
assert.ErrorIs(t, err, ErrFinished)
})
}