-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeeply.go
More file actions
45 lines (42 loc) · 961 Bytes
/
deeply.go
File metadata and controls
45 lines (42 loc) · 961 Bytes
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
package deeply
import (
"log"
"reflect"
)
// Merge will merge two maps by imposing values of 'b' over 'a'
func Merge(a, b map[string]interface{}) map[string]interface{} {
c := Copy(b)
for k, av := range a {
bv, bok := b[k]
if bok {
at := reflect.TypeOf(av)
bt := reflect.TypeOf(bv)
if at == bt {
switch av.(type) {
case map[string]interface{}:
c[k] = Merge(av.(map[string]interface{}), bv.(map[string]interface{}))
default:
c[k] = bv
}
} else {
log.Printf("Non matching types '%v' and '%v' (%s)", at, bt, k)
}
} else {
c[k] = av
}
}
return c
}
// Copy makes a deep copy of a map[string]interface{} (no cloning of pointers, slices or arrays ...)
func Copy(m map[string]interface{}) map[string]interface{} {
c := map[string]interface{}{}
for k, v := range m {
switch v.(type) {
case map[string]interface{}:
c[k] = Copy(v.(map[string]interface{}))
default:
c[k] = v
}
}
return c
}