-
Notifications
You must be signed in to change notification settings - Fork 2
/
apply.go
311 lines (274 loc) · 6.12 KB
/
apply.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
package shorthand
import (
"strconv"
)
// makeGenericMap turns a map[string]any into a map[any]any, copying over
// all existing items.
func makeGenericMap(m map[string]any) map[any]any {
cp := make(map[any]any, len(m))
for k, v := range m {
cp[k] = v
}
return cp
}
func isMap(v any) bool {
if _, ok := v.(map[string]any); ok {
return true
} else if _, ok := v.(map[any]any); ok {
return true
}
return false
}
func isArray(v any) bool {
if _, ok := v.([]any); ok {
return true
}
return false
}
func withDefault(wantMap bool, k any, v any) (any, bool) {
if wantMap && !isMap(v) {
if _, ok := k.(string); ok {
return map[string]any{}, true
} else {
return map[any]any{}, true
}
} else if !wantMap && !isArray(v) {
return []any{}, true
}
return nil, false
}
func applyValue(_ any, op Operation) (any, Error) {
return op.Value, nil
}
func (d *Document) applyIndex(input any, op Operation) (any, Error) {
var err error
// Handle index
d.skipWhitespace()
d.buf.Reset()
// Start by assuming an append (no index)
index := -1
appnd := true
insert := false
for {
// Try to parse each char between the [ and ]
r := d.next()
if r == -1 || r == ']' {
break
}
d.buf.WriteRune(r)
}
if d.buf.Len() > 0 {
// We have values, so try to parse it as an index!
s := d.buf.String()
if s[0] == '^' {
insert = true
s = s[1:]
}
index, err = strconv.Atoi(s)
if err != nil {
return nil, d.error(uint(len(s)), "Cannot convert index to number")
}
appnd = false
}
if d.options.DebugLogger != nil {
d.options.DebugLogger("Setting index %d insert:%v append:%v", index, insert, appnd)
}
// Change the type to a slice if needed.
if !isArray(input) {
input = []any{}
}
s := input.([]any)
if appnd {
// Append (i.e. index equals length).
index = len(s)
} else if index < 0 {
// Support negative indexes, e.g. `-1` is the last item.
index = len(s) + index
}
// Grow by appending nil until the slice is the right length.
grew := false
for len(s) <= index {
grew = true
s = append(s, nil)
}
// Handle insertion, i.e. shifting items if needed after appending a new
// item to shift them into (if the index was within the bounds of the
// original slice).
if insert {
if !grew {
s = append(s, nil)
}
for i := len(s) - 2; i >= index; i-- {
s[i+1] = s[i]
}
}
// Depending on what the next character is, we either set the value or
// recurse with more indexes or path parts.
if p := d.peek(); p == -1 {
if op.Kind == OpDelete {
s = append(s[:index], s[index+1:]...)
} else {
s[index] = op.Value
}
} else if p == '[' {
d.next()
result, err := d.applyIndex(s[index], op)
if err != nil {
return nil, err
}
s[index] = result
} else if p == '.' {
d.next()
result, err := d.applyPathPart(s[index], op)
if err != nil {
return nil, err
}
s[index] = result
} else {
panic("unexpected char " + string(p))
}
return s, nil
}
func (d *Document) applyPathPart(input any, op Operation) (any, Error) {
quoted := false
d.buf.Reset()
for {
r := d.next()
if r == '\\' {
if d.parseEscape(false, false) {
continue
}
}
if r == '"' {
if err := d.parseQuoted(false); err != nil {
return nil, err
}
quoted = true
continue
}
if r == '.' || r == '[' || r == -1 {
keystr := d.buf.String()
var key any = keystr
d.buf.Reset()
if key == "" && !quoted {
// Special case: raw value
if r == '[' {
// This raw value is an array.
return d.applyIndex(input, op)
}
return op.Value, nil
}
if !d.options.ForceStringKeys && !quoted {
if tmp, ok := coerceValue(keystr, d.options.ForceFloat64Numbers); ok {
key = tmp
}
}
if d.options.DebugLogger != nil {
d.options.DebugLogger("Setting key %v", key)
}
wantMap := true
applyFunc := d.applyPathPart
if r == '[' {
wantMap = false
applyFunc = d.applyIndex
} else if r == -1 {
applyFunc = applyValue
}
if !isMap(input) {
if def, ok := withDefault(true, key, nil); ok {
input = def
}
}
if m, ok := input.(map[string]any); ok {
if s, ok := key.(string); ok {
if wantMap && op.Kind == OpDelete {
delete(m, s)
} else {
result, err := applyFunc(m[s], op)
if err != nil {
return nil, err
}
m[s] = result
}
break
} else {
// Key is not a string, so convert input into a generic
// map[any]any and process it further below.
input = makeGenericMap(m)
}
}
if m, ok := input.(map[any]any); ok {
if wantMap && op.Kind == OpDelete {
delete(m, key)
} else {
v := m[key]
if def, ok := withDefault(wantMap, key, v); ok {
v = def
}
result, err := applyFunc(v, op)
if err != nil {
return nil, err
}
m[key] = result
}
break
}
panic("can't get here")
}
d.buf.WriteRune(r)
}
return input, nil
}
func (d *Document) applySwap(input any, op Operation) (any, Error) {
// First, get both left & right values from the input.
left, okl, err := GetPath(op.Path, input, GetOptions{DebugLogger: d.options.DebugLogger})
if err != nil {
return nil, err
}
right, okr, err := GetPath(op.Value.(string), input, GetOptions{DebugLogger: d.options.DebugLogger})
if err != nil {
return nil, err
}
if d.options.DebugLogger != nil {
d.options.DebugLogger("Swapping (%v, %t) & (%v, %t)", left, okl, right, okr)
}
// Set/unset left to value of right
kind := OpSet
if !okr {
kind = OpDelete
}
d.expression = op.Path
d.pos = 0
input, err = d.applyPathPart(input, Operation{
Kind: kind,
Path: op.Path,
Value: right,
})
if err != nil {
return nil, err
}
// Set/unset right to value of left
kind = OpSet
if !okl {
kind = OpDelete
}
d.expression = op.Value.(string)
d.pos = 0
return d.applyPathPart(input, Operation{
Kind: kind,
Path: op.Value.(string),
Value: left,
})
}
func (d *Document) applyOp(input any, op Operation) (any, Error) {
d.expression = op.Path
d.pos = 0
d.buf.Reset()
switch op.Kind {
case OpSet, OpDelete:
return d.applyPathPart(input, op)
case OpSwap:
return d.applySwap(input, op)
}
return d.applyPathPart(input, op)
}