-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstruct_to_map_copier_test.go
More file actions
543 lines (466 loc) · 11.6 KB
/
struct_to_map_copier_test.go
File metadata and controls
543 lines (466 loc) · 11.6 KB
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
package deepcopy
import (
"testing"
"unsafe"
"github.com/stretchr/testify/assert"
)
func Test_Copy_structToMap(t *testing.T) {
t.Run("#1: simple case", func(t *testing.T) {
type SS struct {
I int
U uint
}
var s SS = SS{I: 1, U: 2}
var d map[string]int
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, map[string]int{"I": 1, "U": 2}, d)
var dd map[string]any
err = Copy(&dd, s)
assert.Nil(t, err)
assert.Equal(t, map[string]any{"I": int(1), "U": uint(2)}, dd)
})
t.Run("#2: with copy key", func(t *testing.T) {
type SS struct {
I int `copy:"i"`
U uint
}
var s SS = SS{I: 1, U: 2}
var d map[string]int
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, map[string]int{"i": 1, "U": 2}, d)
})
t.Run("#3: with custom map key/value type", func(t *testing.T) {
type SS struct {
I int `copy:"i"`
U uint
}
type MapKey string
type MapValue int8
var s SS = SS{I: 1, U: 2}
var d map[MapKey]MapValue
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, map[MapKey]MapValue{"i": 1, "U": 2}, d)
})
t.Run("#4: with lossy conversion (int -> int8)", func(t *testing.T) {
type SS struct {
I int `copy:"i"`
U uint
}
type MapKey string
type MapValue int8
var s SS = SS{I: 1, U: 128}
var d map[MapKey]MapValue
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, map[MapKey]MapValue{"i": 1, "U": -128}, d)
})
t.Run("#5: with int -> float conversion", func(t *testing.T) {
type SS struct {
I int `copy:"i"`
U uint
}
var s SS = SS{I: 1, U: 2}
var d map[string]float32
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, map[string]float32{"i": 1, "U": 2}, d)
})
t.Run("#6: with ptr -> value conversion", func(t *testing.T) {
type SS struct {
I *int `copy:"i"`
U uint
}
var s SS = SS{I: ptrOf(1), U: 2}
var d map[string]int
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, map[string]int{"i": 1, "U": 2}, d)
s = SS{I: nil, U: 2}
d = map[string]int{}
err = Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, map[string]int{"i": 0, "U": 2}, d)
})
t.Run("#7: with value -> ptr conversion", func(t *testing.T) {
type SS struct {
I int `copy:"i"`
U uint
}
var s SS = SS{I: 1, U: 2}
var d map[string]*int
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, map[string]*int{"i": ptrOf(1), "U": ptrOf(2)}, d)
})
t.Run("#8: with struct field has type 'any'", func(t *testing.T) {
type SS struct {
I int `copy:"i"`
U any
}
var s SS = SS{I: 1, U: 2}
var d map[string]int
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, map[string]int{"i": 1, "U": 2}, d)
})
t.Run("#9: with map value has type slice", func(t *testing.T) {
type SS struct {
I []int `copy:"i"`
U []uint
}
var s SS = SS{I: []int{1, 2}, U: []uint{11, 22}}
var d map[string][]int
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, map[string][]int{"i": {1, 2}, "U": {11, 22}}, d)
})
t.Run("#10: with struct field is ignored", func(t *testing.T) {
type SS struct {
I []int `copy:"-"`
U uint
}
var s SS = SS{I: []int{1, 2}, U: 22}
var d map[string]int
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, map[string]int{"U": 22}, d)
})
t.Run("#11: cyclic reference", func(t *testing.T) {
type SS struct {
Ref *SS
}
var d map[string]*SS
var s SS = SS{Ref: &SS{Ref: &SS{}}}
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, map[string]*SS{"Ref": {Ref: &SS{}}}, d)
})
t.Run("#12: map key type is not string, but ignore error NonCopyable", func(t *testing.T) {
type SS struct {
I int
}
var s SS = SS{I: 1}
var d map[int]int
err := Copy(&d, &s, IgnoreNonCopyableTypes(true))
assert.Nil(t, err)
assert.Equal(t, map[int]int{}, d)
})
t.Run("#13: type is non-copyable and struct field is unexported, but not required (ignored)", func(t *testing.T) {
type SS struct {
i float32
}
var s SS = SS{i: 1}
var d map[string]string
err := Copy(&d, &s)
assert.Nil(t, err)
assert.Equal(t, map[string]string{}, d)
})
t.Run("#14: deep embedded struct field", func(t *testing.T) {
type SS3 struct {
I int `copy:"i"`
}
type SS2 struct {
SS3
}
type SS struct {
SS2
}
var s SS = SS{SS2: SS2{SS3: SS3{I: 1}}}
var d map[string]int
err := Copy(&d, &s)
assert.Nil(t, err)
assert.Equal(t, map[string]int{"i": 1}, d)
})
t.Run("#15: deep embedded struct field, but nil ptr", func(t *testing.T) {
type SS3 struct {
I int `copy:"i"`
}
type SS2 struct {
*SS3
}
type SS struct {
SS2
}
var s SS = SS{SS2: SS2{SS3: nil}}
var d map[string]int
err := Copy(&d, &s)
assert.Nil(t, err)
assert.Equal(t, map[string]int{}, d)
})
}
func Test_Copy_structToMap_error(t *testing.T) {
t.Run("#1: with struct fields have different types", func(t *testing.T) {
type SS struct {
I int
S string
}
var s SS = SS{I: 1, S: "abc"}
var d map[string]int
err := Copy(&d, s)
assert.ErrorIs(t, err, ErrTypeNonCopyable)
})
t.Run("#2: with struct fields have different types", func(t *testing.T) {
type SS struct {
I int
S any
}
var s SS = SS{I: 1, S: "abc"}
var d map[string]int
err := Copy(&d, s)
assert.ErrorIs(t, err, ErrTypeNonCopyable)
})
t.Run("#3: with non-copyable type", func(t *testing.T) {
type SS struct {
P unsafe.Pointer // unsafe.Pointer is not copyable for now
}
var s SS = SS{P: nil}
var d map[string]int
err := Copy(&d, &s)
assert.ErrorIs(t, err, ErrTypeNonCopyable)
})
t.Run("#4: map key type is not string", func(t *testing.T) {
type SS struct {
I int
}
var s SS = SS{I: 1}
var d map[int]int
err := Copy(&d, &s)
assert.ErrorIs(t, err, ErrTypeNonCopyable)
})
t.Run("#5: non-copyable, but field requires copying", func(t *testing.T) {
type SS struct {
P unsafe.Pointer `copy:",required"`
}
s := SS{P: nil}
var d map[string]unsafe.Pointer
err := Copy(&d, &s, IgnoreNonCopyableTypes(true))
assert.ErrorIs(t, err, ErrFieldRequireCopying)
})
}
func Test_Copy_structToMap_unexported(t *testing.T) {
t.Run("#1: struct field unexported, but required", func(t *testing.T) {
type SS struct {
I int
u uint `copy:"u,required"`
}
var s SS = SS{I: 1, u: 2}
var d map[string]int
err := Copy(&d, &s)
assert.Nil(t, err)
assert.Equal(t, map[string]int{"I": 1, "u": 2}, d)
})
t.Run("#2: struct field unexported, but non-required", func(t *testing.T) {
type SS struct {
I int
u uint `copy:"u"`
}
var s SS = SS{I: 1, u: 2}
var d map[string]int
err := Copy(&d, s) // NOTE: pass src as value to cause Unaddressable error
assert.Nil(t, err)
assert.Equal(t, map[string]int{"I": 1}, d)
})
t.Run("#3: struct field unexported", func(t *testing.T) {
type SS struct {
I int
u uint
}
var s SS = SS{I: 1, u: 2}
var d map[string]int
err := Copy(&d, &s)
assert.Nil(t, err)
assert.Equal(t, map[string]int{"I": 1, "u": 2}, d)
})
}
func Test_Copy_structToMap_unexported_error(t *testing.T) {
t.Run("#1: unaddressable field causes failure of copying unexported field", func(t *testing.T) {
type SS struct {
i int `copy:"i,required"`
S any
}
var s SS = SS{i: 1, S: 2}
var d map[string]int
err := Copy(&d, s)
assert.ErrorIs(t, err, ErrValueUnaddressable)
})
}
type testDstMap1 map[string]int
func (d testDstMap1) CopyI1(i1 int) error {
d["x1"] = i1 * 2
return nil
}
func (d testDstMap1) CopyI2(i2 int) { // incorrect method prototype (no return error)
d["x2"] = i2 * 2
}
func (d testDstMap1) CopyI3(i3 int, v string) error { // incorrect method prototype (2 input args)
d["x3"] = i3 * 2
return nil
}
func (d testDstMap1) CopyI4(i4 uint) error { // incorrect method prototype (unmatched input type)
d["x4"] = int(i4 * 2) //nolint:gosec
return nil
}
func (d testDstMap1) CopyI5(i5 int) string { // incorrect method prototype (not return error type)
return ""
}
func (d testDstMap1) CopyI6(i6 int) error { // incorrect method prototype (unmatched input type)
return errTest
}
func (d testDstMap1) NotCopy(i6 int) error { // not a copying method
return errTest
}
func Test_Copy_structToMap_method(t *testing.T) {
t.Run("#1: field -> dst method", func(t *testing.T) {
type SS struct {
I1 int
U uint
}
var s SS = SS{I1: 1, U: 2}
var d testDstMap1
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, testDstMap1{"x1": 2, "U": 2}, d)
})
t.Run("#2: unexported field -> dst method", func(t *testing.T) {
type SS struct {
i1 int `copy:"I1,required"`
U uint
}
var s SS = SS{i1: 1, U: 2}
var d testDstMap1
err := Copy(&d, &s)
assert.Nil(t, err)
assert.Equal(t, testDstMap1{"x1": 2, "U": 2}, d)
})
t.Run("#3: incorrect method prototype (CopyI2())", func(t *testing.T) {
type SS struct {
I2 int
U uint
}
var s SS = SS{I2: 1, U: 2}
var d testDstMap1
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, testDstMap1{"I2": 1, "U": 2}, d) // CopyI2 is not called
})
t.Run("#4: not allow copying from field -> method", func(t *testing.T) {
type SS struct {
I1 int
U uint
}
var s SS = SS{I1: 1, U: 2}
var d testDstMap1
err := Copy(&d, s, CopyBetweenStructFieldAndMethod(false))
assert.Nil(t, err)
assert.Equal(t, testDstMap1{"I1": 1, "U": 2}, d)
})
t.Run("#5: copy from src embedded field", func(t *testing.T) {
type SBase struct {
I1 int
}
type SS struct {
SBase
U uint
}
var s SS = SS{U: 2, SBase: SBase{I1: 123}}
var d testDstMap1
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, testDstMap1{"U": 2, "x1": 246}, d)
})
t.Run("#6: copy from src embedded field, but field value can't be retrieved due to nil ptr", func(t *testing.T) {
type SBase struct {
I1 int
}
type SS struct {
*SBase
U uint
}
var s SS = SS{U: 2}
var d testDstMap1
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, testDstMap1{"U": 2}, d)
})
}
func Test_Copy_structToMap_method_error(t *testing.T) {
t.Run("#1: unexported field -> dst method", func(t *testing.T) {
type SS struct {
i1 int `copy:"I1,required"`
U uint
}
var s SS = SS{i1: 1, U: 2}
var d testDstMap1
err := Copy(&d, s)
assert.ErrorIs(t, err, ErrValueUnaddressable)
})
t.Run("#2: incorrect method prototype (CopyI4())", func(t *testing.T) {
type SS struct {
I4 int `copy:",required"`
U uint
}
var s SS = SS{I4: 1, U: 2}
var d testDstMap1
err := Copy(&d, s)
assert.ErrorIs(t, err, ErrMethodInvalid)
})
t.Run("#3: copy method return error (CopyI6())", func(t *testing.T) {
type SS struct {
I6 int `copy:",required"`
U uint
}
var s SS = SS{I6: 1, U: 2}
var d testDstMap1
err := Copy(&d, s)
assert.ErrorIs(t, err, errTest)
})
}
type testSrc2 struct {
I int
U uint
}
type testDstMap2 map[string]int
func (d testDstMap2) PostCopy(src any) error {
testSrc2, _ := src.(testSrc2)
if testSrc2.I == 100 {
return errTest
}
d["I"] *= 2
d["U"] *= 2
return nil
}
type testDstMap3 struct {
I int
U uint
}
func (d testDstMap3) PostCopy(src any) any {
d.I *= 2
d.U *= 2
return nil
}
func Test_Copy_structToMap_with_post_copy_event(t *testing.T) {
t.Run("#1: success without error", func(t *testing.T) {
s := testSrc2{I: 1, U: 2}
var d testDstMap2
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, testDstMap2{"I": 2, "U": 4}, d)
})
t.Run("#2: PostCopy returns error", func(t *testing.T) {
s := testSrc2{I: 100, U: 2} // When testSrc2.I == 100, PostCopy returns error
d := testDstMap2{}
err := Copy(&d, s)
assert.NotNil(t, err)
assert.ErrorIs(t, err, errTest)
})
t.Run("#3: dstStruct.PostCopy not satisfied", func(t *testing.T) {
s := testSrc2{I: 1, U: 2}
d := testDstMap3{}
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, testDstMap3{I: 1, U: 2}, d)
})
}