-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobj-utils.go
328 lines (296 loc) · 7.95 KB
/
obj-utils.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package arp
import (
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"strconv"
"strings"
"gopkg.in/yaml.v2"
)
const (
JSON_OBJECT_DELIM = "."
JSON_INDEX_START_DELIM = "["
JSON_INDEX_END_DELIM = "]"
JSON_INDEX_DELIM = JSON_INDEX_START_DELIM + JSON_INDEX_END_DELIM
JSON_RESERVED_CHARS = JSON_OBJECT_DELIM + JSON_INDEX_DELIM
)
func YamlToJson(i interface{}) interface{} {
switch x := i.(type) {
case map[interface{}]interface{}:
m2 := map[string]interface{}{}
for k, v := range x {
m2[k.(string)] = YamlToJson(v)
}
return m2
case []interface{}:
for i, v := range x {
x[i] = YamlToJson(v)
}
}
return i
}
func JsonToYaml(i interface{}) interface{} {
switch x := i.(type) {
case map[string]interface{}:
m2 := map[interface{}]interface{}{}
for k, v := range x {
m2[k] = JsonToYaml(v)
}
return m2
case []interface{}:
for i, v := range x {
x[i] = YamlToJson(v)
}
}
return i
}
func PrintYamlObj(object interface{}) (string, error) {
bytes, err := yaml.Marshal(object)
if err != nil {
return "", err
}
return string(bytes), nil
}
func ObjectPrintf(message string, obj interface{}) string {
objStr, _ := PrintYamlObj(obj)
return fmt.Sprintf("%v:\n---\n%v---\n", message, objStr)
}
func ToJsonObj(obj interface{}) (map[string]interface{}, error) {
b, err := json.Marshal(obj)
if err != nil {
return nil, err
}
var r map[string]interface{}
err = json.Unmarshal(b, &r)
if err != nil {
return nil, err
}
return r, nil
}
func ToJsonStr(obj interface{}) string {
b, _ := json.Marshal(obj)
return string(b)
}
func Base64GzipToByteReader(input string) (io.ReadCloser, error) {
gzipB, err := base64.StdEncoding.DecodeString(input)
if err != nil {
return nil, fmt.Errorf("invalid base64 encoded string found")
}
gzipR, err := gzip.NewReader(bytes.NewReader(gzipB))
if err != nil {
return nil, fmt.Errorf(" base64 encoded string was not gzip compressed.")
}
return gzipR, nil
}
type JsonKey struct {
Name string
IsArray bool
IsArrayElement bool
IsLast bool
IsObject bool
}
// GetJsonPath Returns a string representation of a series of json keys that make up a path to a value
// in a json object. The maxDepth provides a limit on how deep into the path structure to construct since
// the key object count to json path node count is not always 1-1 (e.g. array[index] is split as 2 keys but
// only counts as a single node in the path)
// The remaining unprocessed keys are returned along with the string representation of the processed keys.
func GetJsonPath(keys []JsonKey, maxDepth int) (string, []JsonKey) {
pathStr := ""
nodeCount := 0
remainingKeys := keys
for i, k := range keys {
remainingKeys = keys[i:]
if nodeCount >= maxDepth {
return pathStr, remainingKeys
}
if k.IsArrayElement {
pathStr += fmt.Sprintf("[%v]", k.Name)
nodeCount++
} else if k.IsObject {
pathStr += fmt.Sprintf(".%v", k.Name)
} else if k.IsArray {
pathStr += fmt.Sprintf(".%v", k.Name)
} else {
pathStr += "." + k.Name
nodeCount++
}
}
return pathStr, remainingKeys
}
func sanitizeQuotedIndex(key string) string {
cleaned := key
for _, c := range "\"`'" {
cleaned = strings.TrimPrefix(cleaned, string(c))
cleaned = strings.TrimSuffix(cleaned, string(c))
}
return cleaned
}
// SplitJsonPath Splits a string formatted as a JSON accessor into its individual keys
// with metadata. E.g. "data.someArray[1].value" -> "[data, someArray, 1, value]"
func SplitJsonPath(jsonPath string) []JsonKey {
keys := SplitStringTokens(jsonPath, JSON_OBJECT_DELIM)
// Extract array indexing from the keys as their own key for iterating the datastore.
var expandedKeys []JsonKey
for index, k := range keys {
// now that we've split out the tokens, we can remove any quotes surrounding keys
keyStrs := PromoteTokenQuotes(SplitStringTokens(k, JSON_INDEX_DELIM))
if len(keyStrs) > 0 {
for _, ks := range keyStrs {
var toAdd JsonKey
// test if it's a number
if _, err := strconv.ParseInt(ks, 10, 64); err == nil {
toAdd = JsonKey{Name: ks, IsArrayElement: true}
if len(expandedKeys) > 0 {
// mark the previous key as an array
expandedKeys[len(expandedKeys)-1].IsArray = true
}
} else {
// otherwise its an object key
toAdd = JsonKey{Name: sanitizeQuotedIndex(ks)}
}
expandedKeys = append(expandedKeys, toAdd)
}
} else {
// check for object type hinting. Can force a path to be treated as an object just by having
// {} in the key name somewhere
isObject := strings.Contains(k, "{}")
expandedKeys = append(expandedKeys, JsonKey{Name: k, IsObject: isObject})
}
if len(expandedKeys) > 0 && index < len(keys) {
// Otherwise, we can set the previous element as an object there is a subkey for it.
if !expandedKeys[len(expandedKeys)-1].IsArray && !expandedKeys[len(expandedKeys)-1].IsArrayElement {
expandedKeys[len(expandedKeys)-1].IsObject = true
}
}
}
expandedKeys[len(expandedKeys)-1].IsLast = true
return expandedKeys
}
// PutJsonValue Insert an arbitrary value at a desired jsonPath. If the intermediary
// objects/arrays don't exist, they will be created.
func PutJsonValue(dest map[string]interface{}, jsonPath string, value interface{}) error {
type Noodle struct {
Parent interface{}
Node interface{}
ParentKey string
ParentIndex int64
}
expandedKeys := SplitJsonPath(jsonPath)
node := Noodle{
Node: dest,
Parent: dest,
}
for _, k := range expandedKeys {
key := k.Name
var temp interface{}
switch v := node.Node.(type) {
case map[string]interface{}:
if nextNode, ok := v[key]; !ok {
if k.IsLast {
v[key] = value
return nil
} else if k.IsArray {
temp = make([]interface{}, 1)
} else {
temp = make(map[string]interface{})
}
v[key] = temp
node = Noodle{
Node: temp,
Parent: &v,
ParentKey: key,
ParentIndex: -1,
}
} else {
// otherwise overwrite existing ones
if k.IsLast {
v[key] = value
return nil
}
node = Noodle{
Node: nextNode,
Parent: &v,
ParentKey: key,
ParentIndex: -1,
}
}
case []interface{}:
idx, err := strconv.ParseUint(key, 10, 64)
if err != nil {
return fmt.Errorf(BadIndexDSFmt, jsonPath)
}
// if the index is out of range, then we'll resize the array just enough to fix the index
oldLen := uint64(len(v))
if idx >= oldLen {
newArray := v[:]
delta := (idx - oldLen) + 1
for delta > 0 {
delta--
newArray = append(newArray, nil)
}
if node.ParentKey != "" {
n := node.Parent.(*map[string]interface{})
(*n)[node.ParentKey] = newArray
} else if node.ParentIndex >= 0 {
n := node.Parent.(*[]interface{})
(*n)[node.ParentIndex] = newArray
}
v = newArray
}
if v[idx] == nil {
if k.IsLast {
v[idx] = value
return nil
} else if k.IsArray {
temp = make([]interface{}, 1)
} else {
temp = make(map[string]interface{})
}
v[idx] = temp
} else {
if k.IsLast {
v[idx] = value
return nil
}
}
node = Noodle{
Node: v[idx],
Parent: &v,
ParentIndex: int64(idx),
}
}
}
return nil
}
func GetJsonValue(src map[string]interface{}, jsonPath string) (interface{}, error) {
expandedKeys := SplitJsonPath(jsonPath)
var node interface{}
node = src
for _, k := range expandedKeys {
key := k.Name
switch v := node.(type) {
case map[string]interface{}:
if nextNode, ok := v[key]; !ok {
return "", fmt.Errorf(MissingDSKeyFmt, jsonPath)
} else {
node = nextNode
}
case []interface{}:
idx, err := strconv.ParseUint(key, 10, 64)
if err != nil {
// should catch non integer and negative value
return "", fmt.Errorf(BadIndexDSFmt, jsonPath)
}
if idx > uint64(len(v)) {
return "", fmt.Errorf(IndexExceedsDSFmt, jsonPath)
}
node = v[idx]
default:
return "", fmt.Errorf(MissingDSKeyFmt, jsonPath)
}
}
return node, nil
}