-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.go
More file actions
46 lines (39 loc) · 911 Bytes
/
utils.go
File metadata and controls
46 lines (39 loc) · 911 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
46
package go_config
import (
"github.com/spf13/cast"
)
func MergeMapWithPath(source map[string]interface{}, sub map[string]interface{}, path []string) error {
if len(path) == 0 || (len(path) == 1 && path[0] == "") {
for k, v := range sub {
source[k] = v
}
return nil
}
next, ok := source[path[0]]
if !ok {
next = map[string]interface{}{}
source[path[0]] = next
}
return MergeMapWithPath(cast.ToStringMap(source[path[0]]), sub, path[1:])
}
func Lookup(source map[string]interface{}, key []string) interface{} {
if len(key) == 0 {
return source
}
next, ok := source[key[0]]
if ok {
if len(key) == 1 {
return next
}
// Nested case
switch next.(type) {
case map[interface{}]interface{}:
return Lookup(cast.ToStringMap(next), key[1:])
case map[string]interface{}:
return Lookup(next.(map[string]interface{}), key[1:])
default:
return nil
}
}
return nil
}