-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.go
More file actions
896 lines (787 loc) · 22.2 KB
/
eval.go
File metadata and controls
896 lines (787 loc) · 22.2 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
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
package wirefilter
import (
"fmt"
"math"
"net"
"regexp"
"strings"
"time"
)
// Pre-allocated boolean values to avoid heap allocation on every comparison.
var (
boolTrue Value = BoolValue(true)
boolFalse Value = BoolValue(false)
)
func boolVal(v bool) Value {
if v {
return boolTrue
}
return boolFalse
}
func (f *Filter) evaluateArrayExpr(expr *ArrayExpr, ctx *ExecutionContext, depth int) (Value, error) {
var buf [8]Value
values := buf[:0]
if len(expr.Elements) > len(buf) {
values = make([]Value, 0, len(expr.Elements))
}
for _, elem := range expr.Elements {
if rangeExpr, ok := elem.(*RangeExpr); ok {
rangeVals, err := f.evaluateRangeExpr(rangeExpr, ctx, depth)
if err != nil {
return nil, err
}
if arr, ok := rangeVals.(ArrayValue); ok {
values = append(values, arr...)
} else if rangeVals != nil {
// IntervalValue or other non-array range result
values = append(values, rangeVals)
}
} else {
val, err := f.evaluate(elem, ctx, depth)
if err != nil {
return nil, err
}
values = append(values, val)
}
}
return ArrayValue(values), nil
}
func (f *Filter) evaluateRangeExpr(expr *RangeExpr, ctx *ExecutionContext, depth int) (Value, error) {
start, err := f.evaluate(expr.Start, ctx, depth)
if err != nil {
return nil, err
}
end, err := f.evaluate(expr.End, ctx, depth)
if err != nil {
return nil, err
}
if start == nil || end == nil {
return ArrayValue([]Value{}), nil
}
// Time/duration ranges produce IntervalValue instead of materialized arrays
if start.Type() == TypeTime && end.Type() == TypeTime {
return NewTimeInterval(start.(TimeValue), end.(TimeValue)), nil
}
if start.Type() == TypeDuration && end.Type() == TypeDuration {
return NewDurationInterval(start.(DurationValue), end.(DurationValue)), nil
}
if start.Type() == TypeInt && end.Type() == TypeInt {
return NewIntInterval(start.(IntValue), end.(IntValue)), nil
}
return ArrayValue([]Value{}), nil
}
func (f *Filter) evaluateFieldExpr(expr *FieldExpr, ctx *ExecutionContext) (Value, error) {
val, ok := ctx.GetField(expr.Name)
if !ok {
return nil, nil
}
return val, nil
}
func (f *Filter) evaluateIndexExpr(expr *IndexExpr, ctx *ExecutionContext, depth int) (Value, error) {
object, err := f.evaluate(expr.Object, ctx, depth)
if err != nil {
return nil, err
}
if object == nil {
return nil, nil
}
index, err := f.evaluate(expr.Index, ctx, depth)
if err != nil {
return nil, err
}
if index == nil {
return nil, nil
}
// Map access with string key (or any type converted to string)
if object.Type() == TypeMap {
mapVal := object.(MapValue)
var key string
if index.Type() == TypeString {
key = string(index.(StringValue))
} else {
key = index.String()
}
if val, ok := mapVal.Get(key); ok {
return val, nil
}
return nil, nil
}
// Array access with integer index
if object.Type() == TypeArray && index.Type() == TypeInt {
arr := object.(ArrayValue)
idx := int(index.(IntValue))
if idx < 0 || idx >= len(arr) {
return nil, nil // Out of bounds
}
return arr[idx], nil
}
return nil, nil
}
func (f *Filter) evaluateUnpackExpr(expr *UnpackExpr, ctx *ExecutionContext, depth int) (Value, error) {
arr, err := f.evaluate(expr.Array, ctx, depth)
if err != nil {
return nil, err
}
if arr == nil {
return nil, nil
}
if arr.Type() != TypeArray {
return nil, nil
}
return UnpackedArrayValue{Array: arr.(ArrayValue)}, nil
}
func (f *Filter) evaluateListRefExpr(expr *ListRefExpr, ctx *ExecutionContext) (Value, error) {
if set, ok := ctx.getSet(expr.Name); ok {
return set, nil
}
if list, ok := ctx.GetList(expr.Name); ok {
return list, nil
}
if table, ok := ctx.GetTable(expr.Name); ok {
return table, nil
}
return nil, nil
}
func (f *Filter) evaluateUnaryExpr(expr *UnaryExpr, ctx *ExecutionContext, depth int) (Value, error) {
operand, err := f.evaluate(expr.Operand, ctx, depth)
if err != nil {
return nil, err
}
if expr.Operator == TokenNot {
if operand == nil {
return boolTrue, nil
}
return boolVal(!operand.IsTruthy()), nil
}
return nil, nil
}
// evaluateLogicalOp handles short-circuit evaluation for logical operators (and, or, xor).
// Returns (result, handled, error) where handled=true if this was a logical operator.
func (f *Filter) evaluateLogicalOp(expr *BinaryExpr, left Value, ctx *ExecutionContext, depth int) (Value, bool, error) {
switch expr.Operator {
case TokenAnd:
leftTruthy := left != nil && left.IsTruthy()
if !leftTruthy {
return boolFalse, true, nil // Short-circuit: false and X = false
}
right, err := f.evaluate(expr.Right, ctx, depth)
if err != nil {
return nil, true, err
}
rightTruthy := right != nil && right.IsTruthy()
return boolVal(rightTruthy), true, nil
case TokenOr:
leftTruthy := left != nil && left.IsTruthy()
if leftTruthy {
return boolTrue, true, nil // Short-circuit: true or X = true
}
right, err := f.evaluate(expr.Right, ctx, depth)
if err != nil {
return nil, true, err
}
rightTruthy := right != nil && right.IsTruthy()
return boolVal(rightTruthy), true, nil
case TokenXor:
// XOR cannot short-circuit - both sides needed
right, err := f.evaluate(expr.Right, ctx, depth)
if err != nil {
return nil, true, err
}
leftTruthy := left != nil && left.IsTruthy()
rightTruthy := right != nil && right.IsTruthy()
return boolVal(leftTruthy != rightTruthy), true, nil
}
return nil, false, nil
}
func (f *Filter) evaluateBinaryExpr(expr *BinaryExpr, ctx *ExecutionContext, depth int) (Value, error) {
left, err := f.evaluate(expr.Left, ctx, depth)
if err != nil {
return nil, err
}
// Handle logical operators with short-circuit evaluation
if result, handled, err := f.evaluateLogicalOp(expr, left, ctx, depth); handled {
return result, err
}
// For non-logical operators, evaluate right side
right, err := f.evaluate(expr.Right, ctx, depth)
if err != nil {
return nil, err
}
// Handle UnpackedArrayValue - apply operation to each element (ANY semantics)
if uv, ok := left.(UnpackedArrayValue); ok {
return f.evaluateUnpackedBinaryExpr(uv, expr.Operator, right)
}
switch expr.Operator {
case TokenEq:
return f.evaluateEquality(left, right)
case TokenNe:
result, err := f.evaluateEquality(left, right)
if err != nil {
return nil, err
}
return boolVal(!result.IsTruthy()), nil
case TokenAllEq:
return f.evaluateAllEqual(left, right)
case TokenAnyNe:
return f.evaluateAnyNotEqual(left, right)
case TokenLt:
return f.evaluateComparison(left, right, func(a, b int64) bool { return a < b })
case TokenGt:
return f.evaluateComparison(left, right, func(a, b int64) bool { return a > b })
case TokenLe:
return f.evaluateComparison(left, right, func(a, b int64) bool { return a <= b })
case TokenGe:
return f.evaluateComparison(left, right, func(a, b int64) bool { return a >= b })
case TokenContains:
return f.evaluateContains(left, right)
case TokenMatches:
return f.evaluateMatches(left, right)
case TokenIn:
return f.evaluateIn(left, right)
case TokenWildcard:
return f.evaluateWildcard(left, right, false)
case TokenStrictWildcard:
return f.evaluateWildcard(left, right, true)
case TokenPlus, TokenMinus, TokenAsterisk, TokenDiv, TokenMod:
return f.evaluateArithmetic(left, right, expr.Operator)
}
return boolFalse, nil
}
func (f *Filter) evaluateUnpackedBinaryExpr(uv UnpackedArrayValue, op TokenType, right Value) (Value, error) {
if len(uv.Array) == 0 {
return boolFalse, nil
}
// Apply operation to each element, return true if ANY matches
for _, elem := range uv.Array {
var result Value
var err error
switch op {
case TokenEq:
result, err = f.evaluateEquality(elem, right)
case TokenNe:
eqResult, eqErr := f.evaluateEquality(elem, right)
if eqErr != nil {
return nil, eqErr
}
result = boolVal(!eqResult.IsTruthy())
case TokenLt:
result, err = f.evaluateComparison(elem, right, func(a, b int64) bool { return a < b })
case TokenGt:
result, err = f.evaluateComparison(elem, right, func(a, b int64) bool { return a > b })
case TokenLe:
result, err = f.evaluateComparison(elem, right, func(a, b int64) bool { return a <= b })
case TokenGe:
result, err = f.evaluateComparison(elem, right, func(a, b int64) bool { return a >= b })
case TokenContains:
result, err = f.evaluateContains(elem, right)
case TokenMatches:
result, err = f.evaluateMatches(elem, right)
case TokenWildcard:
result, err = f.evaluateWildcard(elem, right, false)
case TokenStrictWildcard:
result, err = f.evaluateWildcard(elem, right, true)
case TokenIn:
result, err = f.evaluateIn(elem, right)
default:
continue
}
if err != nil {
return nil, err
}
if result != nil && result.IsTruthy() {
return boolTrue, nil
}
}
return boolFalse, nil
}
func (f *Filter) evaluateEquality(left, right Value) (Value, error) {
if left == nil || right == nil {
return boolVal(left == nil && right == nil), nil
}
switch {
case left.Type() == TypeIP && right.Type() == TypeString:
ip := NormalizeIP(net.ParseIP(string(right.(StringValue))))
if ip == nil {
return boolFalse, nil
}
right = IPValue{IP: ip}
case left.Type() == TypeString && right.Type() == TypeIP:
ip := NormalizeIP(net.ParseIP(string(left.(StringValue))))
if ip == nil {
return boolFalse, nil
}
left = IPValue{IP: ip}
case left.Type() == TypeCIDR && right.Type() == TypeString:
_, ipNet, err := net.ParseCIDR(string(right.(StringValue)))
if err != nil {
return boolFalse, nil
}
right = CIDRValue{IPNet: ipNet}
case left.Type() == TypeString && right.Type() == TypeCIDR:
_, ipNet, err := net.ParseCIDR(string(left.(StringValue)))
if err != nil {
return boolFalse, nil
}
left = CIDRValue{IPNet: ipNet}
case left.Type() == TypeTime && right.Type() == TypeString:
t, err := time.Parse(time.RFC3339Nano, string(right.(StringValue)))
if err != nil {
return boolFalse, nil
}
right = NewTimeValue(t)
case left.Type() == TypeString && right.Type() == TypeTime:
t, err := time.Parse(time.RFC3339Nano, string(left.(StringValue)))
if err != nil {
return boolFalse, nil
}
left = NewTimeValue(t)
}
return boolVal(left.Equal(right)), nil
}
func (f *Filter) evaluateComparison(left, right Value, cmp func(int64, int64) bool) (Value, error) {
if left == nil || right == nil {
return boolFalse, nil
}
// Time vs time comparison
if left.Type() == TypeTime && right.Type() == TypeTime {
return boolVal(cmp(int64(left.(TimeValue))-int64(right.(TimeValue)), 0)), nil
}
// Duration vs duration comparison
if left.Type() == TypeDuration && right.Type() == TypeDuration {
return boolVal(cmp(int64(left.(DurationValue)), int64(right.(DurationValue)))), nil
}
// Handle Float and mixed Int/Float comparisons
if left.Type() == TypeFloat || right.Type() == TypeFloat {
leftF, leftOk := toFloat64(left)
rightF, rightOk := toFloat64(right)
if !leftOk || !rightOk {
return boolFalse, nil
}
// Map the int64 comparator to float64 by comparing equivalent sign values
return boolVal(cmp(floatSign(leftF-rightF), 0)), nil
}
if left.Type() != TypeInt || right.Type() != TypeInt {
return boolFalse, nil
}
return boolVal(cmp(int64(left.(IntValue)), int64(right.(IntValue)))), nil
}
// toFloat64 converts Int or Float values to float64 for mixed comparisons.
func toFloat64(v Value) (float64, bool) {
switch val := v.(type) {
case FloatValue:
return float64(val), true
case IntValue:
return float64(val), true
}
return 0, false
}
func (f *Filter) evaluateArithmetic(left, right Value, op TokenType) (Value, error) {
if left == nil || right == nil {
return nil, nil
}
if result, ok := f.evaluateTemporalArithmetic(left, right, op); ok {
return result, nil
}
if left.Type() == TypeFloat || right.Type() == TypeFloat {
return evalFloatArithmetic(left, right, op)
}
if left.Type() == TypeInt && right.Type() == TypeInt {
return evalIntArithmetic(int64(left.(IntValue)), int64(right.(IntValue)), op)
}
return nil, nil
}
func evalFloatArithmetic(left, right Value, op TokenType) (Value, error) {
lf, lok := toFloat64(left)
rf, rok := toFloat64(right)
if !lok || !rok {
return nil, nil
}
switch op {
case TokenPlus:
return FloatValue(lf + rf), nil
case TokenMinus:
return FloatValue(lf - rf), nil
case TokenAsterisk:
return FloatValue(lf * rf), nil
case TokenDiv:
if rf == 0 {
return nil, nil
}
return FloatValue(lf / rf), nil
case TokenMod:
if rf == 0 {
return nil, nil
}
return FloatValue(math.Mod(lf, rf)), nil
}
return nil, nil
}
func evalIntArithmetic(li, ri int64, op TokenType) (Value, error) {
switch op {
case TokenPlus:
result := li + ri
if (ri > 0 && result < li) || (ri < 0 && result > li) {
return nil, nil
}
return IntValue(result), nil
case TokenMinus:
result := li - ri
if (ri > 0 && result > li) || (ri < 0 && result < li) {
return nil, nil
}
return IntValue(result), nil
case TokenAsterisk:
if li != 0 && ri != 0 {
result := li * ri
if result/ri != li {
return nil, nil
}
return IntValue(result), nil
}
return IntValue(0), nil
case TokenDiv:
if ri == 0 {
return nil, nil
}
return IntValue(li / ri), nil
case TokenMod:
if ri == 0 {
return nil, nil
}
return IntValue(li % ri), nil
}
return nil, nil
}
// evaluateTemporalArithmetic handles arithmetic involving time and duration values.
// Returns (result, true) if temporal arithmetic was applied, (nil, false) otherwise.
func (f *Filter) evaluateTemporalArithmetic(left, right Value, op TokenType) (Value, bool) {
lt, rt := left.Type(), right.Type()
// time +/- duration = time
if lt == TypeTime && rt == TypeDuration {
return evalTimeAndDuration(left.(TimeValue), right.(DurationValue), op)
}
// duration + time = time (commutative addition only)
if lt == TypeDuration && rt == TypeTime && op == TokenPlus {
return TimeValue(int64(right.(TimeValue)) + int64(left.(DurationValue))), true
}
// time - time = duration
if lt == TypeTime && rt == TypeTime && op == TokenMinus {
return DurationValue(int64(left.(TimeValue)) - int64(right.(TimeValue))), true
}
// duration op duration
if lt == TypeDuration && rt == TypeDuration {
return evalDurationAndDuration(left.(DurationValue), right.(DurationValue), op)
}
// duration * scalar or scalar * duration
if op == TokenAsterisk {
return evalDurationMultiply(left, right, lt, rt)
}
// duration / scalar
if lt == TypeDuration && (rt == TypeInt || rt == TypeFloat) && op == TokenDiv {
return evalDurationDivScalar(left.(DurationValue), right)
}
return nil, false
}
func evalTimeAndDuration(tv TimeValue, dv DurationValue, op TokenType) (Value, bool) {
switch op {
case TokenPlus:
return TimeValue(int64(tv) + int64(dv)), true
case TokenMinus:
return TimeValue(int64(tv) - int64(dv)), true
}
return nil, true
}
func evalDurationAndDuration(left, right DurationValue, op TokenType) (Value, bool) {
ld, rd := time.Duration(left), time.Duration(right)
switch op {
case TokenPlus:
return DurationValue(ld + rd), true
case TokenMinus:
return DurationValue(ld - rd), true
case TokenDiv:
if rd == 0 {
return nil, true
}
return IntValue(ld / rd), true
case TokenMod:
if rd == 0 {
return nil, true
}
return DurationValue(ld % rd), true
}
return nil, false
}
func evalDurationMultiply(left, right Value, lt, rt Type) (Value, bool) {
if lt == TypeDuration && (rt == TypeInt || rt == TypeFloat) {
d := time.Duration(left.(DurationValue))
if rt == TypeInt {
return DurationValue(d * time.Duration(int64(right.(IntValue)))), true
}
return DurationValue(time.Duration(float64(d) * float64(right.(FloatValue)))), true
}
if (lt == TypeInt || lt == TypeFloat) && rt == TypeDuration {
d := time.Duration(right.(DurationValue))
if lt == TypeInt {
return DurationValue(d * time.Duration(int64(left.(IntValue)))), true
}
return DurationValue(time.Duration(float64(d) * float64(left.(FloatValue)))), true
}
return nil, false
}
func evalDurationDivScalar(dv DurationValue, right Value) (Value, bool) {
d := time.Duration(dv)
if right.Type() == TypeInt {
ri := int64(right.(IntValue))
if ri == 0 {
return nil, true
}
return DurationValue(d / time.Duration(ri)), true
}
rf := float64(right.(FloatValue))
if rf == 0 {
return nil, true
}
return DurationValue(time.Duration(float64(d) / rf)), true
}
// floatSign returns -1, 0, or 1 as an int64 based on the sign of f.
func floatSign(f float64) int64 {
if f < 0 {
return -1
}
if f > 0 {
return 1
}
return 0
}
func (f *Filter) evaluateContains(left, right Value) (Value, error) {
if left == nil || right == nil {
return boolFalse, nil
}
if left.Type() == TypeString && right.Type() == TypeString {
return boolVal(strings.Contains(string(left.(StringValue)), string(right.(StringValue)))), nil
}
if left.Type() == TypeArray {
leftArr := left.(ArrayValue)
// Array contains Array: AND logic - all elements from right exist in left
if right.Type() == TypeArray {
rightArr := right.(ArrayValue)
if len(rightArr) == 0 {
return boolTrue, nil
}
for _, rightElem := range rightArr {
if !leftArr.Contains(rightElem) {
return boolFalse, nil
}
}
return boolTrue, nil
}
// Array contains single value
return boolVal(leftArr.Contains(right)), nil
}
return boolFalse, nil
}
func (f *Filter) evaluateMatches(left, right Value) (Value, error) {
if left == nil || right == nil {
return boolFalse, nil
}
if left.Type() != TypeString || right.Type() != TypeString {
return boolFalse, nil
}
pattern := string(right.(StringValue))
re, err := f.getCompiledRegex(pattern)
if err != nil {
return boolFalse, err
}
return boolVal(re.MatchString(string(left.(StringValue)))), nil
}
func (f *Filter) getCompiledRegex(pattern string) (*regexp.Regexp, error) {
f.regexMu.RLock()
if re, ok := f.regexCache[pattern]; ok {
f.regexMu.RUnlock()
return re, nil
}
f.regexMu.RUnlock()
re, err := regexp.Compile(pattern)
if err != nil {
return nil, err
}
f.regexMu.Lock()
if len(f.regexCache) < maxFilterCacheSize {
f.regexCache[pattern] = re
}
f.regexMu.Unlock()
return re, nil
}
func (f *Filter) evaluateIn(left, right Value) (Value, error) {
if left == nil || right == nil {
return boolFalse, nil
}
// Handle SetValue for O(1) membership
if sv, ok := right.(SetValue); ok {
return boolVal(sv.Contains(left)), nil
}
// Handle IP in CIDR directly: ip.src in 192.168.0.0/24
if left.Type() == TypeIP && right.Type() == TypeCIDR {
ipVal := left.(IPValue)
cidrVal := right.(CIDRValue)
return boolVal(cidrVal.Contains(ipVal.IP)), nil
}
// Handle IntervalValue: value in interval (range membership)
if iv, ok := right.(IntervalValue); ok {
return boolVal(iv.Contains(left)), nil
}
if right.Type() == TypeArray {
rightArr := right.(ArrayValue)
// IP in Array: check if IP matches any element (IP equality or CIDR containment)
if left.Type() == TypeIP {
ipVal := left.(IPValue)
for _, elem := range rightArr {
if elem == nil {
continue
}
switch elem.Type() {
case TypeIP:
if ipVal.IP.Equal(elem.(IPValue).IP) {
return boolTrue, nil
}
case TypeCIDR:
if elem.(CIDRValue).Contains(ipVal.IP) {
return boolTrue, nil
}
}
}
return boolFalse, nil
}
// Array in Array: OR logic - any element from left exists in right
if left.Type() == TypeArray {
leftArr := left.(ArrayValue)
for _, leftElem := range leftArr {
if rightArr.Contains(leftElem) {
return boolTrue, nil
}
}
return boolFalse, nil
}
// Single value in Array (check for IntervalValue elements)
for _, elem := range rightArr {
if iv, ok := elem.(IntervalValue); ok {
if iv.Contains(left) {
return boolTrue, nil
}
continue
}
}
return boolVal(rightArr.Contains(left)), nil
}
// Legacy: IP in CIDR as string (keep for backwards compatibility)
if left.Type() == TypeIP && right.Type() == TypeString {
ipVal := left.(IPValue)
cidr := string(right.(StringValue))
ipNet, err := f.getParsedCIDR(cidr)
if err != nil {
return boolFalse, err
}
return boolVal(ipNet.Contains(ipVal.IP)), nil
}
return boolFalse, nil
}
func (f *Filter) getParsedCIDR(cidr string) (*net.IPNet, error) {
f.cidrMu.RLock()
if ipNet, ok := f.cidrCache[cidr]; ok {
f.cidrMu.RUnlock()
return ipNet, nil
}
f.cidrMu.RUnlock()
_, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, err
}
f.cidrMu.Lock()
if len(f.cidrCache) < maxFilterCacheSize {
f.cidrCache[cidr] = ipNet
}
f.cidrMu.Unlock()
return ipNet, nil
}
func (f *Filter) evaluateAllEqual(left, right Value) (Value, error) {
if left == nil || right == nil {
return boolFalse, nil
}
if left.Type() != TypeArray {
return boolFalse, nil
}
arr := left.(ArrayValue)
if len(arr) == 0 {
return boolFalse, nil
}
for _, elem := range arr {
result, err := f.evaluateEquality(elem, right)
if err != nil {
return nil, err
}
if !result.IsTruthy() {
return boolFalse, nil
}
}
return boolTrue, nil
}
func (f *Filter) evaluateAnyNotEqual(left, right Value) (Value, error) {
if left == nil || right == nil {
return boolFalse, nil
}
if left.Type() != TypeArray {
return boolFalse, nil
}
arr := left.(ArrayValue)
if len(arr) == 0 {
return boolFalse, nil
}
for _, elem := range arr {
result, err := f.evaluateEquality(elem, right)
if err != nil {
return nil, err
}
if !result.IsTruthy() {
return boolTrue, nil
}
}
return boolFalse, nil
}
func (f *Filter) evaluateWildcard(left, right Value, caseSensitive bool) (Value, error) {
if left == nil || right == nil {
return boolFalse, nil
}
if left.Type() != TypeString || right.Type() != TypeString {
return boolFalse, nil
}
pattern := string(right.(StringValue))
text := string(left.(StringValue))
regexPattern := globToRegex(pattern)
if !caseSensitive {
regexPattern = fmt.Sprintf("(?i)%s", regexPattern)
}
re, err := f.getCompiledRegex(regexPattern)
if err != nil {
return boolFalse, err
}
return boolVal(re.MatchString(text)), nil
}
func globToRegex(glob string) string {
var result []byte
result = append(result, '^')
for i := 0; i < len(glob); i++ {
ch := glob[i]
switch ch {
case '*':
result = append(result, '.', '*')
case '?':
result = append(result, '.')
case '.', '+', '^', '$', '(', ')', '[', ']', '{', '}', '|', '\\':
result = append(result, '\\', ch)
default:
result = append(result, ch)
}
}
result = append(result, '$')
return string(result)
}