-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct2map_test.go
395 lines (340 loc) · 8.34 KB
/
struct2map_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
package struct2map
import (
"fmt"
"reflect"
"testing"
"github.com/frankban/quicktest"
)
type testErr struct{}
func (te *testErr) Error() string {
return "error happened"
}
func testError() *testErr {
return nil
}
func badNil() error {
//if err := testError(); err != nil {
// return err
//}
//return nil // this make test return real nil
return testError()
}
func Test_Convert_nilNotEqualNil(t *testing.T) {
err := badNil()
if err == nil {
t.Fatal("err should not eq to nil")
}
c := NewConvertorAny(err)
v := c.Convert()
if v != nil {
t.Fatal(err, "convert to", v, "not nil")
}
}
func Test_Convert_nil(t *testing.T) {
c2 := NewConvertorAny(nil)
v := c2.Convert()
if v != nil {
t.Fatal("nil convert to", v, "not nil")
}
var t2 *testing.T
c2 = NewConvertorAny(t2)
v = c2.Convert()
if v != nil {
t.Fatal("*testing.T(nil) convert to", v, "not nil")
}
}
func Test_Convert_notStruct(t *testing.T) {
var vs = []any{1, 2.1, map[string]any{}, []any{}, true, uint32(32)}
var panicNum int
for _, v := range vs {
last := panicNum
func() {
defer func() {
if p := recover(); p != nil {
panicNum++
}
}()
NewConvertorAny(v).Convert()
}()
if last == panicNum {
t.Fatalf("convert %v no panic", v)
}
}
}
func Test_Convert(t *testing.T) {
type kc struct {
a int
b string
c bool
kcc bool
KCC bool
}
type BC struct {
bb string
cc bool
DD bool
}
type kkc struct {
kc
BC
d bool
DcD bool
//DD bool
VS []any
}
kv := []any{11, true, kc{KCC: true, a: 1232324}}
vv := kkc{VS: kv}
test4(quicktest.New(t), vv, [4]string{
"map[BC:map[DD:false] DcD:false VS:[11 true map[KCC:true]]]",
"map[BC:map[DD:false bb: cc:false] DcD:false VS:[11 true map[KCC:true a:1232324 b: c:false kcc:false]] d:false kc:map[KCC:false a:0 b: c:false kcc:false]]",
"map[DD:false DcD:false VS:[11 true map[KCC:true]]]",
"map[DD:false DcD:false KCC:false VS:[11 true map[KCC:true a:1232324 b: c:false kcc:false]] a:0 b: bb: c:false cc:false d:false kcc:false]",
})
}
func Test_kindInterface(t *testing.T) {
type BBC struct {
VS []any
}
bbc := BBC{}
bbc.VS = append(bbc.VS, 1, nil, map[string]any{}, map[any]any{"tu": 1, "1": 2, 3: 4, "5": map[any]any{"a": "b"}}, 2, "test1")
inx0 := reflect.ValueOf(bbc).FieldByName("VS").Index(0)
dkind := inx0.Kind() // reflect.Interface
if dkind != reflect.Interface {
t.Fatalf("error idx0 kind")
}
of := reflect.ValueOf(inx0.Interface())
wrapKind := of.Kind() // reflect.Int
if wrapKind != reflect.Int { // kind turn back !
t.Fatalf("error idx0 kind")
}
}
func Test_Convert_withSlice(t *testing.T) {
type BBC struct {
VS []any
}
var vs2 []any
var fp *float32
var f32 = 1.22
var fp2 = &f32
bbc := BBC{}
var mp2 map[string]any
bbc.VS = append(bbc.VS,
1,
nil,
map[string]any{},
map[any]any{"tu": 1, "1": 2, 3: 4, "5": map[any]any{"a": "b"}},
2,
"test1",
mp2,
[]any{},
vs2,
fp2,
fp,
)
test4(quicktest.New(t), bbc, [4]string{
"map[VS:[1 <nil> map[] map[1:2 5:map[a:b] tu:1] 2 test1 map[] [] [] 1.22 <nil>]]",
"map[VS:[1 <nil> map[] map[1:2 5:map[a:b] tu:1] 2 test1 map[] [] [] 1.22 <nil>]]",
"map[VS:[1 <nil> map[] map[1:2 5:map[a:b] tu:1] 2 test1 map[] [] [] 1.22 <nil>]]",
"map[VS:[1 <nil> map[] map[1:2 5:map[a:b] tu:1] 2 test1 map[] [] [] 1.22 <nil>]]",
})
}
func Test_Convert_withPtrSliceMap(t *testing.T) {
type BBC struct {
VS *[]any
int *map[string]any
}
c := quicktest.New(t)
result := NewConvertorAny(BBC{}, KeepUnexported()).Convert()
// nil with type
c.Assert(result["VS"], quicktest.IsNil)
c.Assert(reflect.TypeOf(result["VS"]), quicktest.Equals, reflect.TypeOf([]any{}))
c.Assert(result["int"], quicktest.IsNil)
c.Assert(reflect.TypeOf(result["int"]), quicktest.Equals, reflect.TypeOf(map[string]any{}))
test4(c, BBC{}, [4]string{
"map[VS:[]]", // output [], but VS is nil
"map[VS:[] int:map[]]",
"map[VS:[]]",
"map[VS:[] int:map[]]",
})
mp := map[string]any{"a": 1, "b": 2}
test4(c, BBC{VS: &([]any{1, 2, 3}), int: &mp}, [4]string{
"map[VS:[1 2 3]]",
"map[VS:[1 2 3] int:map[a:1 b:2]]",
"map[VS:[1 2 3]]",
"map[VS:[1 2 3] int:map[a:1 b:2]]",
})
}
func Test_Convert_withPtrAnyInSlice(t *testing.T) {
type BBC struct {
VS []any
Va *any
}
bbc := BBC{}
var nm any
var nilSlice []int
var mk any = 12323
bbc.VS = append(bbc.VS, &nm, 1, 2, &nilSlice, nilSlice, &mk)
test4(quicktest.New(t), bbc, [4]string{
"map[VS:[<nil> 1 2 [] [] 12323] Va:<nil>]",
"map[VS:[<nil> 1 2 [] [] 12323] Va:<nil>]",
"map[VS:[<nil> 1 2 [] [] 12323] Va:<nil>]",
"map[VS:[<nil> 1 2 [] [] 12323] Va:<nil>]",
})
}
func Test_Convert_withNilSlice(t *testing.T) {
type BBC struct {
VS []any
}
bbc := BBC{}
pbbc := &bbc
test4(quicktest.New(t), pbbc, [4]string{
"map[VS:[]]",
"map[VS:[]]",
"map[VS:[]]",
"map[VS:[]]",
})
test4(quicktest.New(t), bbc, [4]string{
"map[VS:[]]",
"map[VS:[]]",
"map[VS:[]]",
"map[VS:[]]",
})
test4(quicktest.New(t), &pbbc, [4]string{
"map[VS:[]]",
"map[VS:[]]",
"map[VS:[]]",
"map[VS:[]]",
})
}
func Test_Convert_withFuncChan(t *testing.T) {
type BBC struct {
VS func()
C chan int
c2 chan int
ii int
}
test4(quicktest.New(t), BBC{}, [4]string{
"map[]",
"map[ii:0]",
"map[]",
"map[ii:0]",
})
}
func Test_Convert_embedBaseType(t *testing.T) {
type tt struct {
int
float64
*float32
}
// for base type embed, same as follows
//type tt struct {
// int int
// float64 float64
//}
vv := tt{
int: 11,
float64: 0.2,
}
c := quicktest.New(t)
test4(c, vv, [4]string{
"map[]", "map[float32:<nil> float64:0.2 int:11]", "map[]", "map[float32:<nil> float64:0.2 int:11]",
})
pp := float32(1.1)
vv.float32 = &pp
test4(c, vv, [4]string{
"map[]", "map[float32:1.1 float64:0.2 int:11]", "map[]", "map[float32:1.1 float64:0.2 int:11]",
})
}
func Test_Convert_embedPointer(t *testing.T) {
type tt2 struct {
A int
B bool
c bool
}
type tt struct {
*tt2
D bool
e bool
}
vv := tt{}
c := quicktest.New(t)
test4(c, vv, [4]string{
"map[D:false]", "map[D:false e:false tt2:map[]]", "map[D:false]", "map[D:false e:false]",
})
}
func test4(c *quicktest.C, v any, want [4]string) {
vv := reflect.ValueOf(v)
c1, c2, c3, c4 := NewConvertorValue(vv), NewConvertorValue(vv, KeepUnexported()),
NewConvertorValue(vv, FlattenEmbed()), NewConvertorValue(vv, KeepUnexported(), FlattenEmbed())
var cs []*Convertor
cs = append(cs, c1, c2, c3, c4)
var gots []string
var output []string
for _, convertor := range cs {
convert := convertor.Convert()
gi := fmt.Sprintf("%v", convert)
gots = append(gots, gi)
output = append(output, fmt.Sprintf("\"%s\"", gi))
}
//fmt.Println(strings.Join(output, ",\n") + ",")
for i, got := range gots {
c.Assert(got, quicktest.Equals, want[i], quicktest.Commentf(c.TB.Name()+":"+getComment(cs[i])))
}
}
func getComment(c *Convertor) string {
return fmt.Sprintf("KeepUnexported:%t, FlattenEmbed: %t, %#v", c.KeepUnexported, c.FlattenEmbed, c.v.Interface())
}
func Test_usage(t *testing.T) {
type B struct {
B1 int
B2 string
b3 bool
}
type C struct {
C1 int
C2 string
C3 bool
}
type A struct {
A1 int
A3 bool
B B
C
}
v := A{
A1: 1,
A3: false,
B: B{
B1: 99,
B2: "b2",
b3: true,
},
C: C{
C1: 22,
C2: "c2",
C3: false,
},
}
c1 := NewConvertorAny(v)
c2 := NewConvertorAny(v, FlattenEmbed()) // difference between result of c1 and c2
c3 := NewConvertorAny(v, KeepUnexported()) // export unexported fields
c4 := NewConvertorAny(v, FlattenEmbed(), KeepUnexported())
fmt.Println(c1.Convert()) // map[A1:1 A3:false B:map[B1:99 B2:b2] C:map[C1:22 C2:c2 C3:false]]
fmt.Println(c2.Convert()) // map[A1:1 A3:false B:map[B1:99 B2:b2] C1:22 C2:c2 C3:false]
fmt.Println(c3.Convert()) // map[A1:1 A3:false B:map[B1:99 B2:b2 b3:true] C:map[C1:22 C2:c2 C3:false]]
fmt.Println(c4.Convert()) // map[A1:1 A3:false B:map[B1:99 B2:b2 b3:true] C1:22 C2:c2 C3:false]
fmt.Println(NewConvertorAny(nil).Convert()) // map[] (nil map)
var a = struct{}{}
fmt.Println(NewConvertorAny(a).Convert()) // map[] (empty map)
type D struct {
*C
V *C
}
c5 := NewConvertorAny(D{})
c6 := NewConvertorAny(D{}, FlattenEmbed())
r5 := c5.Convert()
// r5["C"] is nil map but not nil
fmt.Println(r5, reflect.ValueOf(r5["C"]).IsNil(), r5["C"] == nil) // map[C:map[] V:map[]] true false
// enable FlattenEmbed, *C convert to nil map and merge to result.
fmt.Println(c6.Convert()) // map[V:map[]]
}