Skip to content

Commit

Permalink
Adding has filter logic for strings (#758)
Browse files Browse the repository at this point in the history
* Adding contains filter logic for strings

* Updating filter has to be =~
  • Loading branch information
i3149 authored Oct 5, 2024
1 parent 9591bdb commit 423fdd9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pkg/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const (
LessThan = "<"
GreaterThan = ">"
Contains = "%"
Has = "=~"
NotHas = "!~"

String FilterType = "string"
Int = "int"
Expand Down
5 changes: 4 additions & 1 deletion pkg/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func TestFilter(t *testing.T) {
"string,foo,==,bar",
"string,fooAAAA,==,",
"foo,==,bar",
"string,custom_str.foo,=~,",
"string,custom_str.fooAA,!~,",
"string,foo,=~,",
"fooII,==,12",
"src_addr,==,10.2.2.0/24",
"foo,==,no or fooII,==,12",
Expand All @@ -36,7 +39,7 @@ func TestFilter(t *testing.T) {
assert.NoError(err)
assert.Equal(len(filters)+2, len(fs)) // There's an extra and in here.

results := []bool{true, true, true, false, true, true, true, true, false, true, true, true, true, true, true, true, true, true}
results := []bool{true, true, true, false, true, true, true, true, false, true, true, true, true, true, true, true, true, true, true, true, true}
for i, fs := range fs {
assert.Equal(results[i], fs.Filter(kt.InputTesting[0]), "%d", i)
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/filter/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func newStringFilter(log logger.Underlying, fd FilterDef) (*StringFilter, error)
sf.cf = sf.stringNotEquals
case Contains:
sf.cf = sf.stringContains
case Has:
sf.cf = sf.stringHas
case NotHas:
sf.cf = sf.stringNotHas
default:
return nil, fmt.Errorf("Invalid operator for string: %s", fd.Operator)
}
Expand Down Expand Up @@ -96,3 +100,26 @@ func (f *StringFilter) stringContains(chf map[string]interface{}) bool {
}
return false
}

func (f *StringFilter) stringHas(chf map[string]interface{}) bool {
if dd, ok := chf[f.dimension[0]]; ok {
switch dim := dd.(type) {
case string:
return true
case map[string]string:
_, ok := dim[f.dimension[1]]
return ok
}
} else if dd, ok := chf["custom_str"]; ok {
switch dim := dd.(type) {
case map[string]string:
_, ok := dim[f.dimension[0]]
return ok
}
}
return false
}

func (f *StringFilter) stringNotHas(chf map[string]interface{}) bool {
return !f.stringHas(chf)
}

0 comments on commit 423fdd9

Please sign in to comment.