-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtag_test.go
More file actions
827 lines (816 loc) · 23.3 KB
/
tag_test.go
File metadata and controls
827 lines (816 loc) · 23.3 KB
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
// Copyright 2025 Codnect
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tag
import (
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
type CustomType struct {
String string `option:"string"`
Int int `option:"int"`
Float float64 `option:"float"`
Bool bool `option:"bool"`
Slice []any `option:"slice"`
Map map[string]string `option:"map"`
}
type TestTag[T any] struct {
Value T `option:"value"`
String string `option:"string"`
Int int `option:"int"`
Float float64 `option:"float"`
Bool bool `option:"bool"`
Slice []any `option:"slice"`
Map map[string]any `option:"map"`
StringSlice []string `option:"stringSlice"`
IntSlice []int `option:"intSlice"`
FloatSlice []float64 `option:"floatSlice"`
MapSlice []map[string]any `option:"mapSlice"`
Struct CustomType `option:"struct"`
notExported string `option:"notExported"`
OptionWithoutTag string
InvalidFlag string `option:"invalidFlag"`
}
func (t TestTag[T]) Tag() string {
return "test"
}
func TestParse(t *testing.T) {
testCases := []struct {
name string
rawTags string
tagStruct Tagger
wantErr error
wantTagStruct Tagger
}{
{
name: "nil tag struct",
rawTags: ``,
tagStruct: nil,
wantErr: nil,
},
{
name: "empty tags",
rawTags: ``,
tagStruct: &TestTag[any]{},
wantErr: nil,
},
{
name: "non pointer tag struct",
rawTags: `test:"value"`,
tagStruct: TestTag[any]{},
wantErr: errors.New("tag: target must be pointer to struct, got tag.TestTag[interface {}]"),
},
{
name: "blank tags",
rawTags: ` `,
tagStruct: &TestTag[any]{},
wantErr: nil,
},
{
name: "missing tag name",
rawTags: `:"value"`,
tagStruct: &TestTag[any]{},
wantErr: errors.New(`tag: empty tag name in ':"value"'`),
},
{
name: "missing tag value",
rawTags: `test:`,
tagStruct: &TestTag[any]{},
wantErr: errors.New(`tag: missing tag value for tag 'test'`),
},
{
name: "empty tag value",
rawTags: `test:""`,
tagStruct: &TestTag[any]{},
wantErr: errors.New(`tag: empty tag value for tag 'test'`),
},
{
name: "missing positional option value",
rawTags: `test:",flag"`,
tagStruct: &TestTag[any]{},
wantErr: errors.New(`tag: failed to parse 'test' options ",flag": missing positional option value`),
},
{
name: "missing colon after tag name",
rawTags: `test"value"`,
tagStruct: &TestTag[any]{},
wantErr: errors.New(`tag: expected ':' after tag name "test" in "test\"value\""`),
},
{
name: "missing colon after tag name with space before value",
rawTags: `test "value"`,
tagStruct: &TestTag[any]{},
wantErr: errors.New(`tag: expected ':' after tag name "test" in "test \"value\""`),
},
{
name: "missing colon after tag name with space before value",
rawTags: `test:"value,,"`,
tagStruct: &TestTag[any]{},
wantErr: errors.New(`tag: failed to parse 'test' options "value,,": missing option key`),
},
{
name: "missing comma after positional option",
rawTags: `test:"any int=1141"`,
tagStruct: &TestTag[any]{},
wantErr: errors.New(`tag: failed to parse 'test' options "any int=1141": missing ',' after positional option value`),
},
{
name: "tag value with no quotes",
rawTags: `test:value`,
tagStruct: &TestTag[any]{},
wantErr: errors.New(`tag: invalid tag value: must start with "`),
},
{
name: "tag value with missing ending quote",
rawTags: `test:"value`,
tagStruct: &TestTag[any]{},
wantErr: errors.New(`tag: invalid tag value: must end with "`),
},
{
name: "missing option value after equal sign",
rawTags: `test:"any,int="`,
tagStruct: &TestTag[any]{},
wantErr: errors.New(`tag: failed to parse 'test' options "any,int=": missing value for option "int"`),
},
{
name: "missing missing comma after option",
rawTags: `test:"any,int=1141 float=11.41"`,
tagStruct: &TestTag[any]{},
wantErr: errors.New(`tag: failed to parse 'test' options "any,int=1141 float=11.41": missing comma after option "int"`),
},
{
name: "not exported option",
rawTags: `test:"any,notExported='should not be set'"`,
tagStruct: &TestTag[any]{},
wantErr: nil,
wantTagStruct: &TestTag[any]{
Value: "any",
},
},
{
name: "option without tag",
rawTags: `test:"any,optionWithoutTag='should not be set'"`,
tagStruct: &TestTag[any]{},
wantErr: nil,
wantTagStruct: &TestTag[any]{
Value: "any",
},
},
{
name: "string positional option",
rawTags: `test:"any"`,
tagStruct: &TestTag[string]{},
wantErr: nil,
wantTagStruct: &TestTag[string]{
Value: "any",
},
},
{
name: "multiple tag",
rawTags: `any:"value" test:"any"`,
tagStruct: &TestTag[string]{},
wantErr: nil,
wantTagStruct: &TestTag[string]{
Value: "any",
},
},
{
name: "string positional option with quotes",
rawTags: `test:"'any value'"`,
tagStruct: &TestTag[string]{},
wantErr: nil,
wantTagStruct: &TestTag[string]{
Value: "any value",
},
},
{
name: "string slice positional option",
rawTags: `test:"['any','another']"`,
tagStruct: &TestTag[[]string]{},
wantErr: nil,
wantTagStruct: &TestTag[[]string]{
Value: []string{"any", "another"},
},
},
{
name: "int8 positional option",
rawTags: `test:"11"`,
tagStruct: &TestTag[int8]{},
wantErr: nil,
wantTagStruct: &TestTag[int8]{
Value: 11,
},
},
{
name: "int8 slice positional option",
rawTags: `test:"[11,-41]"`,
tagStruct: &TestTag[[]int8]{},
wantErr: nil,
wantTagStruct: &TestTag[[]int8]{
Value: []int8{11, -41},
},
},
{
name: "int16 positional option",
rawTags: `test:"11"`,
tagStruct: &TestTag[int16]{},
wantErr: nil,
wantTagStruct: &TestTag[int16]{
Value: 11,
},
},
{
name: "int16 slice positional option",
rawTags: `test:"[11,-41]"`,
tagStruct: &TestTag[[]int16]{},
wantErr: nil,
wantTagStruct: &TestTag[[]int16]{
Value: []int16{11, -41},
},
},
{
name: "int32 positional option",
rawTags: `test:"11"`,
tagStruct: &TestTag[int32]{},
wantErr: nil,
wantTagStruct: &TestTag[int32]{
Value: 11,
},
},
{
name: "int32 slice positional option",
rawTags: `test:"[11,-41]"`,
tagStruct: &TestTag[[]int32]{},
wantErr: nil,
wantTagStruct: &TestTag[[]int32]{
Value: []int32{11, -41},
},
},
{
name: "int64 positional option",
rawTags: `test:"11"`,
tagStruct: &TestTag[int64]{},
wantErr: nil,
wantTagStruct: &TestTag[int64]{
Value: 11,
},
},
{
name: "int64 slice positional option",
rawTags: `test:"[11,-41]"`,
tagStruct: &TestTag[[]int64]{},
wantErr: nil,
wantTagStruct: &TestTag[[]int64]{
Value: []int64{11, -41},
},
},
{
name: "invalid int positional option",
rawTags: `test:"a1"`,
tagStruct: &TestTag[int8]{},
wantErr: errors.New(`tag: failed to parse 'test' options "a1": invalid int value "a1"`),
},
{
name: "int positional option",
rawTags: `test:"1141"`,
tagStruct: &TestTag[int]{},
wantErr: nil,
wantTagStruct: &TestTag[int]{
Value: 1141,
},
},
{
name: "int slice positional option",
rawTags: `test:"[11,-41]"`,
tagStruct: &TestTag[[]int]{},
wantErr: nil,
wantTagStruct: &TestTag[[]int]{
Value: []int{11, -41},
},
},
{
name: "int8 positional option",
rawTags: `test:"11"`,
tagStruct: &TestTag[int8]{},
wantErr: nil,
wantTagStruct: &TestTag[int8]{
Value: 11,
},
},
{
name: "uint8 slice positional option",
rawTags: `test:"[11,41]"`,
tagStruct: &TestTag[[]uint8]{},
wantErr: nil,
wantTagStruct: &TestTag[[]uint8]{
Value: []uint8{11, 41},
},
},
{
name: "uint16 positional option",
rawTags: `test:"11"`,
tagStruct: &TestTag[uint16]{},
wantErr: nil,
wantTagStruct: &TestTag[uint16]{
Value: 11,
},
},
{
name: "uint16 slice positional option",
rawTags: `test:"[11,41]"`,
tagStruct: &TestTag[[]uint16]{},
wantErr: nil,
wantTagStruct: &TestTag[[]uint16]{
Value: []uint16{11, 41},
},
},
{
name: "uint32 positional option",
rawTags: `test:"11"`,
tagStruct: &TestTag[uint32]{},
wantErr: nil,
wantTagStruct: &TestTag[uint32]{
Value: 11,
},
},
{
name: "uint32 slice positional option",
rawTags: `test:"[11,41]"`,
tagStruct: &TestTag[[]uint32]{},
wantErr: nil,
wantTagStruct: &TestTag[[]uint32]{
Value: []uint32{11, 41},
},
},
{
name: "uint64 positional option",
rawTags: `test:"11"`,
tagStruct: &TestTag[uint64]{},
wantErr: nil,
wantTagStruct: &TestTag[uint64]{
Value: 11,
},
},
{
name: "uint64 slice positional option",
rawTags: `test:"[11,41]"`,
tagStruct: &TestTag[[]uint64]{},
wantErr: nil,
wantTagStruct: &TestTag[[]uint64]{
Value: []uint64{11, 41},
},
},
{
name: "invalid uint positional option",
rawTags: `test:"a1141"`,
tagStruct: &TestTag[uint]{},
wantErr: fmt.Errorf(`tag: failed to parse 'test' options "a1141": invalid unsigned int value "a1141"`),
},
{
name: "uint positional option",
rawTags: `test:"1141"`,
tagStruct: &TestTag[uint]{},
wantErr: nil,
wantTagStruct: &TestTag[uint]{
Value: 1141,
},
},
{
name: "uint slice positional option",
rawTags: `test:"[11,41]"`,
tagStruct: &TestTag[[]uint]{},
wantErr: nil,
wantTagStruct: &TestTag[[]uint]{
Value: []uint{11, 41},
},
},
{
name: "invalid float positional option",
rawTags: `test:"11.41a"`,
tagStruct: &TestTag[float32]{},
wantErr: errors.New(`tag: failed to parse 'test' options "11.41a": invalid float value "11.41a"`),
},
{
name: "float32 positional option",
rawTags: `test:"11.41"`,
tagStruct: &TestTag[float32]{},
wantErr: nil,
wantTagStruct: &TestTag[float32]{
Value: 11.41,
},
},
{
name: "float32 slice positional option",
rawTags: `test:"[11.41,41.11]"`,
tagStruct: &TestTag[[]float32]{},
wantErr: nil,
wantTagStruct: &TestTag[[]float32]{
Value: []float32{11.41, 41.11},
},
},
{
name: "float64 positional option",
rawTags: `test:"11.41"`,
tagStruct: &TestTag[float64]{},
wantErr: nil,
wantTagStruct: &TestTag[float64]{
Value: 11.41,
},
},
{
name: "float64 slice positional option",
rawTags: `test:"[11.41,41.11]"`,
tagStruct: &TestTag[[]float64]{},
wantErr: nil,
wantTagStruct: &TestTag[[]float64]{
Value: []float64{11.41, 41.11},
},
},
{
name: "invalid bool positional option",
rawTags: `test:"truea"`,
tagStruct: &TestTag[bool]{},
wantErr: errors.New(`tag: failed to parse 'test' options "truea": invalid bool value "truea"`),
},
{
name: "bool(true) positional option",
rawTags: `test:"true"`,
tagStruct: &TestTag[bool]{},
wantErr: nil,
wantTagStruct: &TestTag[bool]{
Value: true,
},
},
{
name: "bool(false) positional option",
rawTags: `test:"false"`,
tagStruct: &TestTag[bool]{},
wantErr: nil,
wantTagStruct: &TestTag[bool]{
Value: false,
},
},
{
name: "map positional option",
rawTags: `test:"{'string':'value','int':1141,'float':11.41,'bool':true,'slice':['a','b'],'nested':{'key':'val'}}"`,
tagStruct: &TestTag[map[string]any]{},
wantErr: nil,
wantTagStruct: &TestTag[map[string]any]{
Value: map[string]any{
"string": "value",
"int": int64(1141),
"float": 11.41,
"bool": true,
"slice": []any{"a", "b"},
"nested": map[string]any{"key": "val"},
},
},
},
{
name: "any positional option for string value",
rawTags: `test:"any"`,
tagStruct: &TestTag[any]{},
wantErr: nil,
wantTagStruct: &TestTag[any]{
Value: "any",
},
},
{
name: "any positional option for string value with quotes",
rawTags: `test:"'any value'"`,
tagStruct: &TestTag[any]{},
wantErr: nil,
wantTagStruct: &TestTag[any]{
Value: "any value",
},
},
{
name: "any positional option for int value",
rawTags: `test:"141"`,
tagStruct: &TestTag[any]{},
wantErr: nil,
wantTagStruct: &TestTag[any]{
Value: int64(141),
},
},
{
name: "any positional option for unsigned int value",
rawTags: `test:"-141"`,
tagStruct: &TestTag[any]{},
wantErr: nil,
wantTagStruct: &TestTag[any]{
Value: int64(-141),
},
},
{
name: "any positional option for bool(true) value",
rawTags: `test:"true"`,
tagStruct: &TestTag[any]{},
wantErr: nil,
wantTagStruct: &TestTag[any]{
Value: true,
},
},
{
name: "any positional option for bool(false) value",
rawTags: `test:"false"`,
tagStruct: &TestTag[any]{},
wantErr: nil,
wantTagStruct: &TestTag[any]{
Value: false,
},
},
{
name: "any positional option for float value",
rawTags: `test:"1.41"`,
tagStruct: &TestTag[any]{},
wantErr: nil,
wantTagStruct: &TestTag[any]{
Value: 1.41,
},
},
{
name: "any positional option for unsigned float value",
rawTags: `test:"-1.41"`,
tagStruct: &TestTag[any]{},
wantErr: nil,
wantTagStruct: &TestTag[any]{
Value: -1.41,
},
},
{
name: "any positional option for slice value",
rawTags: `test:"['any','another']"`,
tagStruct: &TestTag[any]{},
wantErr: nil,
wantTagStruct: &TestTag[any]{
Value: []any{"any", "another"},
},
},
{
name: "any positional option for map value",
rawTags: `test:"{'string':'value','int':1141,'float':11.41,'bool':true,'slice':['a','b'],'nested':{'key':'val'}}"`,
tagStruct: &TestTag[any]{},
wantErr: nil,
wantTagStruct: &TestTag[any]{
Value: map[string]any{
"string": "value",
"int": int64(1141),
"float": 11.41,
"bool": true,
"slice": []any{"a", "b"},
"nested": map[string]any{"key": "val"},
},
},
},
{
name: "custom type positional option",
rawTags: `test:"{'string':'value','int':1141,'float':11.41,'bool':true,'slice':['a','b'],'map':{'key':'val'}}"`,
tagStruct: &TestTag[CustomType]{},
wantErr: nil,
wantTagStruct: &TestTag[CustomType]{
Value: CustomType{
String: "value",
Int: 1141,
Float: 11.41,
Bool: true,
Slice: []any{"a", "b"},
Map: map[string]string{"key": "val"},
},
},
},
{
name: "flag option",
rawTags: `test:"any,bool"`,
tagStruct: &TestTag[any]{},
wantErr: nil,
wantTagStruct: &TestTag[any]{
Value: "any",
Bool: true,
},
},
{
name: "invalid slice positional option missing opening bracket",
rawTags: `test:"11,41]"`,
tagStruct: &TestTag[[]int]{},
wantErr: errors.New(`tag: failed to parse 'test' options "11,41]": expected [ at the beginning of slice value '11'`),
},
{
name: "invalid slice positional option missing closing bracket",
rawTags: `test:"[11,41"`,
tagStruct: &TestTag[[]int]{},
wantErr: errors.New(`tag: failed to parse 'test' options "[11,41": unterminated value, missing closing ']'`),
},
{
name: "invalid map positional option missing opening brace",
rawTags: `test:"'key':'val'}"`,
tagStruct: &TestTag[map[string]any]{},
wantErr: errors.New(`tag: failed to parse 'test' options "'key':'val'}": expected { at the beginning of map value 'key'`),
},
{
name: "full options",
rawTags: `test:"any,int=1141,float=11.41,bool=true,slice=['a','b'],map={'key':'val'},stringSlice=['str1','str2'],intSlice=[11,22],floatSlice=[1.1,2.2],mapSlice=[{'key1':'val1'},{'key2':'val2'}]"`,
tagStruct: &TestTag[any]{},
wantErr: nil,
wantTagStruct: &TestTag[any]{
Value: "any",
Int: 1141,
Float: 11.41,
Bool: true,
Slice: []any{"a", "b"},
Map: map[string]any{"key": "val"},
StringSlice: []string{"str1", "str2"},
IntSlice: []int{11, 22},
FloatSlice: []float64{1.1, 2.2},
MapSlice: []map[string]any{
{"key1": "val1"},
{"key2": "val2"},
},
},
},
{
name: "slice option with missing opening bracket",
rawTags: `test:"any,slice=['a','b'"`,
tagStruct: &TestTag[any]{},
wantErr: errors.New(`tag: failed to parse 'test' options "any,slice=['a','b'": unterminated value, missing closing ']'`),
},
{
name: "nested slice positional option",
rawTags: `test:"[['a','b'],['c','d']]"`,
tagStruct: &TestTag[[][]string]{},
wantErr: nil,
wantTagStruct: &TestTag[[][]string]{
Value: [][]string{
{"a", "b"},
{"c", "d"},
},
},
},
{
name: "unknown option flag",
rawTags: `test:"any,unknownFlag"`,
tagStruct: &TestTag[any]{},
wantErr: nil,
wantTagStruct: &TestTag[any]{
Value: "any",
},
},
{
name: "invalid option flag",
rawTags: `test:"any,invalidFlag"`,
tagStruct: &TestTag[any]{},
wantErr: errors.New(`tag: failed to parse 'test' options "any,invalidFlag": invalid type for flag option "invalidFlag": want bool`),
},
{
name: "struct option",
rawTags: `test:"any,struct={'string':'value','int':1141,'float':11.41,'bool':true,'slice':['a','b'],'map':{'key':'val'}}"`,
tagStruct: &TestTag[any]{},
wantErr: nil,
wantTagStruct: &TestTag[any]{
Value: "any",
Struct: CustomType{
String: "value",
Int: 1141,
Float: 11.41,
Bool: true,
Slice: []any{"a", "b"},
Map: map[string]string{"key": "val"},
},
},
},
{
name: "invalid struct option",
rawTags: `test:"any,struct='string':'value','int':1141,'float':11.41,'bool':true,'slice':['a','b'],'map':{'key':'val'}}"`,
tagStruct: &TestTag[any]{},
wantErr: errors.New(`tag: failed to parse 'test' options "any,struct='string':'value','int':1141,'float':11.41,'bool':true,'slice':['a','b'],'map':{'key':'val'}}": value must start with '{'`),
},
{
name: "invalid bool option with non-bool value",
rawTags: `test:"any,bool=non-bool"`,
tagStruct: &TestTag[any]{},
wantErr: errors.New(`tag: failed to parse 'test' options "any,bool=non-bool": invalid bool value "non-bool"`),
},
{
name: "invalid bool option with empty value",
rawTags: `test:"any,bool=[]"`,
tagStruct: &TestTag[any]{},
wantErr: errors.New(`tag: failed to parse 'test' options "any,bool=[]": invalid bool value`),
},
{
name: "invalid integer option value",
rawTags: `test:"any,int=non-int"`,
tagStruct: &TestTag[any]{},
wantErr: errors.New(`tag: failed to parse 'test' options "any,int=non-int": invalid integer value`),
},
{
name: "invalid float option value",
rawTags: `test:"any,float=non-float"`,
tagStruct: &TestTag[any]{},
wantErr: errors.New(`tag: failed to parse 'test' options "any,float=non-float": invalid float value`),
},
{
name: "string option with escape characters",
rawTags: `test:"any,string='line1\nline2\tTabbed'"`,
tagStruct: &TestTag[any]{
String: "line1\nline2\tTabbed",
},
},
{
name: "unsupported field type",
rawTags: `test:"any"`,
wantErr: errors.New(`tag: failed to parse 'test' options "any": unsupported field type: "chan bool"`),
tagStruct: &TestTag[chan bool]{},
},
{
name: "empty positional option value",
rawTags: `test:","`,
tagStruct: &TestTag[any]{},
wantErr: errors.New(`tag: failed to parse 'test' options ",": cannot infer type from empty value`),
},
{
name: "invalid map positional option with missing colon separator",
rawTags: `test:"{'key'}"`,
tagStruct: &TestTag[map[string]any]{},
wantErr: errors.New(`tag: failed to parse 'test' options "{'key'}": invalid map item format, missing ':' separator`),
},
{
name: "invalid custom type with missing colon separator",
rawTags: `test:"{'key'}"`,
tagStruct: &TestTag[CustomType]{},
wantErr: errors.New(`tag: failed to parse 'test' options "{'key'}": invalid struct item format, missing ':' separator`),
},
{
name: "invalid custom type positional option missing opening brace",
rawTags: `test:"'key':'val'}"`,
tagStruct: &TestTag[CustomType]{},
wantErr: errors.New(`tag: failed to parse 'test' options "'key':'val'}": expected { at the beginning of value 'key'`),
},
{
name: "expected , separator in slice",
rawTags: `test:"['a' 'b']"`,
tagStruct: &TestTag[[]string]{},
wantErr: errors.New(`tag: failed to parse 'test' options "['a' 'b']": expected ',' between slice items in ''b']'`),
},
{
name: "expected , separator in map",
rawTags: `test:"{'key1':'val1' 'key2':'val2'}"`,
tagStruct: &TestTag[map[string]string]{},
wantErr: errors.New(`tag: failed to parse 'test' options "{'key1':'val1' 'key2':'val2'}": expected ',' between map items in ''key2':'val2'}'`),
},
{
name: "expected , separator in custom type properties",
rawTags: `test:"{'string':'value' 'int':1141}"`,
tagStruct: &TestTag[CustomType]{},
wantErr: errors.New(`tag: failed to parse 'test' options "{'string':'value' 'int':1141}": expected ',' between fields in ''int':1141}'`),
},
{
name: "invalid map positional option with invalid key",
rawTags: `test:"{: 'val'}"`,
tagStruct: &TestTag[map[string]string]{},
wantErr: errors.New(`tag: failed to parse 'test' options "{: 'val'}": invalid key: must start with '`),
},
{
name: "invalid map positional option with invalid value",
rawTags: `test:"{'key':}"`,
tagStruct: &TestTag[map[string]any]{},
wantErr: errors.New(`tag: failed to parse 'test' options "{'key':}": invalid value: must start with '`),
},
{
name: "invalid custom type positional option with invalid field key",
rawTags: `test:"{: 'value'}"`,
tagStruct: &TestTag[CustomType]{},
wantErr: errors.New(`tag: failed to parse 'test' options "{: 'value'}": invalid field name: must start with '`),
},
{
name: "invalid custom type positional option with invalid field value",
rawTags: `test:"{'string':}"`,
tagStruct: &TestTag[CustomType]{},
wantErr: errors.New(`tag: failed to parse 'test' options "{'string':}": invalid field value: must start with '`),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := Parse(tc.rawTags, tc.tagStruct)
if tc.wantErr != nil {
assert.Equal(t, tc.wantErr.Error(), err.Error())
return
}
assert.NoError(t, err)
if tc.wantTagStruct != nil {
assert.Equal(t, tc.wantTagStruct, tc.tagStruct)
}
})
}
}