-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstruct_test.go
644 lines (638 loc) · 19.6 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
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
// Copyright 2022 Alexandre Dutra
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package goalesce
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_coalescer_deepMergeStruct(t *testing.T) {
t.Run("basic", func(t *testing.T) {
type foo struct {
FieldInt int
}
type bar struct {
FieldInt int
FieldFoo foo
FieldIntPtr *int
FieldBarPtr *bar
unexported int
FieldInterface interface{}
FieldMap map[int]string
FieldMapAtomic map[int]string `goalesce:"atomic"`
}
tests := []struct {
name string
v1 bar
v2 bar
want bar
}{
{
"zeroes",
bar{},
bar{},
bar{},
},
{
"non zeroes int",
bar{FieldInt: 1},
bar{FieldInt: 2},
bar{FieldInt: 2},
},
{
"non zeroes bar",
bar{FieldFoo: foo{FieldInt: 1}},
bar{FieldFoo: foo{FieldInt: 2}},
bar{FieldFoo: foo{FieldInt: 2}},
},
{
"mixed zeroes intptr1",
bar{FieldIntPtr: intPtr(1)},
bar{FieldIntPtr: nil},
bar{FieldIntPtr: intPtr(1)},
},
{
"mixed zeroes intptr2",
bar{FieldIntPtr: nil},
bar{FieldIntPtr: intPtr(2)},
bar{FieldIntPtr: intPtr(2)},
},
{
"mixed zeroes barptr1",
bar{FieldBarPtr: &bar{FieldInt: 1}},
bar{FieldBarPtr: nil},
bar{FieldBarPtr: &bar{FieldInt: 1}},
},
{
"mixed zeroes barptr 2",
bar{FieldBarPtr: nil},
bar{FieldBarPtr: &bar{FieldInt: 2}},
bar{FieldBarPtr: &bar{FieldInt: 2}},
},
{
"non zeroes intptr",
bar{FieldIntPtr: intPtr(1)},
bar{FieldIntPtr: intPtr(2)},
bar{FieldIntPtr: intPtr(2)},
},
{
"non zeroes barptr",
bar{FieldBarPtr: &bar{FieldInt: 1}},
bar{FieldBarPtr: &bar{FieldInt: 2}},
bar{FieldBarPtr: &bar{FieldInt: 2}},
},
{
"non zeroes unexported",
bar{unexported: 1},
bar{unexported: 2},
bar{},
},
{
"field interface nil 1",
bar{FieldInterface: &foo{FieldInt: 1}},
bar{FieldInterface: nil},
bar{FieldInterface: &foo{FieldInt: 1}},
},
{
"field interface nil 2",
bar{FieldInterface: nil},
bar{FieldInterface: &foo{FieldInt: 1}},
bar{FieldInterface: &foo{FieldInt: 1}},
},
{
"field interface same types",
bar{FieldInterface: &bar{FieldInt: 1, FieldMap: map[int]string{1: "a"}}},
bar{FieldInterface: &bar{FieldInt: 0, FieldMap: map[int]string{2: "b"}}},
bar{FieldInterface: &bar{FieldInt: 1, FieldMap: map[int]string{1: "a", 2: "b"}}},
},
{
"field map",
bar{FieldMap: map[int]string{1: "a", 2: "b"}},
bar{FieldMap: map[int]string{2: "a", 3: "b"}},
bar{FieldMap: map[int]string{1: "a", 2: "a", 3: "b"}},
},
{
"field map atomic",
bar{FieldMapAtomic: map[int]string{1: "a", 2: "b"}},
bar{FieldMapAtomic: map[int]string{2: "a", 3: "b"}},
bar{FieldMapAtomic: map[int]string{2: "a", 3: "b"}},
},
{
"non zeroes unexported",
bar{unexported: 1},
bar{unexported: 2},
bar{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := newCoalescer()
got, err := c.deepMergeStruct(reflect.ValueOf(tt.v1), reflect.ValueOf(tt.v2))
require.NoError(t, err)
assert.Equal(t, tt.want, got.Interface())
assertNotSame(t, tt.v1, got.Interface())
assertNotSame(t, tt.v2, got.Interface())
})
}
})
t.Run("slice fields", func(t *testing.T) {
type foo struct {
FieldInt int
FieldIntPtr *int
}
type nested struct {
FieldKey int
FieldNonKey string
FieldInts []int
}
type bar struct {
FieldInts []int
FieldIntsAtomic []int `goalesce:"atomic"`
FieldIntsUnion []int `goalesce:"union"`
FieldIntsAppend []int `goalesce:"append"`
FieldIntsIndex []int `goalesce:"index"`
FieldIntsIndexArray [3]int `goalesce:"index"`
FieldFoos []foo
FieldFoosAtomic []foo `goalesce:"atomic"`
FieldFoosUnion []foo `goalesce:"union"`
FieldFoosAppend []foo `goalesce:"append"`
FieldFoosIndex []foo `goalesce:"index"`
FieldFoosIndexArray [3]foo `goalesce:"index"`
FieldFoosMergeKey []foo `goalesce:"id:FieldInt"`
FieldFooPtrsMergeKey []*foo `goalesce:"id:FieldIntPtr"`
FieldNestedSlice []nested `goalesce:"id:FieldKey"`
}
tests := []struct {
name string
v1 bar
v2 bar
want bar
}{
{
"slice ints default",
bar{FieldInts: []int{1, 2}},
bar{FieldInts: []int{2, 3}},
bar{FieldInts: []int{2, 3}},
},
{
"slice ints atomic",
bar{FieldIntsAtomic: []int{1, 2}},
bar{FieldIntsAtomic: []int{2, 3}},
bar{FieldIntsAtomic: []int{2, 3}},
},
{
"slice ints union",
bar{FieldIntsUnion: []int{1, 2}},
bar{FieldIntsUnion: []int{2, 3}},
bar{FieldIntsUnion: []int{1, 2, 3}},
},
{
"slice ints append",
bar{FieldIntsAppend: []int{1, 2}},
bar{FieldIntsAppend: []int{2, 3}},
bar{FieldIntsAppend: []int{1, 2, 2, 3}},
},
{
"slice ints index",
bar{FieldIntsIndex: []int{1, 2, 3}},
bar{FieldIntsIndex: []int{-1, -2}},
bar{FieldIntsIndex: []int{-1, -2, 3}},
},
{
"array ints index",
bar{FieldIntsIndexArray: [3]int{1, 2, 3}},
bar{FieldIntsIndexArray: [3]int{-1, -2}},
bar{FieldIntsIndexArray: [3]int{-1, -2, 3}},
},
{
"slice foos default",
bar{FieldFoos: []foo{{FieldInt: 1}, {FieldInt: 2}}},
bar{FieldFoos: []foo{{FieldInt: 2}, {FieldInt: 3}}},
bar{FieldFoos: []foo{{FieldInt: 2}, {FieldInt: 3}}},
},
{
"slice foos atomic",
bar{FieldFoosAtomic: []foo{{FieldInt: 1}, {FieldInt: 2}}},
bar{FieldFoosAtomic: []foo{{FieldInt: 2}, {FieldInt: 3}}},
bar{FieldFoosAtomic: []foo{{FieldInt: 2}, {FieldInt: 3}}},
},
{
"slice foos union",
bar{FieldFoosUnion: []foo{{FieldInt: 1}, {FieldInt: 2}}},
bar{FieldFoosUnion: []foo{{FieldInt: 2}, {FieldInt: 3}}},
bar{FieldFoosUnion: []foo{{FieldInt: 1}, {FieldInt: 2}, {FieldInt: 3}}},
},
{
"slice foos append",
bar{FieldFoosAppend: []foo{{FieldInt: 1}, {FieldInt: 2}}},
bar{FieldFoosAppend: []foo{{FieldInt: 2}, {FieldInt: 3}}},
bar{FieldFoosAppend: []foo{{FieldInt: 1}, {FieldInt: 2}, {FieldInt: 2}, {FieldInt: 3}}},
},
{
"slice foos index",
bar{FieldFoosIndex: []foo{{FieldInt: 1}, {FieldInt: 2}, {FieldInt: 3}}},
bar{FieldFoosIndex: []foo{{FieldInt: -1}, {FieldInt: -2}}},
bar{FieldFoosIndex: []foo{{FieldInt: -1}, {FieldInt: -2}, {FieldInt: 3}}},
},
{
"array foos index",
bar{FieldFoosIndexArray: [3]foo{{FieldInt: 1}, {FieldInt: 2}, {FieldInt: 3}}},
bar{FieldFoosIndexArray: [3]foo{{FieldInt: -1}, {FieldInt: -2}}},
bar{FieldFoosIndexArray: [3]foo{{FieldInt: -1}, {FieldInt: -2}, {FieldInt: 3}}},
},
{
"slice foos merge key",
bar{FieldFoosMergeKey: []foo{{FieldInt: 1}, {FieldInt: 2}}},
bar{FieldFoosMergeKey: []foo{{FieldInt: 2}, {FieldInt: 3}}},
bar{FieldFoosMergeKey: []foo{{FieldInt: 1}, {FieldInt: 2}, {FieldInt: 3}}},
},
{
"slice foo ptrs merge key",
bar{FieldFooPtrsMergeKey: []*foo{{FieldIntPtr: intPtr(1)}, {FieldIntPtr: intPtr(2)}}},
bar{FieldFooPtrsMergeKey: []*foo{{FieldIntPtr: intPtr(2)}, {FieldIntPtr: intPtr(3)}}},
bar{FieldFooPtrsMergeKey: []*foo{{FieldIntPtr: intPtr(1)}, {FieldIntPtr: intPtr(2)}, {FieldIntPtr: intPtr(3)}}},
},
{
"nested slice",
bar{FieldNestedSlice: []nested{{FieldKey: 1, FieldNonKey: "abc", FieldInts: []int{1, 2}}}},
bar{FieldNestedSlice: []nested{{FieldKey: 1, FieldNonKey: "def", FieldInts: []int{2, 3}}}},
bar{FieldNestedSlice: []nested{{FieldKey: 1, FieldNonKey: "def", FieldInts: []int{2, 3}}}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := newCoalescer()
got, err := c.deepMergeStruct(reflect.ValueOf(tt.v1), reflect.ValueOf(tt.v2))
require.NoError(t, err)
assert.Equal(t, tt.want, got.Interface())
assertNotSame(t, tt.v1, got.Interface())
assertNotSame(t, tt.v2, got.Interface())
})
}
})
t.Run("options", func(t *testing.T) {
t.Run("field merger", func(t *testing.T) {
type foo struct {
Int int
}
fieldMerger := func(v1, v2 reflect.Value) (reflect.Value, error) {
if v2.Int() == 0 {
return reflect.Value{}, nil // delegate the merge to the default merger
}
return reflect.ValueOf((int)(v1.Int() / v2.Int())), nil
}
c := newCoalescer(WithFieldMerger(reflect.TypeOf(foo{}), "Int", fieldMerger))
got, err := c.deepMergeStruct(reflect.ValueOf(foo{6}), reflect.ValueOf(foo{2}))
require.NoError(t, err)
assert.Equal(t, foo{3}, got.Interface())
got, err = c.deepMergeStruct(reflect.ValueOf(foo{6}), reflect.ValueOf(foo{0}))
require.NoError(t, err)
assert.Equal(t, foo{6}, got.Interface())
})
t.Run("field merger provider", func(t *testing.T) {
type foo struct {
FieldInts []int
}
fieldMerger := func(v1, v2 reflect.Value) (reflect.Value, error) {
return reflect.ValueOf([]int{1, 2, 3}), nil
}
c := newCoalescer(WithFieldMergerProvider(reflect.TypeOf(foo{}), "FieldInts", func(DeepMergeFunc, DeepCopyFunc) DeepMergeFunc {
return fieldMerger
}))
got, err := c.deepMergeStruct(reflect.ValueOf(foo{FieldInts: []int{1, 2}}), reflect.ValueOf(foo{FieldInts: []int{2, 3}}))
require.NoError(t, err)
assert.Equal(t, foo{FieldInts: []int{1, 2, 3}}, got.Interface())
})
t.Run("atomic field", func(t *testing.T) {
type foo struct {
FieldInts map[int]string
}
c := newCoalescer(WithAtomicFieldMerge(reflect.TypeOf(foo{}), "FieldInts"))
got, err := c.deepMergeStruct(
reflect.ValueOf(foo{FieldInts: map[int]string{1: "abc"}}),
reflect.ValueOf(foo{FieldInts: map[int]string{1: "def"}}),
)
require.NoError(t, err)
assert.Equal(t, foo{FieldInts: map[int]string{1: "def"}}, got.Interface())
})
t.Run("field set-union", func(t *testing.T) {
type foo struct {
FieldInts []int
}
c := newCoalescer(WithFieldSetUnionMerge(reflect.TypeOf(foo{}), "FieldInts"))
got, err := c.deepMergeStruct(
reflect.ValueOf(foo{FieldInts: []int{1, 2}}),
reflect.ValueOf(foo{FieldInts: []int{2, 3}}),
)
require.NoError(t, err)
assert.Equal(t, foo{FieldInts: []int{1, 2, 3}}, got.Interface())
})
t.Run("field list-append", func(t *testing.T) {
type foo struct {
FieldInts []int
}
c := newCoalescer(WithFieldListAppendMerge(reflect.TypeOf(foo{}), "FieldInts"))
got, err := c.deepMergeStruct(
reflect.ValueOf(foo{FieldInts: []int{1, 2}}),
reflect.ValueOf(foo{FieldInts: []int{2, 3}}),
)
require.NoError(t, err)
assert.Equal(t, foo{FieldInts: []int{1, 2, 2, 3}}, got.Interface())
})
t.Run("field merge-by-index", func(t *testing.T) {
type foo struct {
FieldInts []int
}
c := newCoalescer(WithFieldMergeByIndex(reflect.TypeOf(foo{}), "FieldInts"))
got, err := c.deepMergeStruct(
reflect.ValueOf(foo{FieldInts: []int{1, 2}}),
reflect.ValueOf(foo{FieldInts: []int{-1}}),
)
require.NoError(t, err)
assert.Equal(t, foo{FieldInts: []int{-1, 2}}, got.Interface())
})
t.Run("field merge-by-id", func(t *testing.T) {
type bar struct {
Name string
}
type foo struct {
FieldBars []bar
}
c := newCoalescer(WithFieldMergeByID(reflect.TypeOf(foo{}), "FieldBars", "Name"))
got, err := c.deepMergeStruct(
reflect.ValueOf(foo{FieldBars: []bar{{"a"}, {"b"}}}),
reflect.ValueOf(foo{FieldBars: []bar{{"b"}, {"a"}}}),
)
require.NoError(t, err)
assert.Equal(t, foo{FieldBars: []bar{{"a"}, {"b"}}}, got.Interface())
})
t.Run("field merge-by-key-func", func(t *testing.T) {
type foo struct {
FieldInts []int
}
c := newCoalescer(WithFieldMergeByKeyFunc(reflect.TypeOf(foo{}), "FieldInts", SliceUnion))
got, err := c.deepMergeStruct(
reflect.ValueOf(foo{FieldInts: []int{1, 2}}),
reflect.ValueOf(foo{FieldInts: []int{2, 3}}),
)
require.NoError(t, err)
assert.Equal(t, foo{FieldInts: []int{1, 2, 3}}, got.Interface())
})
})
t.Run("tag errors", func(t *testing.T) {
type foo struct {
FieldInt int
}
type unknownStrategy struct {
FieldInts []int `goalesce:"unknown"`
}
type invalidAppend struct {
FieldInt int `goalesce:"append"`
}
type invalidUnion struct {
FieldInt int `goalesce:"union"`
}
type invalidIndex struct {
FieldInt int `goalesce:"index"`
}
type invalidMerge struct {
FieldInt int `goalesce:"id"`
}
type missingKey struct {
FieldInts []int `goalesce:"id"`
}
type missingKey2 struct {
FieldInts []int `goalesce:"id:"`
}
type missingKey3 struct {
FieldInts []int `goalesce:"id key"`
}
type wrongElemType struct {
FieldInts []int `goalesce:"id:irrelevant"`
}
type unknownField struct {
FieldFoos []foo `goalesce:"id:unknown"`
FieldFooPtrs []*foo `goalesce:"id:unknown"`
}
tests := []struct {
name string
v1 interface{}
v2 interface{}
want string
}{
{
"unknown strategy",
unknownStrategy{FieldInts: []int{1, 2}},
unknownStrategy{FieldInts: []int{2, 3}},
"field goalesce.unknownStrategy.FieldInts: unknown merge strategy: unknown",
},
{
"invalid append",
invalidAppend{FieldInt: 1},
invalidAppend{FieldInt: 2},
"field goalesce.invalidAppend.FieldInt: append strategy is only supported for slices",
},
{
"invalid union",
invalidUnion{FieldInt: 1},
invalidUnion{FieldInt: 2},
"field goalesce.invalidUnion.FieldInt: union strategy is only supported for slices",
},
{
"invalid index",
invalidIndex{FieldInt: 1},
invalidIndex{FieldInt: 2},
"field goalesce.invalidIndex.FieldInt: index strategy is only supported for slices and arrays",
},
{
"invalid merge",
invalidMerge{FieldInt: 1},
invalidMerge{FieldInt: 2},
"field goalesce.invalidMerge.FieldInt: id strategy is only supported for slices",
},
{
"missing merge key",
missingKey{FieldInts: []int{1}},
missingKey{FieldInts: []int{2}},
"field goalesce.missingKey.FieldInts: id strategy must be followed by a colon and the merge key",
},
{
"missing merge key 2",
missingKey2{FieldInts: []int{1}},
missingKey2{FieldInts: []int{2}},
"field goalesce.missingKey2.FieldInts: id strategy must be followed by a colon and the merge key",
},
{
"missing merge key 3",
missingKey3{FieldInts: []int{1}},
missingKey3{FieldInts: []int{2}},
"field goalesce.missingKey3.FieldInts: id strategy must be followed by a colon and the merge key",
},
{
"wrong element type",
wrongElemType{FieldInts: []int{1}},
wrongElemType{FieldInts: []int{2}},
"field goalesce.wrongElemType.FieldInts: expecting slice of struct or pointer thereto, got: []int",
},
{
"unknown field",
unknownField{FieldFoos: []foo{{FieldInt: 1}}},
unknownField{FieldFoos: []foo{{FieldInt: 2}}},
"field goalesce.unknownField.FieldFoos: slice element type goalesce.foo has no field named unknown",
},
{
"unknown field ptr",
unknownField{FieldFooPtrs: []*foo{{FieldInt: 1}}},
unknownField{FieldFooPtrs: []*foo{{FieldInt: 2}}},
"field goalesce.unknownField.FieldFoos: slice element type goalesce.foo has no field named unknown",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := newCoalescer()
_, err := c.deepMergeStruct(reflect.ValueOf(tt.v1), reflect.ValueOf(tt.v2))
assert.EqualError(t, err, tt.want)
})
}
})
t.Run("interface field", func(t *testing.T) {
type foo struct {
Bird Bird
}
c := newCoalescer()
merged, err := c.deepMergeStruct(reflect.ValueOf(foo{Bird: &Duck{"Donald"}}), reflect.ValueOf(foo{Bird: &Goose{"Scrooge"}}))
assert.Equal(t, foo{Bird: &Goose{"Scrooge"}}, merged.Interface())
assert.NoError(t, err)
})
t.Run("generic error", func(t *testing.T) {
type foo struct {
FieldInt int
}
c := newCoalescer(withMockDeepMergeError)
_, err := c.deepMergeStruct(reflect.ValueOf(foo{FieldInt: 1}), reflect.ValueOf(foo{FieldInt: 2}))
assert.EqualError(t, err, "mock DeepMerge error")
})
}
func Test_newMergeByField(t *testing.T) {
type User struct {
ID int
Name *string
}
u := User{ID: 1, Name: stringPtr("Alice")}
t.Run("on struct", func(t *testing.T) {
mergeKeyFunc := newMergeByField("ID")
mergeKey, err := mergeKeyFunc(-1, reflect.ValueOf(u))
assert.Equal(t, 1, mergeKey.Interface())
assert.NoError(t, err)
})
t.Run("on pointer to struct", func(t *testing.T) {
mergeKeyFunc := newMergeByField("ID")
mergeKey, err := mergeKeyFunc(-1, reflect.ValueOf(u))
assert.Equal(t, 1, mergeKey.Interface())
assert.NoError(t, err)
})
t.Run("on pointer field", func(t *testing.T) {
mergeKeyFunc := newMergeByField("Name")
mergeKey, err := mergeKeyFunc(-1, reflect.ValueOf(u))
assert.Equal(t, "Alice", mergeKey.String())
assert.NoError(t, err)
})
t.Run("on pointer to struct and pointer field", func(t *testing.T) {
mergeKeyFunc := newMergeByField("Name")
mergeKey, err := mergeKeyFunc(-1, reflect.ValueOf(u))
assert.Equal(t, "Alice", mergeKey.String())
assert.NoError(t, err)
})
t.Run("not a struct", func(t *testing.T) {
mergeKeyFunc := newMergeByField("Name")
mergeKey, err := mergeKeyFunc(-1, reflect.ValueOf(123))
assert.False(t, mergeKey.IsValid())
assert.ErrorContains(t, err, "expecting struct or pointer thereto, got: int")
})
t.Run("not a struct, pointer", func(t *testing.T) {
mergeKeyFunc := newMergeByField("Name")
mergeKey, err := mergeKeyFunc(-1, reflect.ValueOf(intPtr(123)))
assert.False(t, mergeKey.IsValid())
assert.ErrorContains(t, err, "expecting struct or pointer thereto, got: *int")
})
t.Run("invalid field", func(t *testing.T) {
mergeKeyFunc := newMergeByField("NonExistent")
mergeKey, err := mergeKeyFunc(-1, reflect.ValueOf(u))
assert.False(t, mergeKey.IsValid())
assert.ErrorContains(t, err, "struct type goalesce.User has no field named NonExistent")
})
t.Run("invalid field on pointer to struct", func(t *testing.T) {
mergeKeyFunc := newMergeByField("NonExistent")
mergeKey, err := mergeKeyFunc(-1, reflect.ValueOf(&u))
assert.False(t, mergeKey.IsValid())
assert.ErrorContains(t, err, "struct type goalesce.User has no field named NonExistent")
})
}
func Test_coalescer_deepCopyStruct(t *testing.T) {
type Foo struct {
FieldInt int
FieldStr string
FieldPtr *string
}
tests := []struct {
name string
v reflect.Value
want reflect.Value
wantErr assert.ErrorAssertionFunc
opts []Option
}{
{
name: "zero",
v: reflect.ValueOf(Foo{}),
want: reflect.ValueOf(Foo{}),
},
{
name: "non zero",
v: reflect.ValueOf(Foo{
FieldInt: 123,
FieldStr: "abc",
FieldPtr: stringPtr("def"),
}),
want: reflect.ValueOf(Foo{
FieldInt: 123,
FieldStr: "abc",
FieldPtr: stringPtr("def"),
}),
},
{
name: "error",
v: reflect.ValueOf(Foo{FieldInt: 123}),
wantErr: assert.Error,
opts: []Option{withMockDeepCopyError},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := newCoalescer(tt.opts...)
got, err := c.deepCopyStruct(tt.v)
if err == nil {
assert.Equal(t, tt.want.Interface(), got.Interface())
assertNotSame(t, tt.v.Interface(), got.Interface())
} else {
assert.False(t, got.IsValid())
}
if tt.wantErr != nil {
tt.wantErr(t, err)
} else {
assert.NoError(t, err)
}
})
}
}