-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstruct_copier_test.go
850 lines (746 loc) · 16.8 KB
/
struct_copier_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
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
package deepcopy
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_Copy_struct(t *testing.T) {
t.Run("#1: copy fields directly", func(t *testing.T) {
type SS struct {
I int
U uint
}
type DD struct {
I int
U uint
}
var s SS = SS{I: 1, U: 2}
var d DD
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, DD{I: 1, U: 2}, d)
})
t.Run("#2: copy fields with conversion", func(t *testing.T) {
type SS struct {
I int
F float32
}
type DD struct {
I IntT
F uint
}
var s SS = SS{I: 1, F: 2.2}
var d DD
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, DD{I: 1, F: 2}, d)
})
t.Run("#3: copy fields with lossy conversion (int -> int8)", func(t *testing.T) {
type SS struct {
I int
F float32
X string
}
type DD struct {
I int8
F uint
Y bool
}
var s SS = SS{I: 128, F: 2.2}
var d DD
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, DD{I: -128, F: 2}, d)
})
t.Run("#4: copy fields with conversion (ptr -> value)", func(t *testing.T) {
type SS struct {
I *int
F float32
}
type DD struct {
I int
F uint
}
var s SS = SS{I: ptrOf(1), F: 2.2}
var d DD
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, DD{I: 1, F: 2}, d)
})
t.Run("#5: copy fields with conversion (value -> ptr)", func(t *testing.T) {
type SS struct {
I int
F float32
}
type DD struct {
I *int8
F uint
}
var s SS = SS{I: 128, F: 2.2}
var d DD
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, DD{I: ptrOf(int8(-128)), F: 2}, d)
})
t.Run("#6: copy fields with conversion (slice -> array)", func(t *testing.T) {
type SS struct {
I []int
F float32
}
type DD struct {
I [5]int8
F uint
}
var s SS = SS{I: []int{126, 127, 128}, F: 2.2}
var d DD = DD{I: [5]int8{1, 2, 3, 4, 5}}
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, DD{I: [5]int8{126, 127, -128, 0, 0}, F: 2}, d)
})
t.Run("#7: struct-in-struct", func(t *testing.T) {
type SS2 struct {
I int
X bool
}
type SS struct {
I int
SS SS2
}
type DD2 struct {
I int
Y string
}
type DD struct {
I int
DD *DD2 `copy:"SS"`
}
var s SS = SS{I: 1, SS: SS2{I: 11, X: true}}
var d DD
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, DD{I: 1, DD: &DD2{I: 11}}, d)
})
t.Run("#8: with src field is ignored", func(t *testing.T) {
type SS struct {
I int `copy:"-"`
F float32
}
type DD struct {
I int
F uint
}
var s SS = SS{I: 1, F: 2.2}
var d DD = DD{I: 100}
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, DD{I: 100, F: 2}, d)
})
t.Run("#9: with dst field is ignored", func(t *testing.T) {
type SS struct {
I int
F float32
}
type DD struct {
I int `copy:"-"`
F uint
}
var s SS = SS{I: 1, F: 2.2}
var d DD = DD{I: 100}
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, DD{I: 100, F: 2}, d)
})
t.Run("#10: structs have fields of type function", func(t *testing.T) {
type SS struct {
I int
Fn func(int) string `copy:"fn"`
}
type DD struct {
I int
Fn func(int) string `copy:"fn"`
}
fn := func(int) string { return "abc" }
var s SS = SS{I: 1, Fn: fn}
var d DD = DD{I: 100}
err := Copy(&d, s)
assert.Nil(t, err)
assert.NotNil(t, d.Fn)
assert.Equal(t, "abc", d.Fn(1))
})
}
func Test_Copy_struct_error(t *testing.T) {
t.Run("#1: struct -> slice (error)", func(t *testing.T) {
type SS struct {
I int
}
var s SS = SS{111}
var d []int
err := Copy(&d, s)
assert.ErrorIs(t, err, ErrTypeNonCopyable)
})
t.Run("#2: src required but no equivalent dst field", func(t *testing.T) {
type SS struct {
I int `copy:",required"`
U uint
}
type DD struct {
U uint
}
var s SS = SS{I: 1, U: 2}
var d DD
err := Copy(&d, s)
assert.ErrorIs(t, err, ErrFieldRequireCopying)
})
t.Run("#3: dst required but no equivalent src field", func(t *testing.T) {
type SS struct {
I int
}
type DD struct {
U uint `copy:",required"`
}
var s SS = SS{I: 1}
var d DD
err := Copy(&d, s)
assert.ErrorIs(t, err, ErrFieldRequireCopying)
})
t.Run("#4: struct -> slice (ignore error)", func(t *testing.T) {
type SS struct {
I int
}
var s SS = SS{111}
var d []int
err := Copy(&d, s, IgnoreNonCopyableTypes(true))
assert.Nil(t, err)
})
t.Run("#5: src ignore non-copyable but set 'required'", func(t *testing.T) {
type SS struct {
I int `copy:",required"`
U uint
}
type DD struct {
I []string
U uint
}
var s SS = SS{I: 1, U: 2}
var d DD
err := Copy(&d, s, IgnoreNonCopyableTypes(true))
assert.ErrorIs(t, err, ErrFieldRequireCopying)
})
t.Run("#6: dst ignore non-copyable but set 'required'", func(t *testing.T) {
type SS struct {
I int
U uint
}
type DD struct {
I []string `copy:",required"`
U uint
}
var s SS = SS{I: 1, U: 2}
var d DD
err := Copy(&d, s, IgnoreNonCopyableTypes(true))
assert.ErrorIs(t, err, ErrFieldRequireCopying)
})
t.Run("#7: non-copyable between src and dst fields", func(t *testing.T) {
type SS struct {
I []float32
U uint
}
type DD struct {
I []string
U uint
}
var s SS = SS{I: []float32{1}, U: 2}
var d DD
err := Copy(&d, s)
assert.ErrorIs(t, err, ErrTypeNonCopyable)
})
}
func Test_Copy_struct_unexported(t *testing.T) {
t.Run("#1: unexported -> unexported", func(t *testing.T) {
type SS struct {
I int
u uint
}
type DD struct {
I int
u uint `copy:",required"`
}
var s SS = SS{I: 1, u: 2}
var d DD
err := Copy(&d, &s)
assert.Nil(t, err)
assert.Equal(t, DD{I: 1, u: 2}, d)
})
t.Run("#2: unexported -> exported", func(t *testing.T) {
type SS struct {
I int
u uint
}
type DD struct {
I int
U uint `copy:"u,required"`
}
var s SS = SS{I: 1, u: 2}
var d DD
err := Copy(&d, &s)
assert.Nil(t, err)
assert.Equal(t, DD{I: 1, U: 2}, d)
})
t.Run("#3: exported -> unexported", func(t *testing.T) {
type SS struct {
I int
U uint
}
type DD struct {
I int
u uint `copy:"U,required"`
}
var s SS = SS{I: 1, U: 2}
var d DD
err := Copy(&d, &s)
assert.Nil(t, err)
assert.Equal(t, DD{I: 1, u: 2}, d)
})
t.Run("#4: exported -> unexported with conversion", func(t *testing.T) {
type SS struct {
I int
U uint
}
type DD struct {
i IntT `copy:"I,required"`
U uint
}
var s SS = SS{I: 1, U: 2}
var d DD
err := Copy(&d, &s)
assert.Nil(t, err)
assert.Equal(t, DD{i: 1, U: 2}, d)
})
t.Run("#5: unexported -> unexported with conversion", func(t *testing.T) {
type SS struct {
i int `copy:"I,required"`
U uint
}
type DD struct {
i *int `copy:"I,required"`
U uint
}
var s SS = SS{i: 1, U: 2}
var d DD
err := Copy(&d, &s)
assert.Nil(t, err)
assert.Equal(t, DD{i: ptrOf(1), U: 2}, d)
})
}
func Test_Copy_struct_unexported_error(t *testing.T) {
t.Run("#1: src is unaddressable", func(t *testing.T) {
type SS struct {
I int
u uint
}
type DD struct {
I int
u uint `copy:",required"`
}
var s SS = SS{I: 1, u: 2}
var d DD
err := Copy(&d, s)
assert.ErrorIs(t, err, ErrValueUnaddressable)
})
}
type testD1 struct {
x1 int
x2 int
x3 int
x4 uint
U uint
}
func (d *testD1) CopyI1(i1 int) error {
d.x1 = i1 * 2
return nil
}
func (d *testD1) CopyI2(i2 int) { // incorrect method prototype (no return error)
d.x2 = i2 * 2
}
func (d *testD1) CopyI3(i3 int, v string) error { // incorrect method prototype (2 input args)
d.x3 = i3 * 2
return nil
}
func (d *testD1) CopyI4(i4 uint) error { // incorrect method prototype (unmatched input type)
d.x4 = i4 * 2
return nil
}
func (d *testD1) CopyI5(i5 int) string { // incorrect method prototype (not return error type)
return ""
}
func (d *testD1) CopyI6(i6 int) error { // incorrect method prototype (unmatched input type)
return errTest
}
func (d *testD1) NotCopy(i6 int) error { // not a copying method
return errTest
}
func Test_Copy_struct_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 testD1
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, testD1{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 testD1
err := Copy(&d, &s)
assert.Nil(t, err)
assert.Equal(t, testD1{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 testD1
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, testD1{U: 2}, d)
})
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 testD1
err := Copy(&d, s, CopyBetweenStructFieldAndMethod(false))
assert.Nil(t, err)
assert.Equal(t, testD1{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 testD1
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, testD1{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 testD1
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, testD1{U: 2}, d)
})
}
func Test_Copy_struct_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 testD1
err := Copy(&d, s)
assert.ErrorIs(t, err, ErrValueUnaddressable)
})
t.Run("#2: incorrect method prototype (CopyI2())", func(t *testing.T) {
type SS struct {
I2 int `copy:",required"`
U uint
}
var s SS = SS{I2: 1, U: 2}
var d testD1
err := Copy(&d, s)
assert.ErrorIs(t, err, ErrFieldRequireCopying)
})
t.Run("#3: incorrect method prototype (CopyI3())", func(t *testing.T) {
type SS struct {
I3 int `copy:",required"`
U uint
}
var s SS = SS{I3: 1, U: 2}
var d testD1
err := Copy(&d, s)
assert.ErrorIs(t, err, ErrFieldRequireCopying)
})
t.Run("#4: 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 testD1
err := Copy(&d, s)
assert.ErrorIs(t, err, ErrMethodInvalid)
})
t.Run("#5: incorrect method prototype (CopyI5())", func(t *testing.T) {
type SS struct {
I5 int `copy:",required"`
U uint
}
var s SS = SS{I5: 1, U: 2}
var d testD1
err := Copy(&d, s)
assert.ErrorIs(t, err, ErrFieldRequireCopying)
})
t.Run("#6: 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 testD1
err := Copy(&d, s)
assert.ErrorIs(t, err, errTest)
})
}
func Test_Copy_struct_with_embedded_struct(t *testing.T) {
type SBase1 struct {
I int
}
type SBase2 struct {
SBase1
S string
}
type DBase1 struct {
I int
}
type DBase2 struct {
DBase1
S string
}
t.Run("#1: both src and dst have equivalent embedded fields", func(t *testing.T) {
type SS struct {
SBase2
U uint `copy:",required"`
}
type DD struct {
DBase2
U uint `copy:",required"`
}
s := SS{U: 100, SBase2: SBase2{S: "abc", SBase1: SBase1{I: 11}}}
d := DD{}
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, DD{U: 100, DBase2: DBase2{S: "abc", DBase1: DBase1{I: 11}}}, d)
// With some tags
type SS2 struct {
SBase2 `copy:"base,required"`
U uint `copy:",required"`
}
type DD2 struct {
DBase2 `copy:"base,required"`
U uint `copy:",required"`
}
s2 := SS2{U: 100, SBase2: SBase2{S: "abc", SBase1: SBase1{I: 11}}}
d2 := DD2{}
err = Copy(&d2, s2)
assert.Nil(t, err)
assert.Equal(t, DD2{U: 100, DBase2: DBase2{S: "abc", DBase1: DBase1{I: 11}}}, d2)
})
t.Run("#2: both src and dst have same embedded struct", func(t *testing.T) {
type SS struct {
SBase2
U uint `copy:",required"`
}
type DD struct {
SBase2
U uint `copy:",required"`
}
s := SS{U: 100, SBase2: SBase2{S: "abc", SBase1: SBase1{I: 11}}}
d := DD{U: 123, SBase2: SBase2{S: "xyz", SBase1: SBase1{I: 111}}}
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, DD{U: 100, SBase2: SBase2{S: "abc", SBase1: SBase1{I: 11}}}, d)
})
t.Run("#3: both src and dst have equivalent embedded fields, but src embeds ptr of struct", func(t *testing.T) {
type SS struct {
*SBase2
U uint `copy:",required"`
}
type DD struct {
DBase2
U uint `copy:",required"`
}
// Ptr has value set
s := SS{U: 100, SBase2: &SBase2{S: "abc", SBase1: SBase1{I: 11}}}
d := DD{}
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, DD{U: 100, DBase2: DBase2{S: "abc", DBase1: DBase1{I: 11}}}, d)
// Ptr is nil
s = SS{U: 100}
d = DD{}
err = Copy(&d, &s)
assert.Nil(t, err)
assert.Equal(t, DD{U: 100}, d)
})
t.Run("#4: both src and dst have equivalent embedded fields, but dst embeds ptr of struct", func(t *testing.T) {
type SS struct {
SBase2
U uint
}
type DD struct {
*DBase2
U uint
}
s := SS{U: 100, SBase2: SBase2{S: "abc", SBase1: SBase1{I: 11}}}
d := DD{}
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, DD{U: 100, DBase2: &DBase2{S: "abc", DBase1: DBase1{I: 11}}}, d)
})
t.Run("#5: src has embedded struct, dst doesn't (flattening the copy)", func(t *testing.T) {
type SS struct {
SBase2
U uint
}
type DD struct {
I int `copy:",required"`
S string `copy:",required"`
U uint `copy:",required"`
}
s := SS{U: 100, SBase2: SBase2{S: "abc", SBase1: SBase1{I: 11}}}
d := DD{S: "xyz"}
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, DD{U: 100, S: "abc", I: 11}, d)
// With ignoring a field
type DD2 struct {
I int `copy:",required"`
S string `copy:"-"`
U uint `copy:",required"`
}
d2 := DD2{}
err = Copy(&d2, s)
assert.Nil(t, err)
assert.Equal(t, DD2{U: 100, I: 11}, d2)
})
t.Run("#6: src has embedded struct ptr, dst doesn't (flattening the copy)", func(t *testing.T) {
type SS struct {
*SBase2
U uint
}
type DD struct {
I int `copy:",required"`
S string `copy:",required"`
U uint `copy:",required"`
}
// Ptr has a value set
s := SS{U: 100, SBase2: &SBase2{S: "abc", SBase1: SBase1{I: 11}}}
d := DD{S: "xyz"}
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, DD{U: 100, S: "abc", I: 11}, d)
// Ptr is nil
s = SS{U: 100}
d = DD{S: "xyz"}
err = Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, DD{U: 100}, d)
// With ignoring a field
type DD2 struct {
I int `copy:",required"`
S string `copy:"-"`
U uint `copy:",required"`
}
s = SS{U: 100, SBase2: &SBase2{S: "abc", SBase1: SBase1{I: 11}}}
d2 := DD2{S: "xyz"}
err = Copy(&d2, s)
assert.Nil(t, err)
assert.Equal(t, DD2{U: 100, S: "xyz", I: 11}, d2)
})
t.Run("#7: dst has embedded struct, src doesn't (flattening the copy)", func(t *testing.T) {
type SS struct {
I int `copy:",required"`
S string `copy:",required"`
U uint `copy:",required"`
}
type DD struct {
DBase2
U uint
}
s := SS{U: 100, S: "abc", I: 11}
d := DD{U: 123, DBase2: DBase2{S: "xyz"}}
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, DD{U: 100, DBase2: DBase2{S: "abc", DBase1: DBase1{I: 11}}}, d)
})
t.Run("#8: dst has embedded struct ptr, src doesn't (flattening the copy)", func(t *testing.T) {
type SS struct {
I int `copy:",required"`
S string `copy:",required"`
U uint `copy:",required"`
}
type DD struct {
*DBase2
U uint
}
// Ptr has value set
s := SS{U: 100, S: "abc", I: 11}
d := DD{U: 123, DBase2: &DBase2{S: "xyz"}}
err := Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, DD{U: 100, DBase2: &DBase2{S: "abc", DBase1: DBase1{I: 11}}}, d)
// Ptr is nil initially
s = SS{U: 100, S: "abc", I: 11}
d = DD{U: 123}
err = Copy(&d, s)
assert.Nil(t, err)
assert.Equal(t, DD{U: 100, DBase2: &DBase2{S: "abc", DBase1: DBase1{I: 11}}}, d)
})
}
func Test_Copy_struct_with_embedded_struct_error(t *testing.T) {
t.Run("#1: src inherited field requires copying", func(t *testing.T) {
type SBase struct {
I int `copy:",required"`
}
type SS struct {
SBase
U uint
}
type DD struct {
U uint
}
s := SS{U: 100, SBase: SBase{I: 11}}
d := DD{}
err := Copy(&d, s)
assert.ErrorIs(t, err, ErrFieldRequireCopying)
})
t.Run("#2: dst inherited field requires copying", func(t *testing.T) {
type SS struct {
U uint
}
type DBase struct {
I int `copy:",required"`
}
type DD struct {
DBase
U uint
}
s := SS{U: 100}
d := DD{}
err := Copy(&d, s)
assert.ErrorIs(t, err, ErrFieldRequireCopying)
})
}