-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparsetag_test.go
105 lines (96 loc) · 2.68 KB
/
parsetag_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
package reflectutils_test
import (
"encoding/json"
"reflect"
"testing"
"github.com/muir/reflectutils"
"github.com/stretchr/testify/assert"
)
func TestSplitTag(t *testing.T) {
ts(t, `env:"foo"`, "env", "foo")
ts(t, `env:"YO" flag:"foo,bar"`, "env", "YO", "flag", "foo,bar")
ts(t, ``)
ts(t, `env:"YO" flag:"foo\",bar"`, "env", "YO", "flag", `foo\",bar`)
ts(t, `env:"YO" flag:"foo,bar" `, "env", "YO", "flag", "foo,bar")
}
func ts(t *testing.T, tag reflect.StructTag, want ...string) {
t.Log(tag)
tags := reflectutils.SplitTag(tag)
s := make([]string, 0, len(tags)*2)
if len(tags) == 0 {
s = nil
}
for _, tag := range tags {
s = append(s, tag.Tag, tag.Value)
}
assert.Equal(t, want, s, tag)
}
func TestTagGet(t *testing.T) {
tg(t, `env:"YO" flag:"foo,bar"`, "env", "YO")
}
func tg(t *testing.T, tags reflect.StructTag, name string, value string) {
t.Log(tags)
tag := reflectutils.GetTag(tags, name)
assert.Equal(t, name, tag.Tag, "name")
assert.Equal(t, value, tag.Value, "value")
}
func TestFill(t *testing.T) {
cases := []struct {
tests interface{}
model interface{}
targetTag string
metaTag string
}{
{
tests: struct {
T1 string `xyz:"a b" want:"{\"P0\":[\"a\",\"b\"]}"`
}{},
model: struct {
P0 []string `tf:"0,split=space" json:",omitempty"`
}{},
targetTag: "xyz",
metaTag: "tf",
},
{
tests: struct {
T2 string `xyz:"a,first" want:"{\"Name\":\"a\",\"First\":true}"`
T3 string `xyz:"a,last" want:"{\"Name\":\"a\",\"First\":false}"`
T4 string `xyz:"a,!first" want:"{\"Name\":\"a\",\"First\":false}"`
T5 string `xyz:"a,!last" want:"{\"Name\":\"a\",\"First\":true}"`
}{},
model: struct {
Name string `pf:"0" json:",omitempty"`
First *bool `pf:"first,!last" json:",omitempty"`
}{},
targetTag: "xyz",
metaTag: "pf",
},
}
for _, tc := range cases {
tc := tc
reflectutils.WalkStructElements(reflect.TypeOf(tc.tests), func(f reflect.StructField) bool {
if tc.metaTag == "" {
tc.metaTag = "pt"
}
if tc.targetTag == "" {
tc.targetTag = "xyz"
}
t.Run(f.Name+"_"+tc.targetTag+"_"+tc.metaTag,
func(t *testing.T) {
t.Logf("%s: %s", f.Name, f.Tag)
got := reflect.New(reflect.TypeOf(tc.model)).Interface()
err := reflectutils.SplitTag(f.Tag).Set().Get(tc.targetTag).Fill(got, reflectutils.WithTag(tc.metaTag))
if !assert.NoErrorf(t, err, "extract tag %s", f.Name) {
return
}
want := reflect.New(reflect.TypeOf(tc.model)).Interface()
err = json.Unmarshal([]byte(f.Tag.Get("want")), want)
if !assert.NoErrorf(t, err, "extract want %s", f.Name) {
return
}
assert.Equal(t, want, got, f.Name)
})
return true
})
}
}