Skip to content

Commit 1f1b760

Browse files
committed
Manage numeric comparisons
1 parent c6200c6 commit 1f1b760

File tree

8 files changed

+254
-73
lines changed

8 files changed

+254
-73
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,7 @@ Images
955955
image-push Push MULTIARCH_TARGETS images
956956
manifest-build Build MULTIARCH_TARGETS manifest
957957
manifest-push Push MULTIARCH_TARGETS manifest
958+
goyacc Regenerate filters query langage
958959
959960
kubernetes
960961
deploy Deploy the image

docs/filtering.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ The syntax includes:
1111
- Logical boolean operators (case insensitive)
1212
- `and`
1313
- `or`
14-
- String comparison operators
14+
- Comparison operators
1515
- equals `=`
1616
- not equals `!=`
1717
- matches regexp `=~`
1818
- not matches regexp `!~`
19+
- greater than `>`
20+
- less than `<`
1921
- Unary operations
2022
- field is present: `with(field)`
2123
- field is absent: `without(field)`

pkg/dsl/eval_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,43 @@ func TestWithout(t *testing.T) {
103103
})
104104
assert.False(t, result, "Should reject flows from bar")
105105
}
106+
107+
func TestNumeric(t *testing.T) {
108+
predicate, err := Parse(`flowdirection=0 and bytes > 15`)
109+
assert.NoError(t, err)
110+
assert.NotNil(t, predicate)
111+
112+
result := predicate(config.GenericMap{
113+
"srcnamespace": "plop",
114+
"flowdirection": 0,
115+
"bytes": 20,
116+
})
117+
assert.True(t, result)
118+
119+
result = predicate(config.GenericMap{
120+
"srcnamespace": "plop",
121+
"flowdirection": int16(0),
122+
"bytes": int16(20),
123+
})
124+
assert.True(t, result)
125+
126+
result = predicate(config.GenericMap{
127+
"srcnamespace": "plop",
128+
"flowdirection": 1,
129+
"bytes": 20,
130+
})
131+
assert.False(t, result)
132+
133+
result = predicate(config.GenericMap{
134+
"srcnamespace": "plop",
135+
"flowdirection": 0,
136+
"bytes": 10,
137+
})
138+
assert.False(t, result)
139+
140+
result = predicate(config.GenericMap{
141+
"srcnamespace": "plop",
142+
"bytes": 20,
143+
})
144+
assert.False(t, result)
145+
}

pkg/dsl/expr.y

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ package dsl
55
%union{
66
expr Expression
77
value string
8+
intValue int
89
}
910

1011
%type <expr> root
1112
%type <expr> expr
1213

13-
%token <value> NF_FIELD STRING NUMBER AND OR EQ NEQ REG NREG OPEN_PARENTHESIS CLOSE_PARENTHESIS WITH WITHOUT
14+
%token <value> NF_FIELD STRING AND OR EQ NEQ GT LT REG NREG OPEN_PARENTHESIS CLOSE_PARENTHESIS WITH WITHOUT
15+
%token <intValue> NUMBER
1416
%left AND
1517
%left OR
1618
%%
@@ -29,6 +31,10 @@ expr:
2931
| WITHOUT OPEN_PARENTHESIS NF_FIELD CLOSE_PARENTHESIS { $$ = WithoutExpr{key: $3} }
3032
| NF_FIELD EQ STRING { $$ = EqExpr{key: $1, value: $3} }
3133
| NF_FIELD NEQ STRING { $$ = NEqExpr{key: $1, value: $3} }
34+
| NF_FIELD EQ NUMBER { $$ = EqNumExpr{key: $1, value: $3} }
35+
| NF_FIELD NEQ NUMBER { $$ = NEqNumExpr{key: $1, value: $3} }
36+
| NF_FIELD LT NUMBER { $$ = LessThanExpr{key: $1, value: $3} }
37+
| NF_FIELD GT NUMBER { $$ = GreaterThanExpr{key: $1, value: $3} }
3238
| NF_FIELD REG STRING { $$ = RegExpr{key: $1, value: $3} }
3339
| NF_FIELD NREG STRING { $$ = NRegExpr{key: $1, value: $3} }
3440
%%

pkg/dsl/expr.y.go

Lines changed: 76 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)