Skip to content

Commit

Permalink
make sure has is also mark as a macro
Browse files Browse the repository at this point in the history
  • Loading branch information
darwin67 committed Sep 27, 2024
1 parent 2dd909a commit 40e415e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
25 changes: 24 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ func (p ParsedExpression) RootGroups() []*Node {
return []*Node{&p.Root}
}

func (p ParsedExpression) Valid() bool {
if p.Root.Predicate == nil && p.Root.GroupID.String() == "" {
return false
}
// TODO: support this in the future
if p.HasMacros {
return false
}

return true
}

// PredicateGroup represents a group of predicates that must all pass in order to execute the
// given expression. For example, this might contain two predicates representing an expression
// with two operators combined with "&&".
Expand Down Expand Up @@ -376,9 +388,20 @@ func navigateAST(nav expr, parent *Node, vars LiftedArgs, rand RandomReader) ([]
stack = stack[1:]

switch item.ast.Kind() {
case celast.SelectKind:
c := item.ast.AsSelect()
child := &Node{
Predicate: &Predicate{
Ident: c.FieldName(),
Operator: "select",
},
}
child.normalize()
result = append(result, child)
hasMacros = true
case celast.ComprehensionKind:
// These are not supported. A comprehension is eg. `.exists` and must
// awlays run naively right now.
// always run naively right now.
c := item.ast.AsComprehension()
child := &Node{
Predicate: &Predicate{
Expand Down
45 changes: 45 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,47 @@ func TestParse(t *testing.T) {

})

// TODO: Should figure how to support it in the future
t.Run("It marks macros and zero value for invalid ones", func(t *testing.T) {
tests := []parseTestInput{
{
input: `has(event.name)`,
output: "name select <nil>",
expected: ParsedExpression{
Root: Node{
GroupID: newGroupID(1),
Predicate: &Predicate{
Ident: "name",
Operator: "select",
},
},
HasMacros: true,
},
},
{
input: `event.name.StartWith("hello")`,
output: "",
expected: ParsedExpression{},
},
{
input: `event.data.num.filter(x, x >= 10)`,
output: "x comprehension <nil>",
expected: ParsedExpression{
Root: Node{
GroupID: newGroupID(1),
Predicate: &Predicate{
Ident: "x",
Operator: "comprehension",
},
},
HasMacros: true,
},
},
}

assert(t, tests)
})

// TODO
/*
t.Run("It deduplicates expressions", func(t *testing.T) {
Expand Down Expand Up @@ -1279,3 +1320,7 @@ func TestRootGroups(t *testing.T) {
})

}

func strPtr(v string) *string {

Check failure on line 1324 in parser_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

func `strPtr` is unused (unused)
return &v
}

0 comments on commit 40e415e

Please sign in to comment.