Skip to content

Commit

Permalink
Merge pull request #95 from pinpt/filter_stringify
Browse files Browse the repository at this point in the history
add string to filter so we can debug
  • Loading branch information
Jeff Haynie authored Jun 22, 2020
2 parents 25d977d + 81e0e6d commit 9de4dc7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions filterexpr/filterexpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
type Filter interface {
// Test will return true if the filter expression matches the map
Test(kv map[string]interface{}) bool

// String will return a string representation
String() string
}

type filter struct {
Expand All @@ -23,6 +26,10 @@ func (f *filter) Test(kv map[string]interface{}) bool {
return f.expr.Test(kv)
}

func (f *filter) String() string {
return f.expr.String()
}

// Compile will compile the filter expression as a string in Filter which can be saved and invoked and is thread safe
func Compile(expr string) (Filter, error) {
object, err := ParseReader("", strings.NewReader(expr), MaxExpressions(150000))
Expand Down
7 changes: 7 additions & 0 deletions filterexpr/filterexpr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,10 @@ func TestABigAsExpression(t *testing.T) {
assert.NoError(err)
assert.True(filter.Test(map[string]interface{}{"model": "activityfeed.Feed", "user_id": "40bfb0d341249a58"}))
}

func TestStringify(t *testing.T) {
assert := assert.New(t)
filter, err := Compile(`user-id:"a"`)
assert.NoError(err)
assert.Equal("ExpressionGroup[[Expression[Node[user-id=a],,<nil>]]]", filter.String())
}

0 comments on commit 9de4dc7

Please sign in to comment.