-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield_walker_test.go
215 lines (187 loc) · 4.05 KB
/
field_walker_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
package conv
import (
"reflect"
"testing"
)
func TestFieldWalker_WalkFields(t *testing.T) {
type want struct {
Name string
Path string
Index []int
Tag string
}
check := func(t *testing.T, walker *FieldWalker, ws []want) {
i := 0
walker.WalkFields(func(f FieldInfo) bool {
if i > len(ws)-1 {
t.Fatalf("too many fields, index: %d", i)
}
w := ws[i]
if f.Name != w.Name {
t.Fatalf("want name %s, got %s", w.Name, f.Name)
}
if f.Path != w.Path {
t.Fatalf("want path %s, got %s", w.Path, f.Path)
}
if !reflect.DeepEqual(f.Index, w.Index) {
t.Fatalf("%s: want index %v, got %v", w.Name, w.Index, f.Index)
}
if f.TagValue != w.Tag {
t.Fatalf("want tag %s, got %s", w.Tag, f.TagValue)
}
i++
return true
})
if i != len(ws) {
t.Fatalf("not enough fields")
}
}
t.Run("empty", func(t *testing.T) {
walker := NewFieldWalker(reflect.TypeOf(struct{}{}), "")
count := 0
walker.WalkFields(func(fi FieldInfo) bool {
count++
return true
})
if count != 0 {
t.FailNow()
}
})
t.Run("top2", func(t *testing.T) {
type s struct{ X, Y, Z int }
walker := NewFieldWalker(reflect.TypeOf(s{}), "")
count := 0
walker.WalkFields(func(fi FieldInfo) bool {
count++
return count < 2
})
if count != 2 {
t.FailNow()
}
})
t.Run("without-tag", func(t *testing.T) {
type Ec struct {
D int
x int //lint:ignore U1000 Test unexported fields.
}
type Eb struct {
B int // hided by T.B
Ec // hided by T.Ec.D
C int
}
type T struct {
A int
Eb `conv:"X"` // the tag will not be processed
B string // hides Eb.B
Ec
}
walker := NewFieldWalker(reflect.TypeOf(T{}), "")
check(t, walker, []want{
{"A", "A", []int{0}, ""},
{"B", "B", []int{2}, ""},
{"C", "Eb.C", []int{1, 2}, ""},
{"D", "Ec.D", []int{3, 0}, ""},
})
})
t.Run("with-tag", func(t *testing.T) {
type A struct {
A int
X int // hided by T.B
}
type B struct {
B1 int // absent
B2 int // absent
}
type T struct {
*A
s int `c:"V"` //lint:ignore U1000 Test unexported fields.
B `c:"X"` // hides B.X, the traverse will not go into the field
}
walker := NewFieldWalker(reflect.TypeOf(T{}), "c")
check(t, walker, []want{
{"B", "B", []int{2}, "X"},
{"A", "A.A", []int{0, 0}, ""},
})
})
}
func TestFieldWalker_WalkValues(t *testing.T) {
type want struct {
Value int // 0 if the field isn't int.
Path string
}
check := func(t *testing.T, walker *FieldWalker, val reflect.Value, ws []want) {
i := 0
walker.WalkValues(val, func(f FieldInfo, v reflect.Value) bool {
if i > len(ws)-1 {
t.Errorf("too many fields, index: %d", i)
}
w := ws[i]
if v.Kind() == reflect.Int && v.Interface() != w.Value {
t.Errorf("index %d %s, want value %v, got %v", i, f.Name, w.Value, v.Interface())
}
if f.Path != w.Path {
t.Errorf("want path %s, got %s", w.Path, f.Path)
}
i++
return true
})
if i != len(ws) {
t.Fatalf("not enough fields")
}
}
t.Run("nil", func(t *testing.T) {
var a *struct{}
walker := NewFieldWalker(reflect.TypeOf(a), "")
walker.WalkValues(reflect.ValueOf(a), func(fi FieldInfo, v reflect.Value) bool {
t.FailNow()
return true
})
})
t.Run("top1", func(t *testing.T) {
var a struct{ X, Y int }
walker := NewFieldWalker(reflect.TypeOf(a), "")
count := 0
walker.WalkValues(reflect.ValueOf(a), func(fi FieldInfo, v reflect.Value) bool {
count++
return count < 1
})
if count != 1 {
t.FailNow()
}
})
t.Run("all", func(t *testing.T) {
type A struct {
A1 int
A2 int
x int
}
type B struct {
A // hided by C.A
B1 int
A1 int // hided by C.A.A1
}
type C struct {
C1 int
C2 int
*A // nil pointer will be ignored, hides B.A
*B
D struct{ X, Y int }
}
c := &C{
C1: 1,
C2: 2,
B: &B{
A: A{100, 200, 300},
B1: 10,
A1: 20, // hides A.A1
},
}
walker := NewFieldWalker(reflect.TypeOf(c), "")
check(t, walker, reflect.ValueOf(c), []want{
{1, "C1"},
{2, "C2"},
{0, "D"},
{10, "B.B1"},
})
})
}