-
Notifications
You must be signed in to change notification settings - Fork 107
/
pathvalue.go
192 lines (179 loc) · 3.69 KB
/
pathvalue.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package logevent
import (
"reflect"
"strconv"
"strings"
lru "github.com/hashicorp/golang-lru"
)
type pathtoken struct {
isSlice bool
index int
key string
}
const cacheSize = 200
var cache *lru.Cache
func init() {
var err error
cache, err = lru.New(cacheSize)
if err != nil {
panic(err)
}
}
func getPathArrayToken(path string) []pathtoken {
var tokens []pathtoken
for len(path) > 0 && path[len(path)-1] == ']' {
pos := strings.LastIndexByte(path, '[')
if pos < 0 {
break
}
indexStr := path[pos+1 : len(path)-1]
index, err := strconv.Atoi(indexStr)
if err != nil {
panic(err)
}
t := pathtoken{
isSlice: true,
index: index,
}
tokens = append([]pathtoken{t}, tokens...)
path = path[:pos]
}
if len(tokens) > 0 && len(path) > 0 {
// object key
tokens = append([]pathtoken{{
isSlice: false,
key: path,
}}, tokens...)
}
return tokens
}
func compilePath(path string) []pathtoken {
fields := strings.Split(path, ".")
tokens := make([]pathtoken, 0, len(fields)+2)
for _, field := range fields {
atokens := getPathArrayToken(field)
if len(atokens) > 0 {
tokens = append(tokens, atokens...)
} else {
tokens = append(tokens, pathtoken{
isSlice: false,
key: field,
})
}
}
return tokens
}
func compilePathWithCache(path string) []pathtoken {
var tokens []pathtoken
cachedTokens, ok := cache.Get(path)
if ok {
tokens = cachedTokens.([]pathtoken)
} else {
tokens = compilePath(path)
cache.Add(path, tokens)
}
return tokens
}
func getPathValue(obj any, path string) (any, bool) {
tokens := compilePathWithCache(path)
return getPathValueFromTokens(obj, tokens)
}
func getPathValueFromTokens(obj any, tokens []pathtoken) (any, bool) {
for _, t := range tokens {
switch v := obj.(type) {
case map[string]any:
if t.isSlice {
// invalid path
return nil, false
}
var ok bool
obj, ok = v[t.key]
if !ok {
return nil, false
}
case []any:
if !t.isSlice {
// invalid path
return nil, false
}
if t.index < 0 {
if -t.index > len(v) {
// array index out of range
return nil, false
}
obj = v[len(v)+t.index]
} else if t.index >= len(v) {
// array index out of range
return nil, false
} else {
obj = v[t.index]
}
default:
s := reflect.ValueOf(obj)
if s.Kind() == reflect.Slice {
if !t.isSlice {
// invalid path
return nil, false
}
if t.index < 0 {
if -t.index > s.Len() {
// array index out of range
return nil, false
}
obj = s.Index(s.Len() + t.index).Interface()
} else if t.index >= s.Len() {
// array index out of range
return nil, false
} else {
obj = s.Index(t.index).Interface()
}
} else {
// TODO: reflect struct
return nil, false
}
}
}
return obj, true
}
func setPathValue(obj map[string]any, path string, v any) bool {
fieldSplits := strings.Split(path, ".")
for i, key := range fieldSplits {
if i >= len(fieldSplits)-1 {
obj[key] = v
return true
} else if node, ok := obj[key]; ok {
switch v := node.(type) {
case map[string]any:
obj = v
case nil:
obj[key] = map[string]any{}
obj = obj[key].(map[string]any)
default:
return false
}
} else {
obj[key] = map[string]any{}
obj = obj[key].(map[string]any)
}
}
return false
}
func removePathValue(obj map[string]any, field string) bool {
fieldSplits := strings.Split(field, ".")
for i, key := range fieldSplits {
if i >= len(fieldSplits)-1 {
delete(obj, key)
return true
} else if node, ok := obj[key]; ok {
switch v := node.(type) {
case map[string]any:
obj = v
default:
return false
}
} else {
break
}
}
return false
}