-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
102 lines (90 loc) · 2.12 KB
/
path.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package maps // import "syreclabs.com/go/maps"
import (
"errors"
"reflect"
"strconv"
"strings"
)
func PathSet(root interface{}, path string, value interface{}) error {
if root == nil {
return errors.New("nil obj")
}
typ := reflect.TypeOf(root)
if typ.Kind() != reflect.Ptr {
return errors.New("non-pointer obj: " + typ.String())
}
if typ.Elem().Kind() != reflect.Map {
return errors.New("non-map obj: " + typ.String())
}
keys, err := parsePath(path)
if err != nil {
return err
}
// walk backwards through path and prepare container chain
var val interface{} = value
for i := len(keys); i > 0; i-- {
k := keys[i-1]
if idx, err := strconv.ParseInt(k, 10, 64); err == nil {
// slice
s := make([]interface{}, idx+1, idx+1)
s[idx] = val
val = s
} else {
// map
val = map[string]interface{}{k: val}
}
}
// merge prepared container chain with root
merge(reflect.Indirect(reflect.ValueOf(root)).Interface(), val)
return nil
}
var errInvalidPath = errors.New("invalid path")
func parsePath(path string) ([]string, error) {
// support both "Foo[0].Bar" and "Foo.0.Bar" path syntax
path = strings.Replace(path, "[", ".", -1)
path = strings.Replace(path, "]", "", -1)
keys := strings.Split(path, ".")
if len(keys) == 0 {
return nil, errInvalidPath
}
if keys[0] == "" || keys[0] == "." {
return nil, errInvalidPath
}
if _, err := strconv.ParseInt(keys[0], 10, 64); err == nil {
return nil, errInvalidPath
}
return keys, nil
}
func merge(dst, src interface{}) interface{} {
switch s := src.(type) {
case map[string]interface{}:
var mdst map[string]interface{}
mdst, ok := dst.(map[string]interface{})
if !ok {
mdst = make(map[string]interface{}, len(s))
}
for k := range s {
mdst[k] = merge(mdst[k], s[k])
}
return mdst
case []interface{}:
var sdst []interface{}
sdst, ok := dst.([]interface{})
if !ok {
sdst = make([]interface{}, len(s))
}
if len(sdst) < len(s) {
ns := make([]interface{}, len(s))
copy(ns, sdst)
sdst = ns
}
for i := range s {
if v := merge(sdst[i], s[i]); v != nil {
sdst[i] = v
}
}
return sdst
default:
return src
}
}