forked from PaesslerAG/jsonpath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselector.go
More file actions
247 lines (219 loc) · 5.75 KB
/
Copy pathselector.go
File metadata and controls
247 lines (219 loc) · 5.75 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
package jsonpath
import (
"context"
"errors"
"fmt"
"strconv"
"github.com/PaesslerAG/gval"
)
//plainSelector evaluate exactly one result
type plainSelector func(c context.Context, r interface{}, v *PathValue) (*PathValue, error)
//ambiguousSelector evaluate wildcard
type ambiguousSelector func(c context.Context, r interface{}, v *PathValue, match ambiguousMatcher)
//@
func currentElementSelector() plainSelector {
return func(c context.Context, r interface{}, v *PathValue) (*PathValue, error) {
var pv PathValue
pv.Path = make([]string, 0)
pv.Value = c.Value(currentElement{})
return &pv, nil
}
}
type currentElement struct{}
func currentContext(c context.Context, v interface{}) context.Context {
return context.WithValue(c, currentElement{}, v)
}
//.x, [x]
func directSelector(key gval.Evaluable) plainSelector {
return func(c context.Context, r interface{}, v *PathValue) (*PathValue, error) {
var pv PathValue
pv.Path = append(v.Path[:0:0], v.Path...)
e, matchedPath, err := selectValue(c, key, r, v.Value)
if err != nil {
return v, err // TODO: better way to return pathValuePair
}
pv.Path = append(pv.Path, matchedPath)
pv.Value = e
return &pv, nil
}
}
// * / [*]
func starSelector() ambiguousSelector {
return func(c context.Context, r interface{}, v *PathValue, match ambiguousMatcher) {
var pv PathValue
pv.Path = append(v.Path[:0:0], v.Path...)
visitAll(v, func(key string, pval *PathValue) {
pv.Path = append(pv.Path, key)
match(key, pval) })
}
}
// [x, ...]
func multiSelector(keys []gval.Evaluable) ambiguousSelector {
if len(keys) == 0 {
return starSelector()
}
return func(c context.Context, r interface{}, v *PathValue, match ambiguousMatcher) {
for _, k := range keys {
var pv PathValue
pv.Path = append(v.Path[:0:0], v.Path...)
e, wildcard, err := selectValue(c, k, r, v.Value)
if err != nil {
continue
}
pv.Path = append(pv.Path, wildcard)
pv.Value = e
match(wildcard, &pv)
}
}
}
func selectValue(c context.Context, key gval.Evaluable, r, v interface{}) (value interface{}, jkey string, err error) {
c = currentContext(c, v)
switch o := v.(type) {
case []interface{}:
i, err := key.EvalInt(c, r)
if err != nil {
return nil, "", fmt.Errorf("could not select value, invalid key: %s", err)
}
if i < 0 || i >= len(o) {
return nil, "", fmt.Errorf("index %d out of bounds", i)
}
return o[i], strconv.Itoa(i), nil
case map[string]interface{}:
k, err := key.EvalString(c, r)
if err != nil {
return nil, "", fmt.Errorf("could not select value, invalid key: %s", err)
}
if r, ok := o[k]; ok {
return r, k, nil
}
return nil, "", fmt.Errorf("unknown key %s", k)
default:
return nil, "", fmt.Errorf("unsupported value type %T for select, expected map[string]interface{} or []interface{}", o)
}
}
//..
func mapperSelector() ambiguousSelector {
return mapper
}
func mapper(c context.Context, r interface{}, v *PathValue, match ambiguousMatcher) {
match([]interface{}{}, v)
visitAll(v, func(wildcard string, v *PathValue) {
mapper(c, r, v, func(key interface{}, v *PathValue) {
match(append([]interface{}{wildcard}, key.([]interface{})...), v)
})
})
}
func visitAll(pv *PathValue, visit func(key string, v *PathValue)) {
switch t := pv.Value.(type) {
case []interface{}:
values := pv.Value.([]interface{})
for i, e := range values {
k := "[" + strconv.Itoa(i) + "]"
var npv PathValue
npv.Path = append(pv.Path[:0:0], pv.Path...)
npv.Path = append(npv.Path, k)
npv.Value = e
visit(k, &npv)
}
case map[string]interface{}:
valueMap := pv.Value.(map[string]interface{})
for k, e := range valueMap{
var npv PathValue
npv.Path = append(pv.Path[:0:0], pv.Path...)
npv.Path = append(npv.Path, k)
npv.Value = e
visit(k, &npv)
}
default:
fmt.Errorf("Invalid type %T in visitAll", t)
return
}
}
//[? ]
func filterSelector(filter gval.Evaluable) ambiguousSelector {
return func(c context.Context, r interface{}, v *PathValue, match ambiguousMatcher) {
visitAll(v, func(wildcard string, v *PathValue) {
var pv PathValue
pv.Path = append(v.Path[:0:0], v.Path...)
pv.Value = v.Value
condition, err := filter.EvalBool(currentContext(c, pv.Value), r)
if err != nil {
return
}
if condition {
match(wildcard, &pv)
}
})
}
}
//[::]
func rangeSelector(min, max, step gval.Evaluable) ambiguousSelector {
return func(c context.Context, r interface{}, v *PathValue, match ambiguousMatcher) {
var pv PathValue
pv.Path = append(v.Path[:0:0], v.Path...)
pv.Value = v.Value
cs, ok := v.Value.([]interface{})
if !ok {
return
}
c = currentContext(c, v.Value)
min, err := min.EvalInt(c, r)
if err != nil {
return
}
max, err := max.EvalInt(c, r)
if err != nil {
return
}
step, err := step.EvalInt(c, r)
if err != nil {
return
}
if min > max {
return
}
n := len(cs)
min = negmax(min, n)
max = negmax(max, n)
if step == 0 {
step = 1
}
if step > 0 {
for i := min; i < max; i += step {
var pv PathValue
pv.Path = append(v.Path[:0:0], v.Path...)
pv.Value = cs[i]
match(strconv.Itoa(i), &pv)
}
} else {
for i := max - 1; i >= min; i += step {
var pv PathValue
pv.Path = append(v.Path[:0:0], v.Path...)
pv.Value = cs[i]
match(strconv.Itoa(i), &pv)
}
}
}
}
func negmax(n, max int) int {
if n < 0 {
n = max + n
if n < 0 {
n = 0
}
} else if n > max {
return max
}
return n
}
// ()
func newScript(script gval.Evaluable) plainSelector {
return func(c context.Context, r interface{}, v *PathValue) (*PathValue, error) {
val, err := script(currentContext(c, v.Value), r)
pv, ok := val.(PathValue)
if (!ok) {
return nil, errors.New("script return non path value pair") //TODO, refine error message
}
return &pv, err
}
}