-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.go
55 lines (55 loc) · 1.61 KB
/
doc.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
// Package merger can merge different Go types together.
//
// This can be useful for:
// * Merging configurations from different sources
// * Deduplicate items before serialization
// * Setting field preferences
//
// Merge Algorithm
//
// Let A be the first arbitrary value and B be the second arbitrary value with precedence.
//
// boolean, numeric and string types will be overwritten by the argument with precedence (B).
// slice and array types will be concatenated (A ∥ B).
// struct and map types will be merged together giving a union of all fields, where the values of them are merged too (A ∪ B)
//
// Merge Examples
//
// Merging boolean, numeric, string types (overwrite):
// A := "a"
// B := 4
// V, _ := merger.Merge(A, B)
// // V: 4
//
// Merging map, struct types (union):
// A := map[string]int{
// "x": 1,
// "y": 2,
// }
// B := map[string]int{
// "a": 1,
// "b": 2,
// }
// V, _ := merger.Merge(A, B)
// // V: map[string]int{"a": 1, "b": 2, "x": 1, "y": 2}
//
// Merging slice, array types (concat):
// A := []int{1, 2, 3}
// B := []int{4, 5, 6}
// V, _ := merger.Merge(A, B)
// // V: []int{1, 2, 3, 4, 5, 6}
//
// Merging embedded types:
// A := map[string]interface{}{
// "a": []int{1, 2},
// "b": "aAaAaA",
// "c": map[int]int{1: 10, 2: 20},
// }
// B := map[string]interface{}{
// "a": []int{3, 4},
// "b": "bBbBbB",
// "c": map[int]int{1: 100, 3: 30},
// }
// V, _ := merger.Merge(A, B)
// // V: map[string]interface{}{"a": []int{1,2,3,4}, "b": "bBbBbB", "c": map[int]int{1:100, 2:20, 3:30}}
package merger