This repository has been archived by the owner on Dec 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmerge_test.go
741 lines (602 loc) · 16.6 KB
/
merge_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
package conjungo
import (
"encoding/json"
"errors"
"reflect"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
)
var _ = Describe("Options", func() {
var (
testOpts *Options
)
Context("happy path", func() {
BeforeEach(func() {
testOpts = NewOptions()
})
It("not nil", func() {
Expect(testOpts).ToNot(BeNil())
})
It("defailts to overwrite true", func() {
Expect(testOpts.Overwrite).To(BeTrue())
})
It("sets up mergefuncs", func() {
Expect(testOpts.mergeFuncs).ToNot(BeNil())
})
})
Context("set merge funcs", func() {
var (
opt *Options
mf MergeFunc
)
BeforeEach(func() {
opt = NewOptions()
mf = func(t, s reflect.Value, o *Options) (reflect.Value, error) {
return reflect.Value{}, errors.New("special error")
}
})
It("SetTypeMergeFunc sets func", func() {
t := reflect.TypeOf("")
opt.SetTypeMergeFunc(t, mf)
_, err := opt.mergeFuncs.typeFuncs[t](reflect.Value{}, reflect.Value{}, nil)
Expect(err.Error()).To(Equal("special error"))
})
It("SetKindMergeFunc sets func", func() {
k := reflect.ValueOf("").Kind()
opt.SetKindMergeFunc(k, mf)
_, err := opt.mergeFuncs.kindFuncs[k](reflect.Value{}, reflect.Value{}, nil)
Expect(err.Error()).To(Equal("special error"))
})
It("SetDefaultMergeFunc sets func", func() {
opt.SetDefaultMergeFunc(mf)
_, err := opt.mergeFuncs.defaultFunc(reflect.Value{}, reflect.Value{}, nil)
Expect(err.Error()).To(Equal("special error"))
})
})
})
var _ = Describe("Merge", func() {
type Foo struct {
Name string
Size int
Special bool
Tags []interface{}
}
var (
targetMap, sourceMap map[string]interface{}
)
Context("happy path smoke test", func() {
Context("maps", func() {
BeforeEach(func() {
targetMap = map[string]interface{}{
"A": "wrong",
"B": 1,
"C": map[string]interface{}{"foo": "unchanged", "bar": "orig"},
"D": []interface{}{"unchanged", 0},
"E": Foo{Name: "target", Size: 1, Special: false, Tags: []interface{}{"unchanged", 0}},
}
sourceMap = map[string]interface{}{
"A": "correct",
"B": 2,
"C": map[string]interface{}{"bar": "newVal", "safe": "added"},
"D": []interface{}{"added", 1},
"E": Foo{Name: "source", Size: 3, Special: true, Tags: []interface{}{"added", 1}},
}
})
It("merges correctly", func() {
err := Merge(&targetMap, sourceMap, NewOptions())
Expect(err).ToNot(HaveOccurred())
jsonB, errJson := json.Marshal(targetMap)
Expect(errJson).ToNot(HaveOccurred())
expectedJSON := `{
"A": "correct",
"B": 2,
"C": {
"bar": "newVal",
"foo": "unchanged",
"safe": "added"
},
"D": [
"unchanged",
0,
"added",
1
],
"E": {
"Name": "source",
"Size": 3,
"Special": true,
"Tags": [
"unchanged",
0,
"added",
1
]
}
}`
Expect(jsonB).To(MatchJSON(expectedJSON))
})
It("accepts nil options", func() {
err := Merge(&targetMap, sourceMap, nil)
Expect(err).ToNot(HaveOccurred())
})
})
Context("structs", func() {
var (
targetStruct, sourceStruct Foo
)
BeforeEach(func() {
targetStruct = Foo{
Name: "target",
Size: 1,
Special: false,
Tags: []interface{}{"unchanged", 0},
}
sourceStruct = Foo{
Name: "source",
Size: 3,
Special: true,
Tags: []interface{}{"added", 1},
}
})
It("merges correctly", func() {
err := Merge(&targetStruct, sourceStruct, NewOptions())
Expect(err).ToNot(HaveOccurred())
//newMap, ok := merged.(map[string]interface{})
//Expect(ok).To(BeTrue())
jsonB, errJson := json.Marshal(targetStruct)
Expect(errJson).ToNot(HaveOccurred())
expectedJSON := `{
"Name": "source",
"Size": 3,
"Special": true,
"Tags": [
"unchanged",
0,
"added",
1
]
}`
Expect(jsonB).To(MatchJSON(expectedJSON))
})
})
})
Context("happy path - overwrite is false", func() {
var (
err error
)
BeforeEach(func() {
targetMap = map[string]interface{}{
"A": "original",
"B": 1,
"C": map[string]interface{}{"foo": "unchanged", "bar": "orig"},
"D": []interface{}{"unchanged", 0},
}
sourceMap = map[string]interface{}{
"A": "overwritten",
"B": 2,
"C": map[string]interface{}{"bar": "newVal", "baz": "added"},
"D": []interface{}{"added", 1},
"E": "inserted",
}
opts := NewOptions()
opts.Overwrite = false
err = Merge(&targetMap, sourceMap, opts)
})
It("does not error", func() {
Expect(err).ToNot(HaveOccurred())
})
It("does not overwrite a top level string", func() {
Expect(targetMap["A"]).To(Equal("original"))
})
It("does not overwrite a top level int", func() {
Expect(targetMap["B"]).To(Equal(1))
})
It("inserts a new top level string", func() {
Expect(targetMap["E"]).To(Equal("inserted"))
})
Context("sub map", func() {
var (
newSubMap map[string]interface{}
ok bool
)
JustBeforeEach(func() {
newSubMap, ok = targetMap["C"].(map[string]interface{})
Expect(ok).To(BeTrue())
})
It("does not overwrite a sub map value", func() {
Expect(newSubMap["bar"]).To(Equal("orig"))
})
It("inserts a new sub map value", func() {
Expect(newSubMap["baz"]).To(Equal("added"))
})
It("maintains unduplicated values", func() {
Expect(newSubMap["foo"]).To(Equal("unchanged"))
})
})
Context("sub slice", func() {
It("merges properly", func() {
newSubSlice, ok := targetMap["D"].([]interface{})
Expect(ok).To(BeTrue())
Expect(len(newSubSlice)).To(Equal(4))
Expect(newSubSlice).To(ContainElement("unchanged"))
Expect(newSubSlice).To(ContainElement(0))
Expect(newSubSlice).To(ContainElement("added"))
Expect(newSubSlice).To(ContainElement(1))
})
})
Context("pointers", func() {
Context("target is already pointer", func() {
//TODO: implement
//interface of pointer value
})
Context("pointer and value", func() {
Context("target is pointer source is value", func() {
//TODO: implement
})
Context("target is value source is pointer", func() {
//TODO: implement
})
})
})
})
Context("happy path specific types", func() {
DescribeTable("basic types",
func(target, source interface{}) {
err := Merge(&target, source, NewOptions())
Expect(err).ToNot(HaveOccurred())
Expect(target).To(Equal(source))
},
Entry("overwrites string",
"wrong",
"correct",
),
Entry("overwrites int",
0,
1,
),
Entry("overwrites float",
0.0,
1.0,
),
Entry("overwrites bool",
true,
false,
),
Entry("time",
time.Now(),
time.Now().Add(time.Hour),
),
)
Context("merge map", func() {
const (
testKey = "theKey"
)
It("merges correctly", func() {
targetMap = map[string]interface{}{
testKey: map[string]interface{}{"foo": "unchanged", "bar": "orig"},
}
sourceMap = map[string]interface{}{
testKey: map[string]interface{}{"bar": "newVal", "baz": "added"},
}
err := Merge(&targetMap, sourceMap, NewOptions())
Expect(err).ToNot(HaveOccurred())
dataMap, ok := targetMap[testKey].(map[string]interface{})
Expect(ok).To(BeTrue())
Expect(dataMap["foo"]).To(Equal("unchanged"))
Expect(dataMap["bar"]).To(Equal("newVal"))
Expect(dataMap["baz"]).To(Equal("added"))
})
})
Context("nil target", func() {
It("merges correctly", func() {
source := "bar"
var target string
err := Merge(&target, source, NewOptions())
Expect(err).ToNot(HaveOccurred())
Expect(target).To(Equal("bar"))
})
})
Context("nil source value", func() {
It("doesnt error", func() {
target := "foo"
err := Merge(&target, nil, NewOptions())
Expect(err).ToNot(HaveOccurred())
Expect(target).To(Equal("foo"))
})
})
Context("nil source and target value", func() {
It("doesnt error", func() {
var target int
err := Merge(&target, nil, NewOptions())
Expect(err).ToNot(HaveOccurred())
Expect(target).To(BeZero())
})
})
Context("nil pointer value", func() {
Context("source nil", func() {
It("doesnt error", func() {
target := "foo"
var source *string
err := Merge(&target, source, NewOptions())
Expect(err).ToNot(HaveOccurred())
Expect(target).To(Equal("foo")) // unchanged
})
})
Context("target nil", func() {
It("doesnt error", func() {
source := "foo"
var target *string
err := Merge(target, &source, NewOptions())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("target can not be zero value"))
})
})
})
Context("pointers", func() {
Context("both pointers", func() {
It("merges correctly", func() {
source := "bar"
var target string
err := Merge(&target, &source, NewOptions())
Expect(err).ToNot(HaveOccurred())
Expect(target).To(Equal("bar"))
})
})
})
Context("merge slice", func() {
It("merges correctly", func() {
target := []interface{}{"unchanged", 0}
source := []interface{}{"added", 1}
err := Merge(&target, source, NewOptions())
Expect(err).ToNot(HaveOccurred())
Expect(len(target)).To(Equal(4))
Expect(target).To(ContainElement("unchanged"))
Expect(target).To(ContainElement(0))
Expect(target).To(ContainElement("added"))
Expect(target).To(ContainElement(1))
})
})
Context("merge struct", func() {
type Thing struct {
Foo string
}
It("merges correctly", func() {
target := Thing{Foo: "bar"}
source := Thing{Foo: "baz"}
err := Merge(&target, source, NewOptions())
Expect(err).ToNot(HaveOccurred())
Expect(target.Foo).To(Equal("baz"))
})
It("target struct and source value merges correctly", func() {
target := Thing{Foo: "bar"}
source := reflect.ValueOf(Thing{Foo: "baz"})
err := Merge(&target, source, NewOptions())
Expect(err).ToNot(HaveOccurred())
Expect(target.Foo).To(Equal("baz"))
})
It("target value and source struct merges correctly", func() {
target := Thing{Foo: "bar"}
source := Thing{Foo: "baz"}
tVal := reflect.ValueOf(&target)
err := Merge(tVal, source, NewOptions())
Expect(err).ToNot(HaveOccurred())
Expect(target.Foo).To(Equal("baz"))
})
It("both values merges correctly", func() {
target := Thing{Foo: "bar"}
source := reflect.ValueOf(Thing{Foo: "baz"})
err := Merge(reflect.ValueOf(&target), source, NewOptions())
Expect(err).ToNot(HaveOccurred())
Expect(target.Foo).To(Equal("baz"))
})
It("pointer to target value errors", func() {
target := Thing{Foo: "bar"}
source := reflect.ValueOf(Thing{Foo: "baz"})
tVal := reflect.ValueOf(&target)
err := Merge(tVal, source, NewOptions())
Expect(err).ToNot(HaveOccurred())
Expect(target.Foo).To(Equal("baz"))
})
})
Context("merge errors (interfaces)", func() {
It("merges", func() {
target := errors.New("some err")
source := errors.New("other err")
opts := NewOptions()
err := Merge(target, source, opts)
Expect(err).ToNot(HaveOccurred())
Expect(target.Error()).To(ContainSubstring("other"))
})
})
Context("merge functions", func() {
It("merges", func() {
target := func() string { return "no" }
source := func() string { return "yes" }
opts := NewOptions()
err := Merge(&target, source, opts)
Expect(err).ToNot(HaveOccurred())
ret := reflect.ValueOf(target).Call(nil)
Expect(ret[0].Interface()).To(Equal("yes"))
})
})
})
Context("failure modes", func() {
Context("target is not a pointer", func() {
It("returns error", func() {
err := Merge("foo", "bar", NewOptions())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("target must be a pointer"))
})
})
Context("merge func returns error", func() {
It("returns an error", func() {
target := errors.New("some err")
source := errors.New("other err")
opts := NewOptions()
// define a merge func that always errors for the error type
ve := reflect.ValueOf(errors.New(""))
opts.mergeFuncs.setTypeMergeFunc(
// error is an interface around *errors.errorString so dereference
reflect.Indirect(ve).Type(),
erroringMergeFunc,
)
err := Merge(target, source, opts)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("returns error"))
})
})
Context("type mismatch", func() {
It("returns an error", func() {
target := 0
source := ""
err := Merge(&target, source, NewOptions())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Types do not match"))
})
})
Context("NewOptions not used to create options", func() {
It("returns an error", func() {
target := ""
source := ""
err := Merge(&target, source, &Options{})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("use NewOptions()"))
})
})
Context("type mismatch deeper in the tree", func() {
It("returns an error", func() {
testKey := "theKey"
targetMap = map[string]interface{}{
testKey: 0,
}
sourceMap = map[string]interface{}{
testKey: "",
}
err := Merge(&targetMap, sourceMap, NewOptions())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Types do not match"))
})
})
Context("merge returns wrong type", func() {
type Bar struct{}
It("returns an error", func() {
target := Bar{}
source := Bar{}
opts := NewOptions()
// define a merge func that returns wrong type
opts.mergeFuncs.setTypeMergeFunc(
reflect.TypeOf(Bar{}),
func(t, s reflect.Value, o *Options) (reflect.Value, error) {
return reflect.ValueOf("a string"), nil
},
)
err := Merge(&target, source, opts)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("Merge failed: expected merged result to be conjungo.Bar but got string"))
})
})
})
})
//TODO: maybe some of these tests are duplicates. Dedup them sometime.
var _ = Describe("MergeMapStrIFace", func() {
var (
targetMap, sourceMap map[string]interface{}
err error
)
BeforeEach(func() {
targetMap = map[string]interface{}{
"A": "original",
"B": 1,
"C": map[string]interface{}{"foo": "unchanged", "bar": "orig"},
"D": []interface{}{"unchanged", 0},
}
sourceMap = map[string]interface{}{
"A": "overwritten",
"B": 2,
"C": map[string]interface{}{"bar": "newVal", "baz": "added"},
"D": []interface{}{"added", 1},
"E": "inserted",
}
})
Context("happy path", func() {
JustBeforeEach(func() {
opts := NewOptions()
opts.Overwrite = false
err = Merge(&targetMap, sourceMap, opts)
})
It("does not error", func() {
Expect(err).ToNot(HaveOccurred())
})
It("does not overwrite a top level string", func() {
Expect(targetMap["A"]).To(Equal("original"))
})
It("does not overwrite a top level int", func() {
Expect(targetMap["B"]).To(Equal(1))
})
It("inserts a new top level string", func() {
Expect(targetMap["E"]).To(Equal("inserted"))
})
})
Context("Merge fails", func() {
BeforeEach(func() {
targetMap["F"] = errors.New("some err")
sourceMap["F"] = errors.New("other err")
opts := NewOptions()
// define a merge func that always errors for the error type
opts.mergeFuncs.setTypeMergeFunc(
reflect.TypeOf(errors.New("")),
erroringMergeFunc,
)
err = Merge(&targetMap, sourceMap, opts)
})
It("returns an error", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("returns error"))
})
})
})
var _ = Describe("isEmpty", func() {
It("not empty", func() {
res := isEmpty(reflect.ValueOf("not empty"))
Expect(res).ToNot(BeTrue())
})
It("not valid", func() {
res := isEmpty(reflect.ValueOf(nil))
Expect(res).To(BeTrue())
})
It("empty map", func() {
var v map[string]interface{}
res := isEmpty(reflect.ValueOf(v))
Expect(res).To(BeTrue())
})
It("empty slice", func() {
var v []interface{}
res := isEmpty(reflect.ValueOf(v))
Expect(res).To(BeTrue())
})
It("nil pointer", func() {
var v *int = nil
res := isEmpty(reflect.ValueOf(v))
Expect(res).To(BeTrue())
})
It("nil chan", func() {
var v chan int
res := isEmpty(reflect.ValueOf(v))
Expect(res).To(BeTrue())
})
It("nil func", func() {
var v func()
res := isEmpty(reflect.ValueOf(v))
Expect(res).To(BeTrue())
})
It("nil interface", func() {
var v interface{}
res := isEmpty(reflect.ValueOf(v))
Expect(res).To(BeTrue())
})
})
func erroringMergeFunc(t, s reflect.Value, o *Options) (reflect.Value, error) {
return reflect.Value{}, errors.New("returns error")
}