This repository has been archived by the owner on Sep 6, 2023. It is now read-only.
forked from go-playground/mold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmold_test.go
672 lines (536 loc) · 15.4 KB
/
mold_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
package mold_test
import (
"context"
"errors"
"reflect"
"strings"
"testing"
"time"
. "github.com/go-playground/assert/v2"
"github.com/takt-corp/mold"
)
// NOTES:
// - Run "go test" to run tests
// - Run "gocov test | gocov report" to report on test converage by file
// - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called
//
// or
//
// -- may be a good idea to change to output path to somewherelike /tmp
// go test -coverprofile cover.out && go tool cover -html=cover.out -o cover.html
//
func TestBadValues(t *testing.T) {
tform := mold.New()
tform.Register("blah", func(ctx context.Context, fl mold.FieldLevel) error { return nil })
type Test struct {
Ignore string `mold:"-"`
String string `mold:"blah,,blah"`
}
var tt Test
err := tform.Struct(context.Background(), &tt)
NotEqual(t, err, nil)
Equal(t, err.Error(), "invalid tag '' found on field String")
err = tform.Struct(context.Background(), tt)
NotEqual(t, err, nil)
Equal(t, err.Error(), "mold: Struct(non-pointer mold.Test)")
err = tform.Struct(context.Background(), nil)
NotEqual(t, err, nil)
Equal(t, err.Error(), "mold: Struct(nil)")
var i int
err = tform.Struct(context.Background(), &i)
NotEqual(t, err, nil)
Equal(t, err.Error(), "mold: (nil *int)")
var iface interface{}
err = tform.Struct(context.Background(), iface)
NotEqual(t, err, nil)
Equal(t, err.Error(), "mold: Struct(nil)")
iface = nil
err = tform.Struct(context.Background(), &iface)
NotEqual(t, err, nil)
Equal(t, err.Error(), "mold: (nil *interface {})")
var tst *Test
err = tform.Struct(context.Background(), tst)
NotEqual(t, err, nil)
Equal(t, err.Error(), "mold: Struct(nil *mold.Test)")
var tm *time.Time
err = tform.Field(context.Background(), tm, "blah")
NotEqual(t, err, nil)
Equal(t, err.Error(), "mold: Field(nil *time.Time)")
PanicMatches(t, func() { tform.Register("", nil) }, "Function Key cannot be empty")
PanicMatches(t, func() { tform.Register("test", nil) }, "Function cannot be empty")
PanicMatches(t, func() {
tform.Register(",", func(ctx context.Context, fl mold.FieldLevel) error { return nil })
}, "Tag ',' either contains restricted characters or is the same as a restricted tag needed for normal operation")
PanicMatches(t, func() { tform.RegisterAlias("", "") }, "Alias cannot be empty")
PanicMatches(t, func() { tform.RegisterAlias("test", "") }, "Aliased tags cannot be empty")
PanicMatches(t, func() { tform.RegisterAlias(",", "test") }, "Alias ',' either contains restricted characters or is the same as a restricted tag needed for normal operation")
}
func TestBasicTransform(t *testing.T) {
type Test struct {
String string `r:"repl"`
}
var tt Test
set := mold.New()
set.SetTagName("r")
set.Register("repl", func(ctx context.Context, fl mold.FieldLevel) error {
fl.Field().SetString("test")
return nil
})
err := set.Struct(context.Background(), &tt)
Equal(t, err, nil)
Equal(t, tt.String, "test")
type Test2 struct {
Test Test
String string `r:"repl"`
}
var tt2 Test2
err = set.Struct(context.Background(), &tt2)
Equal(t, err, nil)
Equal(t, tt2.Test.String, "test")
Equal(t, tt2.String, "test")
type Test3 struct {
Test
String string `r:"repl"`
}
var tt3 Test3
err = set.Struct(context.Background(), &tt3)
Equal(t, err, nil)
Equal(t, tt3.Test.String, "test")
Equal(t, tt3.String, "test")
type Test4 struct {
Test *Test
String string `r:"repl"`
}
var tt4 Test4
err = set.Struct(context.Background(), &tt4)
Equal(t, err, nil)
Equal(t, tt4.Test, nil)
Equal(t, tt4.String, "test")
tt5 := Test4{Test: &Test{}}
err = set.Struct(context.Background(), &tt5)
Equal(t, err, nil)
Equal(t, tt5.Test.String, "test")
Equal(t, tt5.String, "test")
type Test6 struct {
Test *Test `r:"default"`
String string `r:"repl"`
}
var tt6 Test6
set.Register("default", func(ctx context.Context, fl mold.FieldLevel) error {
fl.Field().Set(reflect.New(fl.Field().Type().Elem()))
return nil
})
err = set.Struct(context.Background(), &tt6)
Equal(t, err, nil)
NotEqual(t, tt6.Test, nil)
Equal(t, tt6.Test.String, "test")
Equal(t, tt6.String, "test")
tt6.String = "BAD"
var tString string
// wil invoke one processing and one waiting
go func() {
err := set.Field(context.Background(), &tString, "repl")
Equal(t, err, nil)
}()
err = set.Field(context.Background(), &tt6.String, "repl")
Equal(t, err, nil)
Equal(t, tt6.String, "test")
err = set.Field(context.Background(), &tt6.String, "")
Equal(t, err, nil)
err = set.Field(context.Background(), &tt6.String, "-")
Equal(t, err, nil)
err = set.Field(context.Background(), tt6.String, "test")
NotEqual(t, err, nil)
Equal(t, err.Error(), "mold: Field(non-pointer string)")
err = set.Field(context.Background(), nil, "test")
NotEqual(t, err, nil)
Equal(t, err.Error(), "mold: Field(nil)")
var iface interface{}
err = set.Field(context.Background(), iface, "test")
NotEqual(t, err, nil)
Equal(t, err.Error(), "mold: Field(nil)")
done := make(chan struct{})
go func() {
err := set.Field(context.Background(), &tString, "nonexistant")
NotEqual(t, err, nil)
close(done)
}()
err = set.Field(context.Background(), &tt6.String, "nonexistant")
NotEqual(t, err, nil)
Equal(t, err.Error(), "unregistered/undefined transformation 'nonexistant' found on field")
<-done
set.Register("dummy", func(ctx context.Context, fl mold.FieldLevel) error { return nil })
err = set.Field(context.Background(), &tt6.String, "dummy")
Equal(t, err, nil)
}
func TestAlias(t *testing.T) {
type Test struct {
String string `r:"repl,repl2"`
}
var tt Test
set := mold.New()
set.SetTagName("r")
set.Register("repl", func(ctx context.Context, fl mold.FieldLevel) error {
fl.Field().SetString("test")
return nil
})
set.Register("repl2", func(ctx context.Context, fl mold.FieldLevel) error {
fl.Field().SetString("test2")
return nil
})
err := set.Struct(context.Background(), &tt)
Equal(t, err, nil)
Equal(t, tt.String, "test2")
set.RegisterAlias("rep", "repl,repl2")
set.RegisterAlias("bad", "repl,,repl2")
type Test2 struct {
String string `r:"rep"`
}
var tt2 Test2
err = set.Struct(context.Background(), &tt2)
Equal(t, err, nil)
Equal(t, tt.String, "test2")
var s string
err = set.Field(context.Background(), &s, "bad")
NotEqual(t, err, nil)
// var s string
err = set.Field(context.Background(), &s, "repl,rep,bad")
NotEqual(t, err, nil)
}
func TestArray(t *testing.T) {
type Test struct {
Arr []string `s:"defaultArr,dive,defaultStr"`
}
set := mold.New()
set.SetTagName("s")
set.Register("defaultArr", func(ctx context.Context, fl mold.FieldLevel) error {
if mold.HasValue(fl.Field()) {
return nil
}
fl.Field().Set(reflect.MakeSlice(fl.Field().Type(), 2, 2))
return nil
})
set.Register("defaultStr", func(ctx context.Context, fl mold.FieldLevel) error {
if fl.Field().String() == "ok" {
return errors.New("ALREADY OK")
}
fl.Field().SetString("default")
return nil
})
var tt Test
err := set.Struct(context.Background(), &tt)
Equal(t, err, nil)
Equal(t, len(tt.Arr), 2)
Equal(t, tt.Arr[0], "default")
Equal(t, tt.Arr[1], "default")
tt2 := Test{
Arr: make([]string, 1),
}
err = set.Struct(context.Background(), &tt2)
Equal(t, err, nil)
Equal(t, len(tt2.Arr), 1)
Equal(t, tt2.Arr[0], "default")
tt3 := Test{
Arr: []string{"ok"},
}
err = set.Struct(context.Background(), &tt3)
NotEqual(t, err, nil)
Equal(t, err.Error(), "ALREADY OK")
}
func TestMap(t *testing.T) {
type Test struct {
Map map[string]string `s:"defaultMap,dive,defaultStr"`
}
set := mold.New()
set.SetTagName("s")
set.Register("defaultMap", func(ctx context.Context, fl mold.FieldLevel) error {
if mold.HasValue(fl.Field()) {
return nil
}
fl.Field().Set(reflect.MakeMap(fl.Field().Type()))
return nil
})
set.Register("defaultStr", func(ctx context.Context, fl mold.FieldLevel) error {
if fl.Field().String() == "ok" {
return errors.New("ALREADY OK")
}
fl.Field().SetString("default")
return nil
})
var tt Test
err := set.Struct(context.Background(), &tt)
Equal(t, err, nil)
Equal(t, len(tt.Map), 0)
tt2 := Test{
Map: map[string]string{"key": ""},
}
err = set.Struct(context.Background(), &tt2)
Equal(t, err, nil)
Equal(t, len(tt2.Map), 1)
Equal(t, tt2.Map["key"], "default")
tt3 := Test{
Map: map[string]string{"key": "ok"},
}
err = set.Struct(context.Background(), &tt3)
NotEqual(t, err, nil)
Equal(t, err.Error(), "ALREADY OK")
}
func TestInterface(t *testing.T) {
type Test struct {
Iface interface{} `s:"default"`
}
type Inner struct {
STR string
String string `s:"defaultStr"`
}
type Test2 struct {
Iface interface{} `s:"default2"`
}
type Inner2 struct {
String string `s:"error"`
}
set := mold.New()
set.SetTagName("s")
set.Register("default", func(ctx context.Context, fl mold.FieldLevel) error {
fl.Field().Set(reflect.ValueOf(Inner{STR: "test"}))
return nil
})
set.Register("default2", func(ctx context.Context, fl mold.FieldLevel) error {
fl.Field().Set(reflect.ValueOf(Inner2{}))
return nil
})
set.Register("defaultStr", func(ctx context.Context, fl mold.FieldLevel) error {
if mold.HasValue(fl.Field()) && fl.Field().String() == "ok" {
return errors.New("ALREADY OK")
}
fl.Field().Set(reflect.ValueOf("default"))
return nil
})
set.Register("error", func(ctx context.Context, fl mold.FieldLevel) error {
return errors.New("BAD VALUE")
})
var tt Test
err := set.Struct(context.Background(), &tt)
Equal(t, err, nil)
NotEqual(t, tt.Iface, nil)
inner, ok := tt.Iface.(Inner)
Equal(t, ok, true)
Equal(t, inner.String, "default")
Equal(t, inner.STR, "test")
var tt2 Test2
err = set.Struct(context.Background(), &tt2)
NotEqual(t, err, nil)
type Test3 struct {
Iface interface{} `s:"defaultStr"`
}
var tt3 Test3
tt3.Iface = "String"
err = set.Struct(context.Background(), &tt3)
Equal(t, err, nil)
Equal(t, tt3.Iface.(string), "default")
type Test4 struct {
Iface interface{} `s:"defaultStr,defaultStr"`
}
var tt4 Test4
tt4.Iface = nil
err = set.Struct(context.Background(), &tt4)
Equal(t, err, nil)
Equal(t, tt4.Iface.(string), "default")
type Test5 struct {
Iface interface{} `s:"defaultStr,error"`
}
var tt5 Test5
tt5.Iface = "String"
err = set.Struct(context.Background(), &tt5)
NotEqual(t, err, nil)
}
func TestInterfacePtr(t *testing.T) {
type Test struct {
Iface interface{} `s:"default"`
}
type Inner struct {
String string `s:"defaultStr"`
}
set := mold.New()
set.SetTagName("s")
set.Register("default", func(ctx context.Context, fl mold.FieldLevel) error {
fl.Field().Set(reflect.ValueOf(new(Inner)))
return nil
})
set.Register("defaultStr", func(ctx context.Context, fl mold.FieldLevel) error {
if fl.Field().String() == "ok" {
return errors.New("ALREADY OK")
}
fl.Field().SetString("default")
return nil
})
var tt Test
err := set.Struct(context.Background(), &tt)
Equal(t, err, nil)
NotEqual(t, tt.Iface, nil)
inner, ok := tt.Iface.(*Inner)
Equal(t, ok, true)
Equal(t, inner.String, "default")
type Test2 struct {
Iface interface{}
}
var tt2 Test2
tt2.Iface = Inner{}
err = set.Struct(context.Background(), &tt2)
Equal(t, err, nil)
}
func TestStructLevel(t *testing.T) {
type Test struct {
String string
}
set := mold.New()
set.RegisterStructLevel(func(ctx context.Context, sl mold.StructLevel) error {
s := sl.Struct().Interface().(Test)
if s.String == "error" {
return errors.New("BAD VALUE")
}
s.String = "test"
sl.Struct().Set(reflect.ValueOf(s))
return nil
}, Test{})
var tt Test
err := set.Struct(context.Background(), &tt)
Equal(t, err, nil)
Equal(t, tt.String, "test")
tt.String = "error"
err = set.Struct(context.Background(), &tt)
NotEqual(t, err, nil)
}
func TestTimeType(t *testing.T) {
var tt time.Time
set := mold.New()
set.Register("default", func(ctx context.Context, fl mold.FieldLevel) error {
fl.Field().Set(reflect.ValueOf(time.Now()))
return nil
})
err := set.Field(context.Background(), &tt, "default")
Equal(t, err, nil)
err = set.Field(context.Background(), &tt, "default,dive")
NotEqual(t, err, nil)
Equal(t, errors.Is(err, mold.ErrInvalidDive), true)
}
func TestParam(t *testing.T) {
type Test struct {
String string `r:"ltrim=#$_"`
}
set := mold.New()
set.SetTagName("r")
set.Register("ltrim", func(ctx context.Context, fl mold.FieldLevel) error {
fl.Field().SetString(strings.TrimLeft(fl.Field().String(), fl.Param()))
return nil
})
tt := Test{String: "_test"}
err := set.Struct(context.Background(), &tt)
Equal(t, err, nil)
Equal(t, tt.String, "test")
}
func TestDiveKeys(t *testing.T) {
type Test struct {
Map map[string]string `s:"dive,keys,default,endkeys,default"`
}
set := mold.New()
set.SetTagName("s")
set.Register("default", func(ctx context.Context, fl mold.FieldLevel) error {
fl.Field().Set(reflect.ValueOf("after"))
return nil
})
set.Register("err", func(ctx context.Context, fl mold.FieldLevel) error {
return errors.New("err")
})
test := Test{
Map: map[string]string{
"b4": "b4",
},
}
err := set.Struct(context.Background(), &test)
Equal(t, err, nil)
val := test.Map["after"]
Equal(t, val, "after")
m := map[string]string{
"b4": "b4",
}
err = set.Field(context.Background(), &m, "dive,keys,default,endkeys,default")
Equal(t, err, nil)
val = m["after"]
Equal(t, val, "after")
err = set.Field(context.Background(), &m, "keys,endkeys,default")
Equal(t, err, mold.ErrInvalidKeysTag)
err = set.Field(context.Background(), &m, "dive,endkeys,default")
Equal(t, err, mold.ErrUndefinedKeysTag)
err = set.Field(context.Background(), &m, "dive,keys,err,endkeys")
NotEqual(t, err, nil)
m = map[string]string{
"b4": "b4",
}
err = set.Field(context.Background(), &m, "dive,keys,default,endkeys,err")
NotEqual(t, err, nil)
}
func TestStructArray(t *testing.T) {
type InnerStruct struct {
String string `s:"defaultStr"`
}
type Test struct {
Inner InnerStruct
Arr []InnerStruct `s:"defaultArr"`
ArrDive []InnerStruct `s:"defaultArr,dive"`
ArrNoTag []InnerStruct
}
set := mold.New()
set.SetTagName("s")
set.Register("defaultArr", func(ctx context.Context, fl mold.FieldLevel) error {
if mold.HasValue(fl.Field()) {
return nil
}
fl.Field().Set(reflect.MakeSlice(fl.Field().Type(), 2, 2))
return nil
})
set.Register("defaultStr", func(ctx context.Context, fl mold.FieldLevel) error {
if fl.Field().String() == "ok" {
return errors.New("ALREADY OK")
}
fl.Field().SetString("default")
return nil
})
var tt Test
err := set.Struct(context.Background(), &tt)
Equal(t, err, nil)
Equal(t, len(tt.Arr), 2)
Equal(t, len(tt.ArrDive), 2)
Equal(t, tt.Arr[0].String, "")
Equal(t, tt.Arr[1].String, "")
Equal(t, tt.ArrDive[0].String, "default")
Equal(t, tt.ArrDive[1].String, "default")
Equal(t, tt.Inner.String, "default")
tt2 := Test{
Arr: make([]InnerStruct, 1),
}
err = set.Struct(context.Background(), &tt2)
Equal(t, err, nil)
Equal(t, len(tt2.Arr), 1)
Equal(t, tt2.Arr[0].String, "")
tt3 := Test{
Arr: []InnerStruct{{"ok"}},
}
err = set.Struct(context.Background(), &tt3)
Equal(t, err, nil)
Equal(t, len(tt3.Arr), 1)
Equal(t, tt3.Arr[0].String, "ok")
tt4 := Test{
ArrDive: []InnerStruct{{"ok"}},
}
err = set.Struct(context.Background(), &tt4)
NotEqual(t, err, nil)
Equal(t, err.Error(), "ALREADY OK")
tt5 := Test{
ArrNoTag: make([]InnerStruct, 1),
}
err = set.Struct(context.Background(), &tt5)
Equal(t, err, nil)
Equal(t, len(tt5.ArrNoTag), 1)
Equal(t, tt5.ArrNoTag[0].String, "")
}