forked from peterbourgon/mergemap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergemap.go
49 lines (44 loc) · 1.14 KB
/
mergemap.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
package mergemap
import (
"errors"
"reflect"
)
var (
MaxDepth = 32
)
// Merge recursively merges the src and dst maps. Key conflicts are resolved by
// preferring src, or recursively descending, if both src and dst are maps.
func Merge(dst, src map[string]interface{}) (map[string]interface{}, error) {
return merge(dst, src, 0)
}
func merge(dst, src map[string]interface{}, depth int) (map[string]interface{}, error) {
if depth > MaxDepth {
return nil, errors.New("mergemap: maps too deeply nested")
}
for key, srcVal := range src {
if dstVal, ok := dst[key]; ok {
srcMap, srcMapOk := mapify(srcVal)
dstMap, dstMapOk := mapify(dstVal)
if srcMapOk && dstMapOk {
var err error
srcVal, err = merge(dstMap, srcMap, depth+1)
if err != nil {
return nil, err
}
}
}
dst[key] = srcVal
}
return dst, nil
}
func mapify(i interface{}) (map[string]interface{}, bool) {
value := reflect.ValueOf(i)
if value.Kind() == reflect.Map {
m := map[string]interface{}{}
for _, k := range value.MapKeys() {
m[k.Interface().(string)] = value.MapIndex(k).Interface()
}
return m, true
}
return map[string]interface{}{}, false
}