From 51cffd25efb64d6f424f65773e387ca45ceb26a1 Mon Sep 17 00:00:00 2001 From: Tony Holdstock-Brown Date: Wed, 23 Oct 2024 12:50:02 -0700 Subject: [PATCH] Handle priting of nils in a JSON compatible manner This removes the formatting from .String() --- expr_test.go | 14 ++++++++++++++ parser.go | 7 +++++++ parser_test.go | 4 ++-- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/expr_test.go b/expr_test.go index 1c31c73..9e185c2 100644 --- a/expr_test.go +++ b/expr_test.go @@ -754,6 +754,20 @@ func TestAddRemove(t *testing.T) { require.EqualValues(t, 1, count) require.EqualValues(t, 1, len(eval)) require.NoError(t, err) + + // Matching this expr should now fail. + eval, count, err = e.Evaluate(ctx, map[string]any{ + "event": map[string]any{ + "data": map[string]any{ + "foo": "yea", + "bar": "baz", + }, + }, + }) + + require.EqualValues(t, 1, count) + require.EqualValues(t, 0, len(eval)) + require.NoError(t, err) }) } diff --git a/parser.go b/parser.go index 777e5bb..f8ef615 100644 --- a/parser.go +++ b/parser.go @@ -326,6 +326,13 @@ func (p Predicate) String() string { switch str := p.Literal.(type) { case string: return fmt.Sprintf("%s %s %v", p.Ident, strings.ReplaceAll(p.Operator, "_", ""), strconv.Quote(str)) + case nil: + if p.LiteralIdent == nil { + // print `foo == null` instead of `foo == `, the Golang default. + // We onyl do this if we're not comparing to an identifier. + return fmt.Sprintf("%s %s null", p.Ident, strings.ReplaceAll(p.Operator, "_", "")) + } + return fmt.Sprintf("%s %s %v", p.Ident, strings.ReplaceAll(p.Operator, "_", ""), lit) default: return fmt.Sprintf("%s %s %v", p.Ident, strings.ReplaceAll(p.Operator, "_", ""), lit) } diff --git a/parser_test.go b/parser_test.go index 52b327c..77534aa 100644 --- a/parser_test.go +++ b/parser_test.go @@ -1060,7 +1060,7 @@ func TestParse(t *testing.T) { tests := []parseTestInput{ { input: `has(event.name)`, - output: "name select ", + output: "name select null", expected: ParsedExpression{ Root: Node{ GroupID: newGroupID(1), @@ -1079,7 +1079,7 @@ func TestParse(t *testing.T) { }, { input: `event.data.num.filter(x, x >= 10)`, - output: "x comprehension ", + output: "x comprehension null", expected: ParsedExpression{ Root: Node{ GroupID: newGroupID(1),