-
Notifications
You must be signed in to change notification settings - Fork 15
/
utils.go
42 lines (33 loc) · 1.05 KB
/
utils.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
package onion
// MergeLayersData is an helper function to merge more layers in one.
// Following slice order, a previous layer key is overriden by an equal key in
// next layer.
func mergeLayersData(layers ...map[string]interface{}) map[string]interface{} {
if len(layers) == 0 {
return map[string]interface{}{}
}
mergedLayer := layers[len(layers)-1]
layers = layers[:len(layers)-1]
for i := len(layers) - 1; i >= 0; i-- {
mergedLayer = mergeKeys(mergedLayer, layers[i])
}
return mergedLayer
}
// mergeKeys recursively merge right into left, never replacing any key that already exists in left
func mergeKeys(left, right map[string]interface{}) map[string]interface{} {
if left == nil {
return right
}
for key, rightVal := range right {
if _, present := left[key]; !present {
left[key] = rightVal
continue
}
leftMap, isLeftValAMap := left[key].(map[string]interface{})
rightMap, isRightValAMap := rightVal.(map[string]interface{})
if isLeftValAMap && isRightValAMap {
left[key] = mergeKeys(leftMap, rightMap)
}
}
return left
}