forked from PaesslerAG/jsonpath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
204 lines (186 loc) · 4.45 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
package jsonpath
import (
"context"
"fmt"
"math"
goscanner "text/scanner"
"github.com/Intevation/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())
return p.parse(ctx)
}
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
}
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 goscanner.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", goscanner.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 goscanner.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", '[', goscanner.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)
}