-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.go
More file actions
461 lines (433 loc) · 13.1 KB
/
Copy pathpath.go
File metadata and controls
461 lines (433 loc) · 13.1 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package jsonc
import (
"fmt"
"slices"
"strconv"
"strings"
)
// Path is a compiled query that selects zero or more [Node] values from a
// JSONC AST. Paths can be built from a rotini-style expression (see
// [PathString]) or from an RFC 6901 JSON Pointer (see [PathPointer]).
//
// A Path is immutable after construction; reuse compiled paths across
// queries against different documents.
type Path struct {
expr string // original expression, for error messages
segments []pathSegment
}
// pathSegment is one component of a Path.
type pathSegment struct {
kind segmentKind
name string // for child / recursive segments — unescaped member name
index int // for index segments — 0-based array index
}
// segmentKind identifies the kind of a path segment.
type segmentKind int
const (
segChild segmentKind = iota // .name or ["name"] — exact child by name
segIndex // [N] — array element by 0-based index
segWildcard // [*] — every child of an object or array
segRecursive // ..name — every descendant matching name
)
// String returns the original expression used to build the path.
func (p Path) String() string { return p.expr }
// PathString compiles a rotini-style path expression into a [Path]. The
// supported grammar is intentionally small:
//
// $ — the root document (optional; default if expression starts with . or [)
// .name — exact object member by name (name follows JS-identifier rules)
// ["name"] — exact object member, allowing arbitrary names
// ['name'] — same as above with single quotes
// [N] — 0-based array index (negative indices are not supported)
// [*] — wildcard: every direct child of the current container
// ..name — recursive descent: every descendant whose key matches name
//
// Whitespace inside brackets is allowed; outside brackets it is not.
//
// Returns [ErrPathSyntax] (wrapped) for malformed expressions.
func PathString(expr string) (Path, error) {
p := Path{expr: expr}
src := expr
if src == "" {
return p, fmt.Errorf("%w: empty expression", ErrPathSyntax)
}
if src[0] == '$' {
src = src[1:]
}
for src != "" {
seg, rest, err := parsePathSegment(src)
if err != nil {
return Path{}, err
}
p.segments = append(p.segments, seg)
src = rest
}
return p, nil
}
// parsePathSegment consumes one segment from the front of s and returns
// (segment, remainder, error).
func parsePathSegment(s string) (pathSegment, string, error) {
switch s[0] {
case '.':
return parseDotSegment(s)
case '[':
return parseBracketSegment(s)
default:
return pathSegment{}, "", fmt.Errorf("%w: unexpected %q", ErrPathSyntax, s[0])
}
}
// parseDotSegment parses ".name" or "..name". Caller has confirmed s[0] == '.'.
func parseDotSegment(s string) (pathSegment, string, error) {
recursive := false
if len(s) >= 2 && s[1] == '.' {
recursive = true
s = s[2:]
} else {
s = s[1:]
}
end := len(s)
for i := range len(s) {
if s[i] == '.' || s[i] == '[' {
end = i
break
}
}
name := s[:end]
if name == "" {
return pathSegment{}, "", fmt.Errorf("%w: empty member name after dot", ErrPathSyntax)
}
kind := segChild
if recursive {
kind = segRecursive
}
return pathSegment{kind: kind, name: name}, s[end:], nil
}
// parseBracketSegment parses "[...]" forms: [N], [*], ["name"], ['name'].
// Caller has confirmed s[0] == '['.
func parseBracketSegment(s string) (pathSegment, string, error) {
closeIdx := strings.IndexByte(s, ']')
if closeIdx < 0 {
return pathSegment{}, "", fmt.Errorf("%w: missing ]", ErrPathSyntax)
}
inner := strings.TrimSpace(s[1:closeIdx])
rest := s[closeIdx+1:]
if inner == "*" {
return pathSegment{kind: segWildcard}, rest, nil
}
if len(inner) >= 2 && (inner[0] == '"' || inner[0] == '\'') && inner[len(inner)-1] == inner[0] {
return pathSegment{kind: segChild, name: inner[1 : len(inner)-1]}, rest, nil
}
idx, err := strconv.Atoi(inner)
if err != nil || idx < 0 {
return pathSegment{}, "", fmt.Errorf("%w: invalid bracket %q", ErrPathSyntax, inner)
}
return pathSegment{kind: segIndex, index: idx}, rest, nil
}
// PathPointer compiles an RFC 6901 JSON Pointer into a [Path]. Per the
// RFC, the empty string refers to the document root; non-empty pointers
// must start with `/`. Tokens use the standard escapes `~0` for `~` and
// `~1` for `/`. Numeric tokens applied to an array select an element by
// index; the special token `-` (one-past-end) is rejected by [Path.Read]
// but accepted by [Path.Append] for the JSON Patch `add` operation.
//
// Returns [ErrPointerSyntax] (wrapped) for malformed pointers.
func PathPointer(ptr string) (Path, error) {
p := Path{expr: ptr}
if ptr == "" {
return p, nil
}
if ptr[0] != '/' {
return Path{}, fmt.Errorf("%w: pointer must start with /", ErrPointerSyntax)
}
for tok := range strings.SplitSeq(ptr[1:], "/") {
decoded := decodePointerToken(tok)
// In a JSON Pointer the same token can refer to either an object
// member or an array index — the structure of the document
// determines which. We default to a child segment; the index is
// resolved at lookup time when a numeric token is applied to an
// array.
seg := pathSegment{kind: segChild, name: decoded}
if i, err := strconv.Atoi(decoded); err == nil && i >= 0 {
seg.index = i
}
p.segments = append(p.segments, seg)
}
return p, nil
}
// decodePointerToken applies RFC 6901 escape rules: ~1 → /, ~0 → ~. The
// order matters (do ~1 before ~0).
func decodePointerToken(tok string) string {
tok = strings.ReplaceAll(tok, "~1", "/")
tok = strings.ReplaceAll(tok, "~0", "~")
return tok
}
// Read returns the [Node] values reachable by p starting from root, in
// document order. Returns nil when no nodes match.
func (p Path) Read(root *Node) []*Node {
if root == nil {
return nil
}
cur := []*Node{root}
for _, seg := range p.segments {
cur = applySegment(cur, seg)
if len(cur) == 0 {
return nil
}
}
return cur
}
// ReadFirst returns the first [Node] reachable by p, or [ErrPathNotFound].
func (p Path) ReadFirst(root *Node) (*Node, error) {
matches := p.Read(root)
if len(matches) == 0 {
return nil, ErrPathNotFound
}
return matches[0], nil
}
// ReadString is a convenience for paths that select a string-valued node.
// Returns the unescaped string value of the first match, or
// [ErrPathNotFound]. Non-string scalars return their raw textual form.
func (p Path) ReadString(root *Node) (string, error) {
n, err := p.ReadFirst(root)
if err != nil {
return "", err
}
switch n.Kind {
case StringNode, NumberNode, BooleanNode, NullNode:
return n.Value, nil
default:
return n.String(), nil
}
}
// ReadPositions returns the source [Position] of every node matched by p.
func (p Path) ReadPositions(root *Node) []Position {
matches := p.Read(root)
out := make([]Position, len(matches))
for i, n := range matches {
out[i] = n.Pos
}
return out
}
// Replace overwrites every node matched by p with a copy of value. When the
// match is a [KeyValueNode] (an object member), the value child is replaced;
// otherwise the matched node itself is overwritten in place.
//
// Returns [ErrPathNotFound] if p matches no nodes.
func (p Path) Replace(root, value *Node) error {
matches := p.Read(root)
if len(matches) == 0 {
return ErrPathNotFound
}
for _, n := range matches {
switch n.Kind {
case KeyValueNode:
if len(n.Children) > 0 {
n.Children[0] = cloneNode(value)
} else {
n.Children = []*Node{cloneNode(value)}
}
default:
*n = *cloneNode(value)
}
}
return nil
}
// Append appends value as a new child to every container matched by p.
// For an object, value must be a [KeyValueNode]. For an array, value is
// appended as an element.
//
// Returns [ErrPathNotFound] if p matches no nodes, or a [SyntaxError] if
// the value kind doesn't fit the matched container kind.
func (p Path) Append(root, value *Node) error {
matches := p.Read(root)
if len(matches) == 0 {
return ErrPathNotFound
}
for _, n := range matches {
switch n.Kind {
case ObjectNode:
if value.Kind != KeyValueNode {
return &SyntaxError{Message: "Append into ObjectNode requires KeyValueNode value"}
}
n.Children = append(n.Children, cloneNode(value))
case ArrayNode:
n.Children = append(n.Children, cloneNode(value))
default:
return &SyntaxError{Message: fmt.Sprintf("Append target must be object or array, got %s", n.Kind)}
}
}
return nil
}
// Delete removes every node matched by p from its parent container.
// KeyValueNode matches are removed from their parent ObjectNode; element
// matches are removed from their parent ArrayNode.
//
// Returns [ErrPathNotFound] if p matches no nodes.
func (p Path) Delete(root *Node) error {
matches := p.Read(root)
if len(matches) == 0 {
return ErrPathNotFound
}
parents := mapNodeParents(root)
// Reverse iteration so index shifts from earlier deletes don't invalidate
// later ones when multiple matches share a parent.
for i, target := range slices.Backward(matches) {
_ = i
parent, idx := findChild(parents, target)
if parent == nil {
continue
}
parent.Children = append(parent.Children[:idx], parent.Children[idx+1:]...)
}
return nil
}
// applySegment maps the current selection through a single segment.
func applySegment(cur []*Node, seg pathSegment) []*Node {
var next []*Node
for _, n := range cur {
switch seg.kind {
case segChild:
next = append(next, childByName(n, seg.name)...)
// JSON Pointer leaves array-vs-object resolution to the document.
// If the node is an array and the segment name parses as a
// non-negative integer, treat it as an index.
if len(next) == 0 && n.Kind == ArrayNode {
if i := seg.indexFromName(); i >= 0 && i < len(n.Children) {
next = append(next, n.Children[i])
}
}
case segIndex:
if n.Kind == ArrayNode && seg.index < len(n.Children) {
next = append(next, n.Children[seg.index])
}
case segWildcard:
next = append(next, valueChildrenPublic(n)...)
case segRecursive:
collectRecursive(n, seg.name, &next)
}
}
return next
}
// indexFromName attempts to interpret a child segment's name as a numeric
// index (used when a JSON Pointer token addresses an array slot).
func (s pathSegment) indexFromName() int {
i, err := strconv.Atoi(s.name)
if err != nil || i < 0 {
return -1
}
return i
}
// childByName returns the value child(ren) of n whose member name equals
// name. For an ObjectNode this is at most one match; the function returns
// the value (not the KeyValueNode wrapper) so subsequent segments operate
// on the actual member.
func childByName(n *Node, name string) []*Node {
if n == nil || n.Kind != ObjectNode {
return nil
}
for _, c := range n.Children {
if c.Kind != KeyValueNode {
continue
}
if c.Key == name && len(c.Children) > 0 {
return []*Node{c.Children[0]}
}
}
return nil
}
// valueChildrenPublic returns the value children of a container, filtering
// out CommentNode siblings and unwrapping KeyValueNode children of objects.
func valueChildrenPublic(n *Node) []*Node {
if n == nil {
return nil
}
var out []*Node
for _, c := range n.Children {
if c.Kind == CommentNode {
continue
}
if c.Kind == KeyValueNode && len(c.Children) > 0 {
out = append(out, c.Children[0])
continue
}
out = append(out, c)
}
return out
}
// collectRecursive walks the AST under n collecting every value reachable
// from a member named name (at any depth).
func collectRecursive(n *Node, name string, out *[]*Node) {
if n == nil {
return
}
switch n.Kind {
case ObjectNode:
for _, c := range n.Children {
if c.Kind != KeyValueNode {
continue
}
if c.Key == name && len(c.Children) > 0 {
*out = append(*out, c.Children[0])
}
for _, gc := range c.Children {
collectRecursive(gc, name, out)
}
}
case ArrayNode:
for _, c := range n.Children {
collectRecursive(c, name, out)
}
}
}
// mapNodeParents builds a map from child node to its parent for the AST
// rooted at n, allowing constant-time lookups during structural mutations.
func mapNodeParents(root *Node) map[*Node]*Node {
parents := make(map[*Node]*Node)
var visit func(n *Node)
visit = func(n *Node) {
if n == nil {
return
}
for _, c := range n.Children {
parents[c] = n
visit(c)
}
}
visit(root)
return parents
}
// findChild returns the parent of target and target's index within
// parent.Children, or (nil, -1) if target is the root or unparented.
func findChild(parents map[*Node]*Node, target *Node) (*Node, int) {
parent := parents[target]
if parent == nil {
return nil, -1
}
for i, c := range parent.Children {
if c == target {
return parent, i
}
}
// Could be wrapped in a KeyValueNode whose only child is target.
for i, c := range parent.Children {
if c.Kind == KeyValueNode && len(c.Children) > 0 && c.Children[0] == target {
return parent, i
}
}
return nil, -1
}
// cloneNode returns a deep copy of n. Used by Replace/Append to avoid
// aliasing the caller's value into the AST.
func cloneNode(n *Node) *Node {
if n == nil {
return nil
}
out := *n
if n.Children != nil {
out.Children = make([]*Node, len(n.Children))
for i, c := range n.Children {
out.Children[i] = cloneNode(c)
}
}
return &out
}