Skip to content

Commit

Permalink
Handle priting of nils in a JSON compatible manner
Browse files Browse the repository at this point in the history
This removes the <nil> formatting from .String()
  • Loading branch information
tonyhb committed Oct 23, 2024
1 parent d8654ed commit 51cffd2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
14 changes: 14 additions & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

Expand Down
7 changes: 7 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == <nil>`, 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)
}
Expand Down
4 changes: 2 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ func TestParse(t *testing.T) {
tests := []parseTestInput{
{
input: `has(event.name)`,
output: "name select <nil>",
output: "name select null",
expected: ParsedExpression{
Root: Node{
GroupID: newGroupID(1),
Expand All @@ -1079,7 +1079,7 @@ func TestParse(t *testing.T) {
},
{
input: `event.data.num.filter(x, x >= 10)`,
output: "x comprehension <nil>",
output: "x comprehension null",
expected: ParsedExpression{
Root: Node{
GroupID: newGroupID(1),
Expand Down

0 comments on commit 51cffd2

Please sign in to comment.