-
Notifications
You must be signed in to change notification settings - Fork 1
/
collections.go
275 lines (246 loc) · 6.32 KB
/
collections.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package lookup
import (
"fmt"
"reflect"
"regexp"
"strconv"
)
type subFilterFunc struct {
expression Runner
}
func (s *subFilterFunc) Run(scope *Scope) Pathor {
b, err := interfaceToBoolOrParse(scope.Position.Raw())
if err != nil {
return NewInvalidor(scope.Path(), err)
}
if b {
return scope.Current
}
return NewInvalidor(scope.Path(), ErrEvalFail)
}
type filterFunc struct {
expression Runner
}
func Filter(expression Runner) *filterFunc {
return &filterFunc{
expression: expression,
}
}
func (ef *filterFunc) Run(scope *Scope) Pathor {
result := arrayOrSliceForEachPath(ExtractPath(scope.Position), nil, scope.Position.Value(), []Runner{
ef.expression,
&subFilterFunc{expression: Result()},
}, scope)
return result
}
type mapFunc struct {
expression Runner
}
func Map(expression Runner) *mapFunc {
return &mapFunc{
expression: expression,
}
}
func (ef *mapFunc) Run(scope *Scope) Pathor {
result := arrayOrSliceForEachPath(ExtractPath(scope.Position), nil, scope.Position.Value(), []Runner{ef.expression}, scope)
return result
}
func forEach(scope *Scope, v reflect.Value, ef func(pathor Pathor) error) Pathor {
switch v.Kind() {
case reflect.Array, reflect.Slice:
if v.IsNil() {
return &Invalidor{
err: fmt.Errorf("mil element at simple path %s element was %s expected array,slice,map,struct", scope.Path(), "nil"),
}
}
for i := 0; i < v.Len(); i++ {
f := v.Index(i)
p := scope.Path() + fmt.Sprintf("[%d]", i)
if err := ef(&Reflector{
path: p,
v: f,
}); err != nil {
return NewInvalidor(p, err)
}
}
}
return nil
}
type containsFunc struct {
expression Runner
}
func Contains(runner Runner) *containsFunc {
return &containsFunc{
expression: runner,
}
}
func (ef *containsFunc) Run(scope *Scope) Pathor {
result := ef.expression.Run(scope)
switch result.Value().Kind() {
case reflect.Slice, reflect.Array:
result = arrayOrSliceForEachPath(scope.Path(), nil, scope.Position.Value(), []Runner{
ValueOf(result),
}, scope)
return every(scope, equals(scope, result))
}
v := scope.Position.Value()
found := false
if err := forEach(scope, v, func(pathor Pathor) error {
p := Equals(NewConstantor(ExtractPath(result), result.Raw())).Run(scope.Next(pathor))
b, err := interfaceToBoolOrParse(p.Raw())
if err == nil && b {
found = true
}
return nil
}); err != nil {
return err
}
return NewConstantor(scope.Path(), found)
}
func In(e Runner) *inFunc {
return &inFunc{
expression: e,
}
}
type inFunc struct {
expression Runner
}
func (ef *inFunc) Run(scope *Scope) Pathor {
var result Pathor
switch scope.Position.Value().Kind() {
case reflect.Slice, reflect.Array:
result = arrayOrSliceForEachPath(scope.Path(), nil, scope.Position.Value(), []Runner{ef}, scope)
return any(scope, result)
default:
result = scope.Position
}
inThis := ef.expression.Run(scope)
inV := inThis.Value()
found := false
if err := forEach(scope, inV, func(pathor Pathor) error {
p := Equals(NewConstantor(ExtractPath(pathor), result.Raw())).Run(scope.Next(pathor))
b, err := interfaceToBoolOrParse(p.Raw())
if err == nil && b {
found = true
}
return nil
}); err != nil {
return err
}
return NewConstantor(scope.Path(), found)
}
type indexFunc struct {
i interface{}
}
func Index(i interface{}) *indexFunc {
return &indexFunc{
i: i,
}
}
func (i *indexFunc) Run(scope *Scope) Pathor {
switch scope.Position.Value().Kind() {
case reflect.Array, reflect.Slice:
return evaluateType(scope, scope.Position, i.i)
default:
return NewInvalidor(ExtractPath(scope.Position), ErrIndexOfNotArray)
}
}
func evaluateType(scope *Scope, pathor Pathor, i interface{}) Pathor {
if i == nil {
return pathor
}
switch reflect.TypeOf(i).Kind() {
case reflect.Float32, reflect.Float64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
ip, err := interfaceToInt(i)
if err != nil {
return NewInvalidor(ExtractPath(pathor), err)
}
return arrayOrSlicePath(fmt.Sprintf("%s[%d]", ExtractPath(pathor), ip), ip, pathor.Value())
case reflect.String:
simpleValue, err := regexp.Compile("^-?\\d+$")
if err != nil {
return NewInvalidor(ExtractPath(pathor), err)
}
if simpleValue.MatchString(i.(string)) {
ii, err := strconv.ParseInt(i.(string), 10, 64)
if err != nil {
return NewInvalidor(fmt.Sprintf("%s[%s]", ExtractPath(pathor), i.(string)), err)
}
return arrayOrSlicePath(fmt.Sprintf("%s[%d]", ExtractPath(pathor), ii), ii, pathor.Value())
}
case reflect.Struct, reflect.Ptr:
switch ii := i.(type) {
case Runner:
p := ii.Run(scope)
return evaluateType(scope, p, p.Raw())
case *Constantor:
return evaluateType(scope, pathor, ii.Raw())
default:
return NewInvalidor(ExtractPath(pathor), ErrIndexValueNotValid)
}
default:
return NewInvalidor(fmt.Sprintf("%s[]", ExtractPath(pathor)), ErrIndexValueNotValid)
}
return NewInvalidor(fmt.Sprintf("%s[]", ExtractPath(pathor)), ErrUnknownIndexMode)
}
func Every(e Runner) *everyFunc {
return &everyFunc{
expression: e,
}
}
type everyFunc struct {
expression Runner
}
func (ef *everyFunc) Run(scope *Scope) Pathor {
everyThis := ef.expression.Run(scope)
return every(scope, everyThis)
}
func every(scope *Scope, everyThis Pathor) Pathor {
v := everyThis.Value()
result := scope.Current
every := true
if err := forEach(scope, v, func(pathor Pathor) error {
p := Truthy(NewConstantor(ExtractPath(result), result.Raw())).Run(scope.Next(pathor))
b, err := interfaceToBoolOrParse(p.Raw())
if err == nil && b {
every = false
}
return nil
}); err != nil {
return err
}
return NewConstantor(scope.Path(), every)
}
func Any(e Runner) *anyFunc {
return &anyFunc{
expression: e,
}
}
type anyFunc struct {
expression Runner
}
func (ef *anyFunc) Run(scope *Scope) Pathor {
anyThis := ef.expression.Run(scope)
return any(scope, anyThis)
}
func any(scope *Scope, anyThis Pathor) Pathor {
v := anyThis.Value()
found := false
if err := forEach(scope, v, func(pathor Pathor) error {
p := truthy(scope, pathor)
b, err := interfaceToBoolOrParse(p.Raw())
if err == nil && b {
found = true
}
return nil
}); err != nil {
return err
}
return NewConstantor(scope.Path(), found)
}
// TODO Map
// TODO Union
// TODO Intersection
// TODO First - warp index
// TODO Last - wrap index
// TODO Range