-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
56 lines (41 loc) · 880 Bytes
/
path.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
package jq
import (
"fmt"
"nikand.dev/go/cbor"
)
type (
Path struct {
Expr FilterPath
path NodePath
arr []Off
}
)
func NewPath(e FilterPath) *Path { return &Path{Expr: e} }
func (f *Path) ApplyTo(b *Buffer, off Off, next bool) (res Off, more bool, err error) {
res, f.path, more, err = f.Expr.ApplyToGetPath(b, off, f.path[:0], next)
if err != nil {
return off, false, err
}
if res == None {
return None, more, nil
}
br := b.Reader()
bw := b.Writer()
f.arr = f.arr[:0]
for _, ps := range f.path {
tag := br.Tag(ps.Off)
var val Off
switch tag {
case cbor.Array:
val = bw.Int(ps.Index)
case cbor.Map:
val, _ = br.ArrayMapIndex(ps.Off, ps.Index)
default:
panic(tag)
}
f.arr = append(f.arr, val)
}
res = bw.Array(f.arr)
return res, more, nil
}
func (p Path) String() string { return fmt.Sprintf("path(%v)", p.Expr) }