-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_test.go
More file actions
585 lines (515 loc) · 13.8 KB
/
basic_test.go
File metadata and controls
585 lines (515 loc) · 13.8 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
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
package copier_test
import (
"encoding/json"
"reflect"
"strings"
"testing"
"github.com/epkgs/copier"
)
type Basic struct {
Vstring string
Vint int
Vint8 int8
Vint16 int16
Vint32 int32
Vint64 int64
Vuint uint
Vbool bool
Vfloat float64
Vextra string
vsilent bool
Vdata any
VjsonInt int
VjsonUint uint
VjsonUint64 uint64
VjsonFloat float64
VjsonNumber json.Number
VRenamed string `json:"custom_name"`
}
type BasicPointer struct {
Vstring *string
Vint *int
Vuint *uint
Vbool *bool
Vfloat *float64
Vextra *string
vsilent *bool
Vdata *any
VjsonInt *int
VjsonFloat *float64
VjsonNumber *json.Number
VRenamed *string `json:"custom_name_pointer"`
}
type TypeConversionResult struct {
IntToFloat float32
IntToUint uint
IntToBool bool
IntToString string
UintToInt int
UintToFloat float32
UintToBool bool
UintToString string
BoolToInt int
BoolToUint uint
BoolToFloat float32
BoolToString string
FloatToInt int
FloatToUint uint
FloatToBool bool
FloatToString string
SliceUint8ToString string
StringToSliceUint8 []byte
ArrayUint8ToString string
StringToInt int
StringToUint uint
StringToBool bool
StringToFloat float32
StringToStrSlice []string
StringToIntSlice []int
StringToStrArray [1]string
StringToIntArray [1]int
SliceToMap map[string]any
MapToSlice []any
ArrayToMap map[string]any
MapToArray [1]any
}
func TestBasicTypes(t *testing.T) {
t.Parallel()
input := map[string]any{
"vstring": "foo",
"vint": 42,
"vint8": 42,
"vint16": 42,
"vint32": 42,
"vint64": 42,
"vuint": 42,
"vbool": true,
"vfloat": 42.42,
"vsilent": true,
"vdata": 42,
"vjsonInt": json.Number("1234"),
"vjsonUint": json.Number("1234"),
"vjsonUint64": json.Number("9223372036854775809"), // 2^63 + 1
"vjsonFloat": json.Number("1234.5"),
"vjsonNumber": json.Number("1234.5"),
}
var result Basic
err := copier.Copy(&result, input)
if err != nil {
t.Errorf("got an err: %s", err.Error())
t.FailNow()
}
if result.Vstring != "foo" {
t.Errorf("vstring value should be 'foo': %#v", result.Vstring)
}
if result.Vint != 42 {
t.Errorf("vint value should be 42: %#v", result.Vint)
}
if result.Vint8 != 42 {
t.Errorf("vint8 value should be 42: %#v", result.Vint)
}
if result.Vint16 != 42 {
t.Errorf("vint16 value should be 42: %#v", result.Vint)
}
if result.Vint32 != 42 {
t.Errorf("vint32 value should be 42: %#v", result.Vint)
}
if result.Vint64 != 42 {
t.Errorf("vint64 value should be 42: %#v", result.Vint)
}
if result.Vuint != 42 {
t.Errorf("vuint value should be 42: %#v", result.Vuint)
}
if result.Vbool != true {
t.Errorf("vbool value should be true: %#v", result.Vbool)
}
if result.Vfloat != 42.42 {
t.Errorf("vfloat value should be 42.42: %#v", result.Vfloat)
}
if result.Vextra != "" {
t.Errorf("vextra value should be empty: %#v", result.Vextra)
}
if result.vsilent != false {
t.Error("vsilent should not be set, it is unexported")
}
if result.Vdata != 42 {
t.Error("vdata should be valid")
}
if result.VjsonInt != 1234 {
t.Errorf("vjsonint value should be 1234: %#v", result.VjsonInt)
}
if result.VjsonUint != 1234 {
t.Errorf("vjsonuint value should be 1234: %#v", result.VjsonUint)
}
if result.VjsonUint64 != 9223372036854775809 {
t.Errorf("vjsonuint64 value should be 9223372036854775809: %#v", result.VjsonUint64)
}
if result.VjsonFloat != 1234.5 {
t.Errorf("vjsonfloat value should be 1234.5: %#v", result.VjsonFloat)
}
if !reflect.DeepEqual(result.VjsonNumber, json.Number("1234.5")) {
t.Errorf("vjsonnumber value should be '1234.5': %T, %#v", result.VjsonNumber, result.VjsonNumber)
}
}
func TestBasic_IntWithFloat(t *testing.T) {
t.Parallel()
input := map[string]any{
"vint": float64(42),
}
var result Basic
err := copier.Copy(&result, input)
if err != nil {
t.Fatalf("got an err: %s", err)
}
}
func TestBasic_Merge(t *testing.T) {
t.Parallel()
input := map[string]any{
"vint": 42,
}
var result Basic
result.Vuint = 100
err := copier.Copy(&result, input)
if err != nil {
t.Fatalf("got an err: %s", err)
}
expected := Basic{
Vint: 42,
Vuint: 100,
}
if !reflect.DeepEqual(result, expected) {
t.Fatalf("bad: %#v", result)
}
}
// 测试问题 #46.
func TestBasic_Struct(t *testing.T) {
t.Parallel()
input := map[string]any{
"vdata": map[string]any{
"vstring": "foo",
},
}
var result, inner Basic
result.Vdata = &inner
err := copier.Copy(&result, input)
if err != nil {
t.Fatalf("got an err: %s", err)
}
expected := Basic{
Vdata: &Basic{
Vstring: "foo",
},
}
if !reflect.DeepEqual(result, expected) {
t.Fatalf("bad: %#v", result)
}
}
func TestBasic_interfaceStruct(t *testing.T) {
t.Parallel()
input := map[string]any{
"vstring": "foo",
}
var iface any = &Basic{}
err := copier.Copy(&iface, input)
if err != nil {
t.Fatalf("got an err: %s", err)
}
expected := &Basic{
Vstring: "foo",
}
if !reflect.DeepEqual(iface, expected) {
t.Fatalf("bad: %#v", iface)
}
}
// 问题 187
func TestBasic_interfaceStructNonPtr(t *testing.T) {
t.Parallel()
input := map[string]any{
"vstring": "foo",
}
var iface any = Basic{}
err := copier.Copy(&iface, input)
if err != nil {
t.Fatalf("got an err: %s", err)
}
expected := Basic{
Vstring: "foo",
}
if !reflect.DeepEqual(iface, expected) {
t.Fatalf("bad: %#v", iface)
}
}
func TestBasic_NilInput(t *testing.T) {
t.Parallel()
var input any
result := Basic{
Vstring: "foo",
}
err := copier.Copy(&result, input)
if err != nil {
t.Fatalf("err: %s", err)
}
if result.Vstring != "foo" {
t.Fatalf("bad: %#v", result.Vstring)
}
}
func TestBasic_TypeConversion(t *testing.T) {
input := map[string]any{
"intToFloat": 42,
"intToUint": 42,
"intToBool": 1,
"intToString": 42,
"uintToInt": 42,
"uintToFloat": 42,
"uintToBool": 42,
"uintToString": 42,
"boolToInt": true,
"boolToUint": true,
"boolToFloat": true,
"boolToString": true,
"floatToInt": 42.42,
"floatToUint": 42.42,
"floatToBool": 42.42,
"floatToString": 42.42,
"sliceUint8ToString": []uint8("foo"),
"stringToSliceUint8": "foo",
"arrayUint8ToString": [3]uint8{'f', 'o', 'o'},
"stringToInt": "42",
"stringToUint": "42",
"stringToBool": "1",
"stringToFloat": "42.42",
"stringToStrSlice": "A",
"stringToIntSlice": "42",
"stringToStrArray": "A",
"stringToIntArray": "42",
"sliceToMap": []any{},
"mapToSlice": map[string]any{},
"arrayToMap": []any{},
"mapToArray": map[string]any{},
}
expectedResultStrict := TypeConversionResult{
IntToFloat: 42.0,
IntToUint: 42,
UintToInt: 42,
UintToFloat: 42,
BoolToInt: 0,
BoolToUint: 0,
BoolToFloat: 0,
FloatToInt: 42,
FloatToUint: 42,
StringToIntSlice: []int{},
StringToSliceUint8: []byte{},
StringToStrSlice: []string{},
SliceToMap: map[string]any{},
MapToSlice: []any{},
ArrayToMap: map[string]any{},
}
expectedResultWeak := TypeConversionResult{
IntToFloat: 42.0,
IntToUint: 42,
IntToBool: true,
IntToString: "42",
UintToInt: 42,
UintToFloat: 42,
UintToBool: true,
UintToString: "42",
BoolToInt: 1,
BoolToUint: 1,
BoolToFloat: 1,
BoolToString: "1",
FloatToInt: 42,
FloatToUint: 42,
FloatToBool: true,
FloatToString: "42.42",
SliceUint8ToString: "foo",
StringToSliceUint8: []byte("foo"),
ArrayUint8ToString: "foo",
StringToInt: 42,
StringToUint: 42,
StringToBool: true,
StringToFloat: 42.42,
StringToStrSlice: []string{"A"},
StringToIntSlice: []int{42},
StringToStrArray: [1]string{"A"},
StringToIntArray: [1]int{42},
SliceToMap: map[string]any{},
MapToSlice: []any{},
ArrayToMap: map[string]any{},
MapToArray: [1]any{},
}
// 测试严格类型转换
var resultStrict TypeConversionResult
err := copier.Copy(&resultStrict, input)
if err == nil {
t.Errorf("should return an error")
}
if !reflect.DeepEqual(resultStrict, expectedResultStrict) {
t.Errorf("expected %#v, got: %#v", expectedResultStrict, resultStrict)
}
// 测试弱类型转换
var resultWeak TypeConversionResult
err = copier.Copy(&resultWeak, input, copier.WithWeaklyTypedInput(true))
if err != nil {
t.Fatalf("got an err: %s", err)
}
if !reflect.DeepEqual(resultWeak, expectedResultWeak) {
t.Errorf("expected \n%#v, got: \n%#v", expectedResultWeak, resultWeak)
}
}
// TestWithTagName 测试 WithTagName 函数的功能
func TestWithTagName(t *testing.T) {
// 测试从 map 复制到结构体,使用不同的标签名称
type MapToStructDestination struct {
Field1 string `xml:"CustomField1"`
Field2 int `xml:"CustomField2"`
}
// 使用 map 作为源,测试 WithTagName 选项
mapSrc := map[string]any{
"CustomField1": "test",
"CustomField2": 42,
}
dst := MapToStructDestination{}
err := copier.Copy(&dst, mapSrc, copier.WithTagName("xml"))
if err != nil {
t.Fatalf("Copy failed: %v", err)
}
// 验证结果
if dst.Field1 != "test" {
t.Errorf("Expected Field1 to be 'test', got '%s'", dst.Field1)
}
if dst.Field2 != 42 {
t.Errorf("Expected Field2 to be 42, got %d", dst.Field2)
}
}
// TestWithFieldNameConverter 测试 WithFieldNameConverter 函数的功能
func TestWithFieldNameConverter(t *testing.T) {
// 测试从 map 复制到结构体,使用字段名称转换器
type MapToStructDestination struct {
FieldOne string
FieldTwo int
}
// 使用 map 作为源,包含下划线大写命名的键
mapSrc := map[string]any{
"FIELD_ONE": "test",
"FIELD_TWO": 42,
}
dst := MapToStructDestination{}
err := copier.Copy(&dst, mapSrc,
copier.WithFieldNameConverter(MapToStructDestination{}, func(fieldName string) string {
// 将目标字段的驼峰命名转换为源 map 的下划线大写命名
var result string
for i, char := range fieldName {
if i > 0 && char >= 'A' && char <= 'Z' {
result += "_"
}
result += string(char)
}
return strings.ToUpper(result)
}),
)
if err != nil {
t.Fatalf("Copy failed: %v", err)
}
// 验证结果
if dst.FieldOne != "test" {
t.Errorf("Expected FieldOne to be 'test', got '%s'", dst.FieldOne)
}
if dst.FieldTwo != 42 {
t.Errorf("Expected FieldTwo to be 42, got %d", dst.FieldTwo)
}
}
// TestCopyFunc 测试 copyFunc 函数的功能
func TestCopyFunc(t *testing.T) {
// copyFunc 是内部函数,我们通过测试函数类型的复制来间接测试它
type Source struct {
FuncField func() string
}
type Destination struct {
FuncField func() string
}
// 创建源对象
src := Source{
FuncField: func() string {
return "test function"
},
}
// 复制到目标对象
dst := Destination{}
err := copier.Copy(&dst, src)
if err != nil {
t.Fatalf("Copy failed: %v", err)
}
// 验证函数被正确复制
if dst.FuncField == nil {
t.Error("Expected FuncField to be copied, but got nil")
return
}
result := dst.FuncField()
if result != "test function" {
t.Errorf("Expected FuncField() to return 'test function', got '%s'", result)
}
}
// TestIsZeroValue 测试 isZeroValue 函数的各种情况
func TestIsZeroValue(t *testing.T) {
// 测试从结构体复制到映射,带有 omitempty 标签的零值字段应该被跳过
type StructWithOmitEmpty struct {
ZeroString string `json:"zero_string,omitempty"`
ZeroInt int `json:"zero_int,omitempty"`
ZeroBool bool `json:"zero_bool,omitempty"`
ZeroSlice []string `json:"zero_slice,omitempty"`
ZeroMap map[string]string `json:"zero_map,omitempty"`
ZeroPtr *string `json:"zero_ptr,omitempty"`
NonZeroString string `json:"non_zero_string,omitempty"`
NonZeroInt int `json:"non_zero_int,omitempty"`
}
// 创建测试数据
nonZeroString := "test"
src := StructWithOmitEmpty{
// 零值字段
ZeroString: "",
ZeroInt: 0,
ZeroBool: false,
ZeroSlice: nil,
ZeroMap: nil,
ZeroPtr: nil,
// 非零值字段
NonZeroString: nonZeroString,
NonZeroInt: 42,
}
// 复制到映射
var result map[string]any
err := copier.Copy(&result, src)
if err != nil {
t.Fatalf("Copy failed: %v", err)
}
// 验证结果
if result == nil {
t.Fatal("Expected result to be a map, got nil")
}
// 验证零值字段没有被复制(因为带有 omitempty 标签)
if _, exists := result["zero_string"]; exists {
t.Error("Expected zero_string to be omitted, but it was copied")
}
if _, exists := result["zero_int"]; exists {
t.Error("Expected zero_int to be omitted, but it was copied")
}
if _, exists := result["zero_bool"]; exists {
t.Error("Expected zero_bool to be omitted, but it was copied")
}
if _, exists := result["zero_slice"]; exists {
t.Error("Expected zero_slice to be omitted, but it was copied")
}
if _, exists := result["zero_map"]; exists {
t.Error("Expected zero_map to be omitted, but it was copied")
}
if _, exists := result["zero_ptr"]; exists {
t.Error("Expected zero_ptr to be omitted, but it was copied")
}
// 验证非零值字段被正确复制
if result["non_zero_string"] != nonZeroString {
t.Errorf("Expected non_zero_string to be '%s', got '%v'", nonZeroString, result["non_zero_string"])
}
if result["non_zero_int"] != 42 {
t.Errorf("Expected non_zero_int to be 42, got %v", result["non_zero_int"])
}
}