-
Notifications
You must be signed in to change notification settings - Fork 37
/
parse.go
223 lines (206 loc) · 5.09 KB
/
parse.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
package jsonpath
import (
"context"
"fmt"
"math"
"text/scanner"
"github.com/PaesslerAG/gval"
)
type parser struct {
*gval.Parser
path path
}
func parseRootPath(ctx context.Context, gParser *gval.Parser) (r gval.Evaluable, err error) {
p := newParser(gParser)
return p.parse(ctx)
}
func parseCurrentPath(ctx context.Context, gParser *gval.Parser) (r gval.Evaluable, err error) {
p := newParser(gParser)
p.appendPlainSelector(currentElementSelector())
eval, err := p.parse(ctx)
// We always want to ensure that the machinery to collect values with paths is circumvented for current path
return func(ctx context.Context, parameter interface{}) (interface{}, error) {
if err != nil {
return nil, err
}
value, err := eval(ctx, parameter)
collectFullPaths := ctx.Value(CollectFullPathsContextKey{})
if b, ok := collectFullPaths.(bool); ok && b {
if m, ok := value.(map[string]interface{}); ok {
for _, v := range m {
return v, nil
}
}
}
return value, err
}, nil
}
func newParser(p *gval.Parser) *parser {
return &parser{Parser: p, path: plainPath{}}
}
func (p *parser) parse(c context.Context) (r gval.Evaluable, err error) {
err = p.parsePath(c)
if err != nil {
return nil, err
}
collectFullPaths := c.Value(CollectFullPathsContextKey{})
if b, ok := collectFullPaths.(bool); ok && b {
return p.path.evaluateWithPaths, nil
}
return p.path.evaluate, nil
}
func (p *parser) parsePath(c context.Context) error {
switch p.Scan() {
case '.':
return p.parseSelect(c)
case '[':
keys, seperator, err := p.parseBracket(c)
if err != nil {
return err
}
switch seperator {
case ':':
if len(keys) > 3 {
return fmt.Errorf("range query has at least the parameter [min:max:step]")
}
keys = append(keys, []gval.Evaluable{
p.Const(0), p.Const(float64(math.MaxInt32)), p.Const(1)}[len(keys):]...)
p.appendAmbiguousSelector(rangeSelector(keys[0], keys[1], keys[2]))
case '?':
if len(keys) != 1 {
return fmt.Errorf("filter needs exactly one key")
}
p.appendAmbiguousSelector(filterSelector(keys[0]))
default:
if len(keys) == 1 {
p.appendPlainSelector(directSelector(keys[0]))
} else {
p.appendAmbiguousSelector(multiSelector(keys))
}
}
return p.parsePath(c)
case '(':
return p.parseScript(c)
default:
p.Camouflage("jsonpath", '.', '[', '(')
return nil
}
}
func (p *parser) parseSelect(c context.Context) error {
scan := p.Scan()
switch scan {
case scanner.Ident:
p.appendPlainSelector(directSelector(p.Const(p.TokenText())))
return p.parsePath(c)
case '.':
p.appendAmbiguousSelector(mapperSelector())
return p.parseMapper(c)
case '*':
p.appendAmbiguousSelector(starSelector())
return p.parsePath(c)
default:
return p.Expected("JSON select", scanner.Ident, '.', '*')
}
}
func (p *parser) parseBracket(c context.Context) (keys []gval.Evaluable, seperator rune, err error) {
for {
scan := p.Scan()
skipScan := false
switch scan {
case '?':
skipScan = true
case ':':
i := float64(0)
if len(keys) == 1 {
i = math.MaxInt32
}
keys = append(keys, p.Const(i))
skipScan = true
case '*':
if p.Scan() != ']' {
return nil, 0, p.Expected("JSON bracket star", ']')
}
return []gval.Evaluable{}, 0, nil
case ']':
if seperator == ':' {
skipScan = true
break
}
fallthrough
default:
p.Camouflage("jsonpath brackets")
key, err := p.ParseExpression(c)
if err != nil {
return nil, 0, err
}
keys = append(keys, key)
}
if !skipScan {
scan = p.Scan()
}
if seperator == 0 {
seperator = scan
}
switch scan {
case ':', ',':
case ']':
return
case '?':
if len(keys) != 0 {
return nil, 0, p.Expected("JSON filter", ']')
}
default:
return nil, 0, p.Expected("JSON bracket separator", ':', ',')
}
if seperator != scan {
return nil, 0, fmt.Errorf("mixed %v and %v in JSON bracket", seperator, scan)
}
}
}
func (p *parser) parseMapper(c context.Context) error {
scan := p.Scan()
switch scan {
case scanner.Ident:
p.appendPlainSelector(directSelector(p.Const(p.TokenText())))
case '[':
keys, seperator, err := p.parseBracket(c)
if err != nil {
return err
}
switch seperator {
case ':':
return fmt.Errorf("mapper can not be combined with range query")
case '?':
if len(keys) != 1 {
return fmt.Errorf("filter needs exactly one key")
}
p.appendAmbiguousSelector(filterSelector(keys[0]))
default:
p.appendAmbiguousSelector(multiSelector(keys))
}
case '*':
p.appendAmbiguousSelector(starSelector())
case '(':
return p.parseScript(c)
default:
return p.Expected("JSON mapper", '[', scanner.Ident, '*')
}
return p.parsePath(c)
}
func (p *parser) parseScript(c context.Context) error {
script, err := p.ParseExpression(c)
if err != nil {
return err
}
if p.Scan() != ')' {
return p.Expected("jsonpath script", ')')
}
p.appendPlainSelector(newScript(script))
return p.parsePath(c)
}
func (p *parser) appendPlainSelector(next plainSelector) {
p.path = p.path.withPlainSelector(next)
}
func (p *parser) appendAmbiguousSelector(next ambiguousSelector) {
p.path = p.path.withAmbiguousSelector(next)
}