-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
179 lines (136 loc) · 3.82 KB
/
index.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
package jq
import (
"fmt"
"nikand.dev/go/cbor"
)
type (
Index int
IndexNoError int
Key string
KeyNoError string
addpath bool
)
const (
withPath addpath = true
withoutPath addpath = false
)
var (
_ FilterPath = Index(0)
_ FilterPath = IndexNoError(0)
_ FilterPath = Key("")
_ FilterPath = KeyNoError("")
)
func (f Index) ApplyToGetPath(b *Buffer, off Off, base NodePath, next bool) (res Off, path NodePath, more bool, err error) {
res, path, err = indexApplyTo(int(f), b, off, base, next, withPath, false)
err = fe(f, off, err)
return
}
func (f IndexNoError) ApplyToGetPath(b *Buffer, off Off, base NodePath, next bool) (res Off, path NodePath, more bool, err error) {
res, path, err = indexApplyTo(int(f), b, off, base, next, withPath, true)
err = fe(f, off, err)
return
}
func (f Index) ApplyTo(b *Buffer, off Off, next bool) (res Off, more bool, err error) {
res, _, err = indexApplyTo(int(f), b, off, nil, next, withoutPath, false)
err = fe(f, off, err)
return
}
func (f IndexNoError) ApplyTo(b *Buffer, off Off, next bool) (res Off, more bool, err error) {
res, _, err = indexApplyTo(int(f), b, off, nil, next, withoutPath, true)
err = fe(f, off, err)
return
}
func indexApplyTo(f int, b *Buffer, off Off, base NodePath, next bool, addpath addpath, noerr bool) (res Off, path NodePath, err error) {
if off == None || next {
return None, base, nil
}
path = base
if addpath {
path = append(base, NodePathSeg{Off: off, Index: f, Key: None})
}
if b.Equal(off, Null) {
return Null, path, nil
}
br := b.Reader()
tag := br.Tag(off)
if tag != cbor.Array && tag != cbor.Map {
if noerr {
return None, path, nil
}
return off, path, NewTypeError(tag, cbor.Array, cbor.Map)
}
l := br.ArrayMapLen(off)
if f < 0 {
f = l + f
}
if f < 0 || f >= l {
return Null, path, nil
}
_, res = b.Reader().ArrayMapIndex(off, f)
if addpath {
path[len(path)-1].Index = f
}
return res, path, nil
}
func (f Key) ApplyToGetPath(b *Buffer, off Off, base NodePath, next bool) (res Off, path NodePath, more bool, err error) {
res, path, err = keyApplyTo(string(f), b, off, base, next, true, false, None)
err = fe(f, off, err)
return
}
func (f KeyNoError) ApplyToGetPath(b *Buffer, off Off, base NodePath, next bool) (res Off, path NodePath, more bool, err error) {
res, path, err = keyApplyTo(string(f), b, off, base, next, true, true, None)
err = fe(f, off, err)
return
}
func (f Key) ApplyTo(b *Buffer, off Off, next bool) (res Off, more bool, err error) {
res, _, err = keyApplyTo(string(f), b, off, nil, next, false, false, None)
err = fe(f, off, err)
return
}
func (f KeyNoError) ApplyTo(b *Buffer, off Off, next bool) (res Off, more bool, err error) {
res, _, err = keyApplyTo(string(f), b, off, nil, next, false, true, None)
err = fe(f, off, err)
return
}
func keyApplyTo(f string, b *Buffer, off Off, base NodePath, next bool, addpath addpath, noerr bool, keyoff Off) (res Off, path NodePath, err error) {
if off == None || next {
return None, base, nil
}
path = base
if addpath {
path = append(base, NodePathSeg{Off: off, Index: -1, Key: keyoff})
}
br := b.Reader()
var l int
if !b.Equal(off, Null) {
tag := br.Tag(off)
if tag != cbor.Map {
if noerr {
return None, path[:len(base)], nil
}
return off, path, NewTypeError(tag, cbor.Map)
}
l = br.ArrayMapLen(off)
}
for j := range l {
k, v := br.ArrayMapIndex(off, j)
if string(br.Bytes(k)) != f {
continue
}
if addpath {
last := len(path) - 1
path[last].Index = j
path[last].Key = k
}
return v, path, nil
}
if addpath {
if keyoff == None {
keyoff = b.Writer().String(f)
}
path[len(path)-1].Key = keyoff
}
return Null, path, nil
}
func (f Index) String() string { return fmt.Sprintf(".[%d]", int(f)) }
func (f Key) String() string { return fmt.Sprintf(".%s", string(f)) }