-
Notifications
You must be signed in to change notification settings - Fork 117
/
filtering.go
432 lines (385 loc) · 12.5 KB
/
filtering.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package query
import (
"fmt"
"reflect"
"regexp"
"strings"
"github.com/golang/protobuf/proto"
)
// Filter is a shortcut to parse a filter string using default FilteringParser implementation
// and call Filter on the returned filtering expression.
func Filter(obj interface{}, filter string) (bool, error) {
f, err := ParseFiltering(filter)
if err != nil {
return false, err
}
return f.Filter(obj)
}
// FilteringExpression is the interface implemented by types that represent nodes in a filtering expression AST.
type FilteringExpression interface {
Filter(interface{}) (bool, error)
}
// Matcher is implemented by structs that require custom filtering logic.
type Matcher interface {
Match(*Filtering) (bool, error)
}
// Filter evaluates underlying filtering expression against obj.
// If obj implements Matcher, call it's custom implementation.
func (m *Filtering) Filter(obj interface{}) (bool, error) {
if m == nil {
return true, nil
}
if matcher, ok := obj.(Matcher); ok {
return matcher.Match(m)
}
r := m.Root
if f, ok := r.(FilteringExpression); ok {
return f.Filter(obj)
} else {
return false, fmt.Errorf("%T type does not implement FilteringExpression", r)
}
}
// TypeMismatchError representes a type that is required for a value under FieldPath.
type TypeMismatchError struct {
ReqType string
FieldPath []string
}
func (e *TypeMismatchError) Error() string {
return fmt.Sprintf("%s is not a %s type", strings.Join(e.FieldPath, "."), e.ReqType)
}
// UnsupportedOperatorError represents an operator that is not supported by a particular field type.
type UnsupportedOperatorError struct {
Type string
Op string
}
func (e *UnsupportedOperatorError) Error() string {
return fmt.Sprintf("%s is not supported for %s type", e.Op, e.Type)
}
// Filter evaluates filtering expression against obj.
func (lop *LogicalOperator) Filter(obj interface{}) (bool, error) {
var res bool
var err error
l := lop.Left
if f, ok := l.(FilteringExpression); ok {
res, err = f.Filter(obj)
if err != nil {
return false, err
}
} else {
return false, fmt.Errorf("%T type does not implement FilteringExpression", l)
}
if lop.Type == LogicalOperator_AND && !res {
return negateIfNeeded(lop.IsNegative, false), nil
} else if lop.Type == LogicalOperator_OR && res {
return negateIfNeeded(lop.IsNegative, true), nil
}
r := lop.Right
if f, ok := r.(FilteringExpression); ok {
res, err = f.Filter(obj)
if err != nil {
return false, err
}
} else {
return false, fmt.Errorf("%T type does not implement FilteringExpression", r)
}
return negateIfNeeded(lop.IsNegative, res), nil
}
// Filter evaluates string condition against obj.
// If obj is a proto message, then 'protobuf' tag is used to map FieldPath to obj's struct fields,
// otherwise 'json' tag is used.
func (c *StringCondition) Filter(obj interface{}) (bool, error) {
fv := fieldByFieldPath(obj, c.FieldPath)
fv = dereferenceValue(fv)
if fv.Kind() != reflect.String {
return false, &TypeMismatchError{"string", c.FieldPath}
}
s := fv.String()
switch c.Type {
case StringCondition_EQ:
return negateIfNeeded(s == c.Value, c.IsNegative), nil
case StringCondition_IEQ:
return negateIfNeeded(strings.ToLower(s) == strings.ToLower(c.Value), c.IsNegative), nil
case StringCondition_MATCH:
// add regex caching
matched, err := regexp.MatchString(c.Value, s)
if err != nil {
return false, err
}
return negateIfNeeded(matched, c.IsNegative), nil
case StringCondition_GT:
return negateIfNeeded(s > c.Value, c.IsNegative), nil
case StringCondition_GE:
return negateIfNeeded(s >= c.Value, c.IsNegative), nil
case StringCondition_LT:
return negateIfNeeded(s < c.Value, c.IsNegative), nil
case StringCondition_LE:
return negateIfNeeded(s <= c.Value, c.IsNegative), nil
default:
return false, &UnsupportedOperatorError{"string", c.Type.String()}
}
}
// Filter evaluates number condition against obj.
// If obj is a proto message, then 'protobuf' tag is used to map FieldPath to obj's struct fields,
// otherwise 'json' tag is used.
func (c *NumberCondition) Filter(obj interface{}) (bool, error) {
fv := fieldByFieldPath(obj, c.FieldPath)
fv = dereferenceValue(fv)
var f float64
switch fv.Kind() {
case reflect.Float32, reflect.Float64:
f = fv.Float()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
f = float64(fv.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
f = float64(fv.Uint())
default:
return false, &TypeMismatchError{"number", c.FieldPath}
}
switch c.Type {
case NumberCondition_EQ:
return negateIfNeeded(f == c.Value, c.IsNegative), nil
case NumberCondition_GT:
return negateIfNeeded(f > c.Value, c.IsNegative), nil
case NumberCondition_GE:
return negateIfNeeded(f >= c.Value, c.IsNegative), nil
case NumberCondition_LT:
return negateIfNeeded(f < c.Value, c.IsNegative), nil
case NumberCondition_LE:
return negateIfNeeded(f <= c.Value, c.IsNegative), nil
default:
return false, &UnsupportedOperatorError{"number", c.Type.String()}
}
}
// Filter evaluates null condition against obj.
// If obj is a proto message, then 'protobuf' tag is used to map FieldPath to obj's struct fields,
// otherwise 'json' tag is used.
func (c *NullCondition) Filter(obj interface{}) (bool, error) {
fv := fieldByFieldPath(obj, c.FieldPath)
if fv.Kind() != reflect.Ptr {
return false, &TypeMismatchError{"nullable", c.FieldPath}
}
return negateIfNeeded(fv.IsNil(), c.IsNegative), nil
}
func (c *StringArrayCondition) Filter(obj interface{}) (bool, error) {
fv := fieldByFieldPath(obj, c.FieldPath)
fv = dereferenceValue(fv)
if fv.Kind() != reflect.String {
return false, &TypeMismatchError{"string", c.FieldPath}
}
s := fv.String()
switch c.Type {
case StringArrayCondition_IN:
return negateIfNeeded(stringInSlice(s, c.Values), c.IsNegative), nil
default:
return false, &UnsupportedOperatorError{"[]string", c.Type.String()}
}
}
func stringInSlice(s string, slice []string) bool {
for _, val := range slice {
if val == s {
return true
}
}
return false
}
func (c *NumberArrayCondition) Filter(obj interface{}) (bool, error) {
fv := fieldByFieldPath(obj, c.FieldPath)
fv = dereferenceValue(fv)
var f float64
switch fv.Kind() {
case reflect.Float32, reflect.Float64:
f = fv.Float()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
f = float64(fv.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
f = float64(fv.Uint())
default:
return false, &TypeMismatchError{"number", c.FieldPath}
}
switch c.Type {
case NumberArrayCondition_IN:
return negateIfNeeded(floatInSlice(f, c.Values), c.IsNegative), nil
default:
return false, &UnsupportedOperatorError{"number", c.Type.String()}
}
}
func floatInSlice(digit float64, slice []float64) bool {
for _, val := range slice {
if digit == val {
return true
}
}
return false
}
func fieldByFieldPath(obj interface{}, fieldPath []string) reflect.Value {
switch obj.(type) {
case proto.Message:
return fieldByProtoPath(obj, fieldPath)
default:
return fieldByJSONPath(obj, fieldPath)
}
}
func fieldByProtoPath(obj interface{}, protoPath []string) reflect.Value {
v := dereferenceValue(reflect.ValueOf(obj))
props := proto.GetProperties(v.Type())
for _, p := range props.Prop {
if p.OrigName == protoPath[0] {
return v.FieldByName(p.Name)
}
if p.JSONName == protoPath[0] {
return v.FieldByName(p.Name)
}
}
return reflect.Value{}
}
func fieldByJSONPath(obj interface{}, jsonPath []string) reflect.Value {
v := dereferenceValue(reflect.ValueOf(obj))
t := v.Type()
for i := 0; i < t.NumField(); i++ {
sf := t.Field(i)
if getJSONName(sf) == jsonPath[0] {
return v.Field(i)
}
}
return reflect.Value{}
}
func getJSONName(sf reflect.StructField) string {
if jsonTag, ok := sf.Tag.Lookup("json"); ok {
return strings.Split(jsonTag, ",")[0]
}
return sf.Name
}
func dereferenceValue(value reflect.Value) reflect.Value {
kind := value.Kind()
for kind == reflect.Ptr || kind == reflect.Interface {
value = value.Elem()
kind = value.Kind()
}
return value
}
func negateIfNeeded(neg bool, value bool) bool {
if neg {
return !value
}
return value
}
func (m *Filtering_Operator) Filter(obj interface{}) (bool, error) {
return m.Operator.Filter(obj)
}
func (m *Filtering_StringCondition) Filter(obj interface{}) (bool, error) {
return m.StringCondition.Filter(obj)
}
func (m *Filtering_NumberCondition) Filter(obj interface{}) (bool, error) {
return m.NumberCondition.Filter(obj)
}
func (m *Filtering_NullCondition) Filter(obj interface{}) (bool, error) {
return m.NullCondition.Filter(obj)
}
func (m *Filtering_StringArrayCondition) Filter(obj interface{}) (bool, error) {
return m.StringArrayCondition.Filter(obj)
}
func (m *Filtering_NumberArrayCondition) Filter(obj interface{}) (bool, error) {
return m.NumberArrayCondition.Filter(obj)
}
func (m *LogicalOperator_LeftOperator) Filter(obj interface{}) (bool, error) {
return m.LeftOperator.Filter(obj)
}
func (m *LogicalOperator_LeftStringCondition) Filter(obj interface{}) (bool, error) {
return m.LeftStringCondition.Filter(obj)
}
func (m *LogicalOperator_LeftNumberCondition) Filter(obj interface{}) (bool, error) {
return m.LeftNumberCondition.Filter(obj)
}
func (m *LogicalOperator_LeftNullCondition) Filter(obj interface{}) (bool, error) {
return m.LeftNullCondition.Filter(obj)
}
func (m *LogicalOperator_LeftStringArrayCondition) Filter(obj interface{}) (bool, error) {
return m.LeftStringArrayCondition.Filter(obj)
}
func (m *LogicalOperator_LeftNumberArrayCondition) Filter(obj interface{}) (bool, error) {
return m.LeftNumberArrayCondition.Filter(obj)
}
func (m *LogicalOperator_RightOperator) Filter(obj interface{}) (bool, error) {
return m.RightOperator.Filter(obj)
}
func (m *LogicalOperator_RightStringCondition) Filter(obj interface{}) (bool, error) {
return m.RightStringCondition.Filter(obj)
}
func (m *LogicalOperator_RightNumberCondition) Filter(obj interface{}) (bool, error) {
return m.RightNumberCondition.Filter(obj)
}
func (m *LogicalOperator_RightNullCondition) Filter(obj interface{}) (bool, error) {
return m.RightNullCondition.Filter(obj)
}
func (m *LogicalOperator_RightStringArrayCondition) Filter(obj interface{}) (bool, error) {
return m.RightStringArrayCondition.Filter(obj)
}
func (m *LogicalOperator_RightNumberArrayCondition) Filter(obj interface{}) (bool, error) {
return m.RightNumberArrayCondition.Filter(obj)
}
// SetRoot automatically wraps r into appropriate oneof structure and sets it to Root.
func (m *Filtering) SetRoot(r interface{}) error {
switch x := r.(type) {
case *LogicalOperator:
m.Root = &Filtering_Operator{x}
case *StringCondition:
m.Root = &Filtering_StringCondition{x}
case *NumberCondition:
m.Root = &Filtering_NumberCondition{x}
case *NullCondition:
m.Root = &Filtering_NullCondition{x}
case *StringArrayCondition:
m.Root = &Filtering_StringArrayCondition{x}
case *NumberArrayCondition:
m.Root = &Filtering_NumberArrayCondition{x}
case nil:
m.Root = nil
default:
return fmt.Errorf("Filtering.Root cannot be assigned to type %T", x)
}
return nil
}
// SetLeft automatically wraps l into appropriate oneof structure and sets it to Root.
func (m *LogicalOperator) SetLeft(l interface{}) error {
switch x := l.(type) {
case *LogicalOperator:
m.Left = &LogicalOperator_LeftOperator{x}
case *StringCondition:
m.Left = &LogicalOperator_LeftStringCondition{x}
case *NumberCondition:
m.Left = &LogicalOperator_LeftNumberCondition{x}
case *NullCondition:
m.Left = &LogicalOperator_LeftNullCondition{x}
case *StringArrayCondition:
m.Left = &LogicalOperator_LeftStringArrayCondition{x}
case *NumberArrayCondition:
m.Left = &LogicalOperator_LeftNumberArrayCondition{x}
case nil:
m.Left = nil
default:
return fmt.Errorf("Filtering.Left cannot be assigned to type %T", x)
}
return nil
}
// SetRight automatically wraps r into appropriate oneof structure and sets it to Root.
func (m *LogicalOperator) SetRight(r interface{}) error {
switch x := r.(type) {
case *LogicalOperator:
m.Right = &LogicalOperator_RightOperator{x}
case *StringCondition:
m.Right = &LogicalOperator_RightStringCondition{x}
case *NumberCondition:
m.Right = &LogicalOperator_RightNumberCondition{x}
case *NullCondition:
m.Right = &LogicalOperator_RightNullCondition{x}
case *StringArrayCondition:
m.Right = &LogicalOperator_RightStringArrayCondition{x}
case *NumberArrayCondition:
m.Right = &LogicalOperator_RightNumberArrayCondition{x}
case nil:
m.Right = nil
default:
return fmt.Errorf("Filtering.Right cannot be assigned to type %T", x)
}
return nil
}