-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnestedjson.go
More file actions
328 lines (290 loc) · 6.4 KB
/
nestedjson.go
File metadata and controls
328 lines (290 loc) · 6.4 KB
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 nestedjson
import (
"encoding/json"
"fmt"
"log"
"strconv"
)
type NestedJson struct {
data map[string]interface{}
}
func splitPath(path string) ([]interface{}, error) {
const (
Blank = iota
Key
Dot
StartArrayIndex
ArrayIndex
EndArrayIndex
)
var parts []interface{}
startPos := 0
pos := 0
state := Blank
for pos = 0; pos < len(path); pos++ {
c := path[pos]
switch {
case c == '.':
switch state {
case Blank, StartArrayIndex, Dot, ArrayIndex:
return nil, fmt.Errorf("Invalid path: %s, pos: %d", path, pos)
case Key:
parts = append(parts, path[startPos:pos])
state = Dot
case EndArrayIndex:
state = Dot
}
case c == '[':
switch state {
case Blank, EndArrayIndex:
state = StartArrayIndex
case StartArrayIndex, Dot:
return nil, fmt.Errorf("Invalid path: %s, pos: %d", path, pos)
case Key:
parts = append(parts, path[startPos:pos])
state = StartArrayIndex
}
case c == ']':
switch state {
case Blank, Key, StartArrayIndex, EndArrayIndex, Dot:
return nil, fmt.Errorf("Invalid path: %s, pos: %d", path, pos)
case ArrayIndex:
i, _ := strconv.Atoi(path[startPos:pos])
parts = append(parts, i)
state = EndArrayIndex
}
case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '_':
switch state {
case Blank, Key:
state = Key
case Dot:
state = Key
startPos = pos
case StartArrayIndex, EndArrayIndex, ArrayIndex:
return nil, fmt.Errorf("Invalid path: %s, pos: %d", path, pos)
}
case '0' <= c && c <= '9':
switch state {
case Blank, Key:
state = Key
case Dot:
state = Key
startPos = pos
case StartArrayIndex:
state = ArrayIndex
startPos = pos
case ArrayIndex:
state = ArrayIndex
case EndArrayIndex:
return nil, fmt.Errorf("Invalid path: %s, pos: %d", path, pos)
}
}
}
switch state {
case EndArrayIndex:
case Key:
parts = append(parts, path[startPos:pos])
default:
return nil, fmt.Errorf("Invalid path: %s, pos: %d", path, pos)
}
return parts, nil
}
func getPart(obj interface{}, part interface{},
createMissingObject bool) (interface{}, error) {
switch p := part.(type) {
case int:
if arr, ok := obj.([]interface{}); ok {
if p < len(arr) {
return arr[p], nil
} else {
return nil, fmt.Errorf("Array index out of bounds: %d", p)
}
} else {
return nil, fmt.Errorf("%s is not an array: %T", obj, obj)
}
case string:
if m, ok := obj.(map[string]interface{}); ok {
if rv, ok := m[p]; ok {
return rv, nil
} else {
if createMissingObject {
rv = make(map[string]interface{})
m[p] = rv
return rv, nil
}
return nil, fmt.Errorf("Key does not exist: %s", p)
}
} else {
return nil, fmt.Errorf("%s is not an object: %T", obj, obj)
}
}
return nil, fmt.Errorf("Invalid Part: %T", part)
}
func New(args ...map[string]interface{}) *NestedJson {
var m *NestedJson
switch len(args) {
case 0:
m = &NestedJson{make(map[string]interface{})}
case 1:
m = &NestedJson{args[0]}
default:
log.Panicf("NewNestedJson() received too many arguments: %d", len(args))
}
return m
}
func Decode(b []byte) (*NestedJson, error) {
var m map[string]interface{}
if err := json.Unmarshal(b, &m); err == nil {
return &NestedJson{m}, nil
} else {
return nil, err
}
}
func DecodeStr(s string) (*NestedJson, error) {
if n, err := Decode([]byte(s)); err == nil {
return n, nil
} else {
return nil, err
}
}
func (n *NestedJson) Encode() ([]byte, error) {
return json.Marshal(&n.data)
}
func (n *NestedJson) EncodeStr() (string, error) {
if b, err := n.Encode(); err == nil {
return string(b), nil
} else {
return "", err
}
}
func (n *NestedJson) EncodePretty() ([]byte, error) {
return json.MarshalIndent(&n.data, "", " ")
}
func (n *NestedJson) EncodePrettyStr() (string, error) {
if b, err := n.EncodePretty(); err == nil {
return string(b), nil
} else {
return "", err
}
}
func (n *NestedJson) Get(path string) (interface{}, error) {
parts, err := splitPath(path)
if err != nil {
return nil, err
}
var curr interface{} = n.data
for _, part := range parts {
curr, err = getPart(curr, part, false)
if err != nil {
return nil, err
}
}
return curr, nil
}
func (n *NestedJson) Set(path string, val interface{}) error {
parts, err := splitPath(path)
if err != nil {
return err
}
var curr interface{} = n.data
for _, part := range parts[:len(parts)-1] {
curr, err = getPart(curr, part, true)
if err != nil {
return err
}
}
switch k := parts[len(parts)-1].(type) {
case int:
if arr, ok := curr.([]interface{}); ok {
arr[k] = val
} else {
return fmt.Errorf("Not an array: %s", curr)
}
case string:
if m, ok := curr.(map[string]interface{}); ok {
m[k] = val
} else {
return fmt.Errorf("Not an object: %s", curr)
}
}
return nil
}
func (n *NestedJson) Data() map[string]interface{} {
return n.data
}
func (n *NestedJson) String(path string) (string, error) {
o, err := n.Get(path)
if err != nil {
return "", err
}
switch rv := o.(type) {
case string:
return rv, nil
default:
return "", fmt.Errorf("%s is not a string", path, o)
}
}
func (n *NestedJson) Int(path string) (int, error) {
o, err := n.Get(path)
if err != nil {
return 0, err
}
switch rv := o.(type) {
case int:
return rv, nil
case float64:
return int(rv), nil
default:
return 0, fmt.Errorf("%s is not an integer", path, o)
}
}
func (n *NestedJson) Float(path string) (float64, error) {
o, err := n.Get(path)
if err != nil {
return 0, err
}
switch rv := o.(type) {
case int:
return float64(rv), nil
case float64:
return rv, nil
default:
return 0, fmt.Errorf("%s is not a float", path, o)
}
}
func (n *NestedJson) Bool(path string) (bool, error) {
o, err := n.Get(path)
if err != nil {
return false, err
}
switch rv := o.(type) {
case bool:
return rv, nil
default:
return false, fmt.Errorf("%s is not a bool", path, o)
}
}
func (n *NestedJson) Array(path string) ([]interface{}, error) {
o, err := n.Get(path)
if err != nil {
return nil, err
}
switch rv := o.(type) {
case []interface{}:
return rv, nil
default:
return nil, fmt.Errorf("%s is not an Array", path, o)
}
}
func (n *NestedJson) Map(path string) (map[string]interface{}, error) {
o, err := n.Get(path)
if err != nil {
return nil, err
}
switch rv := o.(type) {
case map[string]interface{}:
return rv, nil
default:
return nil, fmt.Errorf("%s is not a map", path, o)
}
}