-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct_test.go
215 lines (183 loc) · 5.08 KB
/
struct_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 rflutil
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func valOf(v any) reflect.Value {
return reflect.ValueOf(v)
}
func ptrOf[T any](v T) *T {
return &v
}
func Test_StructGetField(t *testing.T) {
type SS struct {
I int
u uint
}
t.Run("#1: success", func(t *testing.T) {
v, err := StructGetField[int](valOf(&SS{I: 1, u: 2}), "I", true)
assert.Nil(t, err)
assert.Equal(t, 1, v)
})
t.Run("#2: case insensitive", func(t *testing.T) {
v, err := StructGetField[int](valOf(SS{I: 1, u: 2}), "i", false)
assert.Nil(t, err)
assert.Equal(t, 1, v)
})
t.Run("#3: unexported", func(t *testing.T) {
v, err := StructGetField[uint](valOf(&SS{I: 1, u: 2}), "u", true)
assert.Nil(t, err)
assert.Equal(t, uint(2), v)
})
t.Run("#4: unexported - case insensitive", func(t *testing.T) {
v, err := StructGetField[uint](valOf(&SS{I: 1, u: 2}), "U", false)
assert.Nil(t, err)
assert.Equal(t, uint(2), v)
})
}
func Test_StructGetField_failure(t *testing.T) {
type SS struct {
I int
u uint
}
t.Run("#1: field not found", func(t *testing.T) {
_, err := StructGetField[int](valOf(SS{I: 1, u: 2}), "II", true)
assert.ErrorIs(t, err, ErrNotFound)
})
t.Run("#2: input not struct", func(t *testing.T) {
_, err := StructGetField[int](valOf("abc123"), "I", true)
assert.ErrorIs(t, err, ErrTypeInvalid)
})
t.Run("#3: output type unmatched", func(t *testing.T) {
_, err := StructGetField[uint](valOf(&SS{I: 1, u: 2}), "I", true)
assert.ErrorIs(t, err, ErrTypeUnmatched)
})
t.Run("#4: 2 fields have same case-insensitive names", func(t *testing.T) {
// nolint: unused
type SS2 struct {
I int
i string
u uint
}
_, err := StructGetField[int](valOf(SS2{I: 1, u: 2}), "i", false)
assert.ErrorIs(t, err, ErrNotFound)
})
t.Run("#5: unexported but can't get address of field", func(t *testing.T) {
_, err := StructGetField[uint](valOf(SS{I: 1, u: 2}), "u", true)
assert.ErrorIs(t, err, ErrValueUnaddressable)
})
}
func Test_StructSetField(t *testing.T) {
type SS struct {
I int
u uint
}
t.Run("#1: success", func(t *testing.T) {
s := SS{I: 1, u: 2}
err := StructSetField(valOf(&s), "I", 11, true)
assert.Nil(t, err)
assert.Equal(t, 11, s.I)
})
t.Run("#2: case insensitive", func(t *testing.T) {
s := SS{I: 1, u: 2}
err := StructSetField(valOf(&s), "i", 11, false)
assert.Nil(t, err)
assert.Equal(t, 11, s.I)
})
t.Run("#3: unexported field", func(t *testing.T) {
s := SS{I: 1, u: 2}
err := StructSetField(valOf(&s), "u", uint(22), true)
assert.Nil(t, err)
assert.Equal(t, uint(22), s.u)
})
t.Run("#4: unexported field and case insensitive", func(t *testing.T) {
s := SS{I: 1, u: 2}
err := StructSetField(valOf(&s), "U", uint(22), false)
assert.Nil(t, err)
assert.Equal(t, uint(22), s.u)
})
}
func Test_StructSetField_failure(t *testing.T) {
type SS struct {
I int
u uint
}
t.Run("#1: field not found", func(t *testing.T) {
s := SS{I: 1, u: 2}
err := StructSetField(valOf(&s), "II", 1, true)
assert.ErrorIs(t, err, ErrNotFound)
})
t.Run("#2: input not struct", func(t *testing.T) {
s := 123456
err := StructSetField(valOf(&s), "I", 1, true)
assert.ErrorIs(t, err, ErrTypeInvalid)
})
t.Run("#3: field unsettable", func(t *testing.T) {
s := SS{I: 1, u: 2}
err := StructSetField(valOf(s), "I", 11, true)
assert.ErrorIs(t, err, ErrValueUnsettable)
})
t.Run("#4: field type unmatched", func(t *testing.T) {
s := SS{I: 1, u: 2}
err := StructSetField(valOf(&s), "I", int32(11), true)
assert.ErrorIs(t, err, ErrTypeUnmatched)
})
t.Run("#5: unexported but can't get address of field", func(t *testing.T) {
s := SS{I: 1, u: 2}
err := StructSetField(valOf(s), "u", uint(11), true)
assert.ErrorIs(t, err, ErrValueUnaddressable)
})
}
func Test_StructListFields(t *testing.T) {
type SS struct {
I int `mytag:"ii"`
u uint `mytag:"uu"`
}
t.Run("#1: success", func(t *testing.T) {
v, err := StructListFields(valOf(&SS{}), false)
assert.Nil(t, err)
assert.Equal(t, []string{"I"}, v)
})
t.Run("#2: success - flatten embedded struct", func(t *testing.T) {
type SS2 struct {
SS `mytag:"ss"`
I int `mytag:"ii"`
}
v, err := StructListFields(valOf(&SS2{}), true)
assert.Nil(t, err)
assert.Equal(t, []string{"I"}, v)
})
t.Run("#3: success - multi-level embedded structs", func(t *testing.T) {
type SS2 struct {
SS `mytag:"ss"`
I int `mytag:"ii"`
}
type SS3 struct {
I2 int `mytag:"ii2"`
SS2 `mytag:"ss2"`
}
v, err := StructListFields(valOf(SS3{}), true)
assert.Nil(t, err)
assert.Equal(t, []string{"I2", "I"}, v)
})
t.Run("#4: success - embedded structs, no flatten embedded fields", func(t *testing.T) {
type SS2 struct {
SS `mytag:"ss"`
I int `mytag:"ii"`
}
type SS3 struct {
I2 int `mytag:"ii2"`
SS2 `mytag:"ss2"`
}
v, err := StructListFields(valOf(SS3{}), false)
assert.Nil(t, err)
assert.Equal(t, []string{"I2", "SS2"}, v)
})
}
func Test_StructListFields_failure(t *testing.T) {
t.Run("#1: input not struct", func(t *testing.T) {
_, err := StructListFields(valOf("abc123"), false)
assert.ErrorIs(t, err, ErrTypeInvalid)
})
}