-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresolver_test.go
1019 lines (869 loc) · 28 KB
/
resolver_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 owl_test
import (
"context"
"errors"
"fmt"
"os"
"reflect"
"testing"
"github.com/ggicci/owl"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type Pagination struct {
Page int `owl:"form=page"`
Size int `owl:"form=size"`
}
type User struct {
Name string `owl:"form=name"`
Gender string `owl:"form=gender;default=unknown"`
Birthday string `owl:"form=birthday"`
}
type UserSignUpForm struct {
User User `owl:"form=user"`
CSRFToken string `owl:"form=csrf_token"`
}
var expectedUserSignUpFormResolverTree = []*expectedResolver{
{
Index: []int{0},
LookupPath: "User",
NumFields: 3,
Directives: []*owl.Directive{
owl.NewDirective("form", "user"),
},
Leaf: false,
},
{
Index: []int{0, 0},
LookupPath: "User.Name",
NumFields: 0,
Directives: []*owl.Directive{
owl.NewDirective("form", "name"),
},
Leaf: true,
},
{
Index: []int{0, 1},
LookupPath: "User.Gender",
NumFields: 0,
Directives: []*owl.Directive{
owl.NewDirective("form", "gender"),
owl.NewDirective("default", "unknown"),
},
Leaf: true,
},
{
Index: []int{0, 2},
LookupPath: "User.Birthday",
NumFields: 0,
Directives: []*owl.Directive{
owl.NewDirective("form", "birthday"),
},
Leaf: true,
},
{
Index: []int{1},
LookupPath: "CSRFToken",
NumFields: 0,
Directives: []*owl.Directive{
owl.NewDirective("form", "csrf_token"),
},
Leaf: true,
},
}
type expectedResolver struct {
Index []int
LookupPath string
NumFields int
Directives []*owl.Directive
Leaf bool
}
type BuildResolverTreeTestSuite struct {
suite.Suite
expected []*expectedResolver
tree *owl.Resolver
}
func NewBuildResolverTreeTestSuite(tree *owl.Resolver, expected []*expectedResolver) *BuildResolverTreeTestSuite {
return &BuildResolverTreeTestSuite{
expected: expected,
tree: tree,
}
}
func (s *BuildResolverTreeTestSuite) Test_0_Lookup_IsLeaf() {
assert := assert.New(s.T())
for _, expected := range s.expected {
resolver := s.tree.Lookup(expected.LookupPath)
assert.NotNil(resolver)
assert.Equal(expected.Index, resolver.Index)
assert.Equal(expected.NumFields, len(resolver.Children))
assert.Equal(expected.Directives, resolver.Directives)
assert.Equal(expected.Leaf, resolver.IsLeaf())
}
assert.Nil(s.tree.Lookup("SomeNonExistingPath"))
}
func (s *BuildResolverTreeTestSuite) Test_1_GetDirective() {
assert := assert.New(s.T())
for _, expected := range s.expected {
resolver := s.tree.Lookup(expected.LookupPath)
for _, directive := range expected.Directives {
assert.Equal(directive, resolver.GetDirective(directive.Name))
assert.Nil(resolver.GetDirective("SomeNonExistingDirective"))
}
}
}
func TestNew_NormalCasesSuites(t *testing.T) {
tree, err := owl.New(Pagination{})
assert.NoError(t, err)
assert.NotNil(t, tree)
suite.Run(t, NewBuildResolverTreeTestSuite(
tree,
[]*expectedResolver{
{
Index: []int{0},
LookupPath: "Page",
NumFields: 0,
Directives: []*owl.Directive{
owl.NewDirective("form", "page"),
},
Leaf: true,
},
{
Index: []int{1},
LookupPath: "Size",
NumFields: 0,
Directives: []*owl.Directive{
owl.NewDirective("form", "size"),
},
Leaf: true,
},
},
))
tree, err = owl.New(UserSignUpForm{})
assert.NoError(t, err)
assert.NotNil(t, tree)
suite.Run(t, NewBuildResolverTreeTestSuite(
tree,
expectedUserSignUpFormResolverTree,
))
}
func TestNew_SkipFieldsHavingNoDirectives(t *testing.T) {
type AnotherForm struct {
Username string `owl:"form=username"`
Password string `owl:"form=password"`
Hidden string // should be ignored
Pagination *Pagination // should not be ignored
}
tree, err := owl.New(AnotherForm{})
assert.NotNil(t, tree)
assert.NoError(t, err)
suite.Run(t, NewBuildResolverTreeTestSuite(
tree,
[]*expectedResolver{
{
Index: []int{},
LookupPath: "",
NumFields: 3,
Directives: []*owl.Directive{},
Leaf: false,
},
{
Index: []int{0},
LookupPath: "Username",
Directives: []*owl.Directive{
owl.NewDirective("form", "username"),
},
Leaf: true,
},
{
Index: []int{1},
LookupPath: "Password",
NumFields: 0,
Directives: []*owl.Directive{
owl.NewDirective("form", "password"),
},
Leaf: true,
},
{
Index: []int{3}, // Pagination is the 4th field, Hidden is the 3rd field.
LookupPath: "Pagination",
NumFields: 2,
Directives: []*owl.Directive{},
Leaf: false,
},
{
Index: []int{3, 0},
LookupPath: "Pagination.Page",
NumFields: 0,
Directives: []*owl.Directive{
owl.NewDirective("form", "page"),
},
Leaf: true,
},
{
Index: []int{3, 1},
LookupPath: "Pagination.Size",
NumFields: 0,
Directives: []*owl.Directive{
owl.NewDirective("form", "size"),
},
Leaf: true,
},
},
))
}
func TestNew_WithNilType(t *testing.T) {
_, err := owl.New(nil)
assert.ErrorIs(t, err, owl.ErrUnsupportedType)
assert.ErrorContains(t, err, "nil type")
}
func TestNew_WithPointer(t *testing.T) {
_, err := owl.New(&Pagination{})
assert.NoError(t, err)
}
func TestNew_WithNonStruct(t *testing.T) {
_, err := owl.New(123)
assert.ErrorIs(t, err, owl.ErrUnsupportedType)
assert.ErrorContains(t, err, "non-struct type")
}
func TestNew_WithRecursion(t *testing.T) {
type RecursiveRef struct {
Loop *RecursiveRef
}
_, err := owl.New(RecursiveRef{})
assert.NoError(t, err)
}
func TestNew_ParsingDirectives_InvalidName(t *testing.T) {
resolver, err := owl.New(struct {
Invalid string `owl:"invalid/name"`
}{})
assert.Nil(t, resolver)
assert.ErrorContains(t, err, "parse directives")
assert.ErrorContains(t, err, "build resolver for \"Invalid\" failed:")
assert.ErrorIs(t, err, owl.ErrInvalidDirectiveName)
}
func TestNew_ParsingDirectives_DuplicateDirectives(t *testing.T) {
resolver, err := owl.New(struct {
Color string `owl:"form=red;form=blue"`
}{})
assert.Nil(t, resolver)
assert.ErrorContains(t, err, "parse directives")
assert.ErrorContains(t, err, "build resolver for \"Color\" failed:")
assert.ErrorIs(t, err, owl.ErrDuplicateDirective)
}
func TestNew_OptionNilNamespace(t *testing.T) {
resolver, err := owl.New(struct{}{}, owl.WithNamespace(nil))
assert.Nil(t, resolver)
assert.ErrorContains(t, err, "nil namespace")
}
func TestNew_OptionCustomValue(t *testing.T) {
resolver, err := owl.New(Pagination{}, owl.WithValue("hello", "world"))
assert.NotNil(t, resolver)
assert.NoError(t, err)
resolver.Iterate(func(r *owl.Resolver) error {
assert.Equal(t, "world", r.Context.Value("hello"))
return nil
})
}
func TestNew_copyCachedResolver(t *testing.T) {
assert := assert.New(t)
type Appearance struct {
Color string `owl:"form=color"`
Size string `owl:"form=size"`
}
type Settings struct {
Profile string `owl:"form=profile"`
Appearance *Appearance `owl:"form=appearance"`
}
ns1 := owl.NewNamespace()
ns1.RegisterDirectiveExecutor("stdin", owl.DirectiveExecutorFunc(exeNoop))
r1, err := owl.New(Settings{}, owl.WithNamespace(ns1))
assert.NoError(err)
ns2 := owl.NewNamespace()
ns2.RegisterDirectiveExecutor("form", owl.DirectiveExecutorFunc(exeNoop))
r2, err := owl.New(Settings{}, owl.WithNamespace(ns2))
assert.NoError(err)
assert.Equal(ns1, r1.Namespace())
assert.Equal(ns2, r2.Namespace())
for _, lookupName := range []string{
"Profile",
"Appearance",
"Appearance.Color",
"Appearance.Size",
} {
assert.NotEqual(r1.Lookup(lookupName), r2.Lookup(lookupName))
}
assert.Equal(r1.Lookup("Profile").Parent, r1)
assert.Equal(r1.Lookup("Appearance").Parent, r1)
assert.Equal(r1.Lookup("Appearance.Color").Parent, r1.Lookup("Appearance"))
assert.Equal(r1.Lookup("Appearance.Size").Parent, r1.Lookup("Appearance"))
assert.Equal(r2.Lookup("Profile").Parent, r2)
assert.Equal(r2.Lookup("Appearance").Parent, r2)
assert.Equal(r2.Lookup("Appearance.Color").Parent, r2.Lookup("Appearance"))
assert.Equal(r2.Lookup("Appearance.Size").Parent, r2.Lookup("Appearance"))
}
func TestRemoveDirective(t *testing.T) {
type User struct {
Name string `owl:"form=name;query=name;header=X-Name;required"`
}
resolver, err := owl.New(User{})
assert.NoError(t, err)
form := resolver.RemoveDirective("form")
assert.Nil(t, form)
nameResolver := resolver.Lookup("Name")
assert.NotNil(t, nameResolver)
form = nameResolver.RemoveDirective("form")
assert.NotNil(t, form)
assert.Equal(t, "form", form.Name)
assert.Equal(t, []string{"name"}, form.Argv)
assert.Nil(t, nameResolver.GetDirective("form"))
required := nameResolver.RemoveDirective("required")
assert.NotNil(t, required)
assert.Equal(t, "required", required.Name)
assert.Len(t, required.Argv, 0)
assert.Nil(t, nameResolver.GetDirective("required"))
}
func TestResolve_SimpleFlatStruct(t *testing.T) {
assert := assert.New(t)
ns, tracker := createNsForTracking()
type GenerateAccessTokenRequest struct {
Key string `owl:"env=ACCESS_TOKEN_KEY_GENERATION_KEY"`
UserName string `owl:"form=username"`
Expiry int `owl:"form=expiry;default=3600"`
}
resolver, err := owl.New(GenerateAccessTokenRequest{}, owl.WithNamespace(ns))
assert.NoError(err)
expectedExecutedDirectives := []*owl.Directive{
owl.NewDirective("env", "ACCESS_TOKEN_KEY_GENERATION_KEY"),
owl.NewDirective("form", "username"),
owl.NewDirective("form", "expiry"),
owl.NewDirective("default", "3600"),
}
// Resolve
_, err = resolver.Resolve()
assert.NoError(err)
assert.Equal(expectedExecutedDirectives, tracker.Executed.ExecutedDirectives(), "should execute all directives in order")
// ResolveTo
tracker.Reset()
var targetValue = new(GenerateAccessTokenRequest)
err = resolver.ResolveTo(targetValue)
assert.NoError(err)
assert.Equal(expectedExecutedDirectives, tracker.Executed.ExecutedDirectives(), "should execute all directives in order")
}
func TestResolve_EmbeddedStruct(t *testing.T) {
assert := assert.New(t)
ns, tracker := createNsForTracking()
type UserFilter struct {
Gender string `owl:"form=gender"`
Ages []int `owl:"form=age,age[];default=18,999"`
Roles []string `owl:"form=roles,roles[]"`
}
type UserListQuery struct {
UserFilter
Pagination
}
resolver, err := owl.New(UserListQuery{}, owl.WithNamespace(ns))
assert.NoError(err)
expectedExecutedDirectives := []*owl.Directive{
owl.NewDirective("form", "gender"),
owl.NewDirective("form", "age", "age[]"),
owl.NewDirective("default", "18", "999"),
owl.NewDirective("form", "roles", "roles[]"),
owl.NewDirective("form", "page"),
owl.NewDirective("form", "size"),
}
// Resolve
_, err = resolver.Resolve()
assert.NoError(err)
assert.Equal(expectedExecutedDirectives, tracker.Executed.ExecutedDirectives(), "should execute all directives in order")
// ResolveTo
tracker.Reset()
var targetValue = new(UserListQuery)
err = resolver.ResolveTo(targetValue)
assert.NoError(err)
assert.Equal(expectedExecutedDirectives, tracker.Executed.ExecutedDirectives(), "should execute all directives in order")
}
func TestResolve_UnexportedField(t *testing.T) {
type User struct {
Name string `owl:"env=OWL_TEST_NAME"`
age int // should be ignored
Gender string `owl:"env=OWL_TEST_GENDER"`
}
ns := owl.NewNamespace()
ns.RegisterDirectiveExecutor("env", owl.DirectiveExecutorFunc(exeEnvReader))
resolver, err := owl.New(User{}, owl.WithNamespace(ns))
assert.NotNil(t, resolver)
assert.NoError(t, err)
// Set environment variables.
os.Setenv("OWL_TEST_NAME", "owl")
os.Setenv("OWL_TEST_GENDER", "male")
// Resolve.
gotValue, err := resolver.Resolve()
assert.NoError(t, err)
gotUser, ok := gotValue.Interface().(*User)
assert.True(t, ok)
assert.Equal(t, "owl", gotUser.Name)
assert.Equal(t, "male", gotUser.Gender)
os.Clearenv()
}
func TestResolve_MissingExecutor(t *testing.T) {
ns := owl.NewNamespace()
resolver, err := owl.New(Pagination{}, owl.WithNamespace(ns))
assert.NotNil(t, resolver)
assert.NoError(t, err)
_, err = resolver.Resolve()
assert.ErrorContains(t, err, "resolve field \"Page (int)\" failed:")
assert.ErrorIs(t, err, owl.ErrMissingExecutor)
}
func TestResolve_DirectiveExecutionFailure(t *testing.T) {
assert := assert.New(t)
ns := owl.NewNamespace()
var errExecutionFailed = errors.New("directive execution failed")
ns.RegisterDirectiveExecutor("error", owl.DirectiveExecutorFunc(func(dr *owl.DirectiveRuntime) error {
return errExecutionFailed
}))
type Request struct {
Name string `owl:"error"`
}
resolver, err := owl.New(Request{}, owl.WithNamespace(ns))
assert.NoError(err)
rv, err := resolver.Resolve()
assert.NotNil(rv)
resolveError := new(owl.ResolveError)
assert.ErrorAs(err, &resolveError)
assert.Equal("Name", resolveError.Resolver.Field.Name)
directiveExecutionError := new(owl.DirectiveExecutionError)
assert.ErrorAs(err, &directiveExecutionError)
assert.Equal(directiveExecutionError, resolveError.AsDirectiveExecutionError())
assert.Equal("error", directiveExecutionError.Directive.Name)
assert.Len(directiveExecutionError.Directive.Argv, 0)
assert.ErrorContains(err, "execute directive \"error\" with args [] failed:")
assert.ErrorContains(err, "directive execution failed")
assert.ErrorIs(err, errExecutionFailed)
}
func TestResolve_DirectiveRuntimeContext(t *testing.T) {
type contextKey int
const ckSet contextKey = 1
exeSetField := func(dr *owl.DirectiveRuntime) error {
dr.Context = context.WithValue(dr.Context, ckSet, true)
return nil
}
exeUnsetField := func(dr *owl.DirectiveRuntime) error {
dr.Context = context.WithValue(dr.Context, ckSet, false)
return nil
}
exeRequired := func(dr *owl.DirectiveRuntime) error {
alreadySet := dr.Context.Value(ckSet)
if alreadySet == nil || !alreadySet.(bool) {
return errors.New("field is required")
}
return nil
}
ns := owl.NewNamespace()
ns.RegisterDirectiveExecutor("set", owl.DirectiveExecutorFunc(exeSetField))
ns.RegisterDirectiveExecutor("unset", owl.DirectiveExecutorFunc(exeUnsetField))
ns.RegisterDirectiveExecutor("required", owl.DirectiveExecutorFunc(exeRequired))
type RequestSet struct {
// In required directive, the context value of ckSet should be true here.
Name string `owl:"set;required"`
}
resolver, err := owl.New(RequestSet{}, owl.WithNamespace(ns))
assert.NoError(t, err)
_, err = resolver.Resolve()
assert.NoError(t, err)
type RequestUnset struct {
// In required directive, the context value of ckSet should be false here.
// Thus, the required directive should fail.
Name string `owl:"unset;required"`
}
resolver, err = owl.New(RequestUnset{}, owl.WithNamespace(ns))
assert.NoError(t, err)
_, err = resolver.Resolve()
assert.ErrorContains(t, err, "field is required")
}
func TestResolve_NestedDirectives(t *testing.T) {
type User struct {
Name string `owl:"env=OWL_TEST_NAME"`
Role string `owl:"env=OWL_TEST_ROLE"`
}
type Request struct {
// NOTE: Login will be created and updated by login directive,
// and its fields will also be updated by env directive.
Login User `owl:"login"`
Action string `owl:"env=OWL_TEST_ACTION"`
}
expected := &Request{
Login: User{
Name: "owl", // set by env
Role: "admin", // set by login
},
Action: "addAccount",
}
ns := owl.NewNamespace()
ns.RegisterDirectiveExecutor("env", owl.DirectiveExecutorFunc(exeEnvReader))
ns.RegisterDirectiveExecutor("login", owl.DirectiveExecutorFunc(func(dr *owl.DirectiveRuntime) error {
u := User{Name: "hello", Role: "admin"}
dr.Value.Elem().Set(reflect.ValueOf(u))
return nil
}))
os.Setenv("OWL_TEST_NAME", "owl")
os.Setenv("OWL_TEST_ACTION", "addAccount")
resolver, err := owl.New(Request{}, owl.WithNamespace(ns))
assert.NoError(t, err)
gotValue, err := resolver.Resolve()
assert.NoError(t, err)
assert.Equal(t, expected, gotValue.Interface().(*Request))
os.Clearenv()
}
func TestResolve_WithNestedDirectivesEnabled_false(t *testing.T) {
assert := assert.New(t)
ns, tracker := createNsForTracking()
tree, err := owl.New(
UserSignUpForm{},
owl.WithNamespace(ns),
owl.WithNestedDirectivesEnabled(false), // disable resolving nested directives
)
assert.NoError(err)
assert.NotNil(tree)
suite.Run(t, NewBuildResolverTreeTestSuite(
tree, expectedUserSignUpFormResolverTree,
))
// Won't resolve nested directives because WithNestedDirectivesEnabled(false).
_, err = tree.Resolve()
assert.NoError(err)
assert.Equal([]*owl.Directive{
owl.NewDirective("form", "user"),
owl.NewDirective("form", "csrf_token"),
}, tracker.Executed.ExecutedDirectives(), "should not resolve nested directives")
// The value set in New will be overridden by the value set in Resolve or Scan.
tracker.Reset()
_, err = tree.Resolve(owl.WithNestedDirectivesEnabled(true)) // override
assert.NoError(err)
assert.Equal([]*owl.Directive{
owl.NewDirective("form", "user"),
// User nested directives.
owl.NewDirective("form", "name"),
owl.NewDirective("form", "gender"),
owl.NewDirective("default", "unknown"),
owl.NewDirective("form", "birthday"),
owl.NewDirective("form", "csrf_token"),
}, tracker.Executed.ExecutedDirectives(), "should resolve nested directives")
}
func TestResolveTo_InstantializeOnlyNilPointerForNestedStruct(t *testing.T) {
type Owner struct {
Type string `owl:"env=type"`
Name string `owl:"env=name"`
}
type AddOwnershipRequest struct {
ResourceId string `owl:"env=resource_id"`
Owner *Owner
}
ns := owl.NewNamespace()
ns.RegisterDirectiveExecutor("env", owl.DirectiveExecutorFunc(exeEnvReader))
resolver, err := owl.New(AddOwnershipRequest{}, owl.WithNamespace(ns))
assert.NoError(t, err)
os.Setenv("type", "usergroup")
os.Setenv("name", "admin")
os.Setenv("resource_id", "123")
useOwner := &Owner{}
reqWithOwnerInstantiated := &AddOwnershipRequest{
ResourceId: "",
Owner: useOwner,
}
err = resolver.ResolveTo(reqWithOwnerInstantiated)
assert.NoError(t, err)
// The Owner field is already instantiated, so we only populate the fields,
// but not create a new instance and assign it to the Owner field.
assert.Same(t, useOwner, reqWithOwnerInstantiated.Owner)
assert.Equal(t, "usergroup", reqWithOwnerInstantiated.Owner.Type)
assert.Equal(t, "admin", reqWithOwnerInstantiated.Owner.Name)
assert.Equal(t, "123", reqWithOwnerInstantiated.ResourceId)
// The Owner field is nil, so we create a new instance when resolving.
reqWithOwnerNotInstantiated := &AddOwnershipRequest{
ResourceId: "",
Owner: nil,
}
err = resolver.ResolveTo(reqWithOwnerNotInstantiated)
assert.NoError(t, err)
assert.Equal(t, &Owner{Type: "usergroup", Name: "admin"}, reqWithOwnerNotInstantiated.Owner)
assert.Equal(t, "123", reqWithOwnerNotInstantiated.ResourceId)
os.Clearenv()
}
func TestResolveTo_PopulateFieldsOnDemand(t *testing.T) {
type User struct {
Name string `owl:"env=OWL_TEST_NAME"`
}
ns := owl.NewNamespace()
ns.RegisterDirectiveExecutor("env", owl.DirectiveExecutorFunc(exeEnvReader))
resolver, err := owl.New(User{}, owl.WithNamespace(ns))
assert.NoError(t, err)
user := &User{Name: "admin"}
err = resolver.ResolveTo(user)
assert.NoError(t, err)
assert.Equal(t, "admin", user.Name) // not changed
os.Setenv("OWL_TEST_NAME", "owl")
err = resolver.ResolveTo(user)
assert.NoError(t, err)
assert.Equal(t, "owl", user.Name) // changed
os.Clearenv()
}
func TestRevoleTo_MultiLevelPointer(t *testing.T) {
type User struct {
Name string `owl:"env=OWL_TEST_NAME"`
}
ns := owl.NewNamespace()
ns.RegisterDirectiveExecutor("env", owl.DirectiveExecutorFunc(exeEnvReader))
resolver, err := owl.New(User{}, owl.WithNamespace(ns))
assert.NoError(t, err)
var user = new(User)
// *T
os.Setenv("OWL_TEST_NAME", "owl")
assert.NoError(t, resolver.ResolveTo(user))
assert.Equal(t, "owl", user.Name)
// **T
os.Setenv("OWL_TEST_NAME", "golang")
assert.NoError(t, resolver.ResolveTo(&user))
assert.Equal(t, "golang", user.Name)
// ***T
var userPtr = &user
os.Setenv("OWL_TEST_NAME", "world")
assert.NoError(t, resolver.ResolveTo(&userPtr))
assert.Equal(t, "world", user.Name)
assert.Same(t, *userPtr, user)
os.Clearenv()
}
func TestResolveTo_ErrNilValue(t *testing.T) {
resolver, err := owl.New(User{})
assert.NoError(t, err)
err = resolver.ResolveTo(nil)
assert.ErrorContains(t, err, "nil")
assert.ErrorIs(t, err, owl.ErrInvalidResolveTarget)
err = resolver.ResolveTo((*User)(nil))
assert.ErrorContains(t, err, "nil pointer")
assert.ErrorIs(t, err, owl.ErrInvalidResolveTarget)
}
func TestResolveTo_ErrNonPointerValue(t *testing.T) {
resolver, err := owl.New(User{})
assert.NoError(t, err)
var user User
err = resolver.ResolveTo(user)
assert.ErrorContains(t, err, "non-pointer")
assert.ErrorIs(t, err, owl.ErrInvalidResolveTarget)
}
func TestResolveTo_ErrTypeMismatch(t *testing.T) {
resolver, err := owl.New(User{})
assert.NoError(t, err)
var user = new(Pagination)
err = resolver.ResolveTo(user)
assert.ErrorIs(t, err, owl.ErrTypeMismatch)
assert.ErrorIs(t, err, owl.ErrInvalidResolveTarget)
}
func TestScan(t *testing.T) {
ns, tracker := createNsForTracking()
resolver, err := owl.New(User{}, owl.WithNamespace(ns))
assert.NoError(t, err)
user := &User{
Name: "Ggicci",
Gender: "male",
Birthday: "1991-11-10",
}
expected := ExecutedDataList{
{owl.NewDirective("form", "name"), "Ggicci"},
{owl.NewDirective("form", "gender"), "male"},
{owl.NewDirective("default", "unknown"), "male"},
{owl.NewDirective("form", "birthday"), "1991-11-10"},
}
// scan on pointer value
err = resolver.Scan(user)
assert.NoError(t, err)
assert.Equal(t, expected, tracker.Executed)
// scan on non-pointer value
tracker.Reset()
err = resolver.Scan(*user)
assert.NoError(t, err)
assert.Equal(t, expected, tracker.Executed)
}
func TestScan_withOpts(t *testing.T) {
ns, _ := createNsForTrackingWithContextVerifier(&ContextVerifier{"hello", "world"})
resolver, err := owl.New(User{}, owl.WithNamespace(ns))
assert.NoError(t, err)
err = resolver.Scan(User{}, owl.WithValue("hello", "world"))
assert.NoError(t, err)
err = resolver.Scan(User{}, owl.WithValue("hello", "golang"))
assert.ErrorContains(t, err, "unexpected context value")
}
func TestScan_overrideNamespace(t *testing.T) {
resolver, err := owl.New(User{}) // using default namespace
assert.NoError(t, err)
err = resolver.Scan(User{})
assert.Error(t, err) // should fail because default namespace has no directive executors
ns, _ := createNsForTracking()
err = resolver.Scan(User{}, owl.WithNamespace(ns))
assert.NoError(t, err) // should success because namespace is overrided
}
func TestScan_onNilValue(t *testing.T) {
resolver, err := owl.New(User{})
assert.NoError(t, err)
err = resolver.Scan(nil)
assert.ErrorContains(t, err, "nil")
}
func TestScan_ErrTypeMismatch(t *testing.T) {
resolver, err := owl.New(User{})
assert.NoError(t, err)
err = resolver.Scan(Pagination{})
assert.ErrorIs(t, err, owl.ErrTypeMismatch)
}
func TestScan_ErrMissingExecutor(t *testing.T) {
resolver, err := owl.New(User{})
assert.NoError(t, err)
err = resolver.Scan(User{})
assert.ErrorIs(t, err, owl.ErrMissingExecutor)
assert.Len(t, err.(interface{ Unwrap() []error }).Unwrap(), 3)
}
func TestScan_NestedDirectives(t *testing.T) {
ns, tracker := createNsForTracking()
resolver, err := owl.New(UserSignUpForm{}, owl.WithNamespace(ns))
assert.NoError(t, err)
form := &UserSignUpForm{
User: User{
Name: "Ggicci",
Gender: "male",
Birthday: "1991-11-10",
},
CSRFToken: "123456",
}
expected := ExecutedDataList{
{owl.NewDirective("form", "user"), form.User}, // nested struct
{owl.NewDirective("form", "name"), "Ggicci"},
{owl.NewDirective("form", "gender"), "male"},
{owl.NewDirective("default", "unknown"), "male"},
{owl.NewDirective("form", "birthday"), "1991-11-10"},
{owl.NewDirective("form", "csrf_token"), "123456"},
}
err = resolver.Scan(form)
assert.NoError(t, err)
assert.Equal(t, expected, tracker.Executed)
}
func TestScan_WithNestedDirectivesEnabled_false(t *testing.T) {
ns, tracker := createNsForTracking()
resolver, err := owl.New(
UserSignUpForm{},
owl.WithNamespace(ns),
owl.WithNestedDirectivesEnabled(false), // disable resolving nested directives
)
assert.NoError(t, err)
form := &UserSignUpForm{
User: User{
Name: "Ggicci",
Gender: "male",
Birthday: "1991-11-10",
},
CSRFToken: "123456",
}
expected := ExecutedDataList{
{owl.NewDirective("form", "user"), form.User},
// Nested directives are not resolved:
// {owl.NewDirective("form", "name"), "Ggicci"},
// ...
{owl.NewDirective("form", "csrf_token"), "123456"},
}
err = resolver.Scan(form)
assert.NoError(t, err)
assert.Equal(t, expected, tracker.Executed)
}
func TestScan_NestedDirectives_ScanErrors_executeDirectiveFailed(t *testing.T) {
ns, _ := createNsForTrackingWithError(errors.New("TestScan_NestedDirectives_ScanErrors"))
resolver, err := owl.New(UserSignUpForm{}, owl.WithNamespace(ns))
assert.NoError(t, err)
form := &UserSignUpForm{
User: User{
Name: "Ggicci",
Gender: "male",
Birthday: "1991-11-10",
},
CSRFToken: "123456",
}
err = resolver.Scan(form)
assert.Error(t, err)
assert.ErrorContains(t, err, "TestScan_NestedDirectives_ScanErrors")
assert.Len(t, err.(interface{ Unwrap() []error }).Unwrap(), 5)
}
func TestScan_NestedDirectives_ScanErrors_ErrScanNilField(t *testing.T) {
type MyUserSignUpForm struct {
User *User
Token string
}
resolver, err := owl.New(MyUserSignUpForm{})
assert.NoError(t, err)
form := &MyUserSignUpForm{User: nil, Token: "123456"}
err = resolver.Scan(form)
assert.ErrorIs(t, err, owl.ErrScanNilField)
assert.Len(t, err.(interface{ Unwrap() []error }).Unwrap(), 3) // User has 3 fields that defined owl directives.
}
func TestIterate(t *testing.T) {
resolver, err := owl.New(UserSignUpForm{})
assert.NoError(t, err)
type contextKey int
const ckHello contextKey = 1
callback := func(r *owl.Resolver) error {
r.Context = context.WithValue(r.Context, ckHello, "world")
return nil
}
resolver.Iterate(callback)
assert.Equal(t, "world", resolver.Lookup("User").Context.Value(ckHello))
assert.Equal(t, "world", resolver.Lookup("User.Name").Context.Value(ckHello))
assert.Equal(t, "world", resolver.Lookup("User.Gender").Context.Value(ckHello))
assert.Equal(t, "world", resolver.Lookup("User.Birthday").Context.Value(ckHello))
assert.Equal(t, "world", resolver.Lookup("CSRFToken").Context.Value(ckHello))
}
func TestIterate_CallbackFail(t *testing.T) {
resolver, err := owl.New(UserSignUpForm{})
assert.NoError(t, err)
type contextKey int
const ckHello contextKey = 1
callback := func(r *owl.Resolver) error {
if r.Field.Name == "Gender" {
return errors.New("callback failed")
}
r.Context = context.WithValue(r.Context, ckHello, "world")
return nil
}
err = resolver.Iterate(callback)
assert.ErrorContains(t, err, "callback failed")
assert.Equal(t, "world", resolver.Lookup("User").Context.Value(ckHello))
assert.Equal(t, "world", resolver.Lookup("User.Name").Context.Value(ckHello))
assert.Equal(t, nil, resolver.Lookup("User.Gender").Context.Value(ckHello))
assert.Equal(t, nil, resolver.Lookup("User.Birthday").Context.Value(ckHello))
assert.Equal(t, nil, resolver.Lookup("CSRFToken").Context.Value(ckHello))
}
func TestWithNestedDirectivesEnabled_definitionOfNestedDirectives(t *testing.T) {
type CreateUserRequest struct {
ApiVersion string `owl:"form=api_version"`
User
}
ns, tracker := createNsForTracking()
resolver, err := owl.New(CreateUserRequest{}, owl.WithNamespace(ns))
assert.NoError(t, err)
resolver.Resolve(owl.WithNestedDirectivesEnabled(false))
assert.Equal(t, []*owl.Directive{
owl.NewDirective("form", "api_version"),
// Below are NOT nested directives. Because the field User has no directives.
// If we added any directive to User, then the directives below will be nested directives.
// Ex: User `owl:"form=user"`
owl.NewDirective("form", "name"),
owl.NewDirective("form", "gender"),