Skip to content

Commit

Permalink
Fixes #106:
Browse files Browse the repository at this point in the history
  Checking parenthesis for subExpression during outputExpression leads to
  only counting the subExpression parenthesis as the final tally, and
  causes correct parenthesis cases to be incorrect as the prev counts are lost
  • Loading branch information
nelio2k committed May 30, 2019
1 parent ee8f149 commit 9f6a955
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
18 changes: 11 additions & 7 deletions filterExprParser.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,9 @@ func (fe *FilterExpression) String() string {
return strings.Join(output, " ")
}

// Outputs the head of the Expression match tree of which represents everything underneath
func (f *FilterExpression) OutputExpression() (Expression, error) {
func (f *FilterExpression) outputExpressionNoParenCheck() (Expression, error) {
var outExpr OrExpr

if f.GetTotalOpenParens() != f.GetTotalCloseParens() {
return outExpr, ErrorMalformedParenthesis
}

for _, oneExpr := range f.AndConditions {
andExpr, err := oneExpr.OutputExpression()
if err != nil {
Expand All @@ -123,7 +118,7 @@ func (f *FilterExpression) OutputExpression() (Expression, error) {
combinedExpr = append(combinedExpr, outExpr)

for _, subFilterExpr := range f.SubFilterExpr {
subExpr, err := subFilterExpr.OutputExpression()
subExpr, err := subFilterExpr.outputExpressionNoParenCheck()
if err != nil {
return combinedExpr, err
}
Expand All @@ -136,6 +131,15 @@ func (f *FilterExpression) OutputExpression() (Expression, error) {
}
}

// Outputs the head of the Expression match tree of which represents everything underneath
func (f *FilterExpression) OutputExpression() (Expression, error) {
if f.GetTotalOpenParens() != f.GetTotalCloseParens() {
return nil, ErrorMalformedParenthesis
}

return f.outputExpressionNoParenCheck()
}

type FEOpenParen struct {
Parens string `@"("`
}
Expand Down
13 changes: 12 additions & 1 deletion filterExprParser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ func TestFilterExpressionParser(t *testing.T) {
expr, err = fe.OutputExpression()
assert.Nil(err)

fe = &FilterExpression{}
err = parser.ParseString("((TRUE OR FALSE) AND (FALSE OR TRUE)) OR (TRUE AND TRUE)", fe)
assert.Nil(err)
expr, err = fe.OutputExpression()
assert.Nil(err)

fe = &FilterExpression{}
err = parser.ParseString("NOT NOT NOT TRUE", fe)
assert.Nil(err)
Expand Down Expand Up @@ -745,7 +751,7 @@ func TestFilterExpressionParser(t *testing.T) {
_, err = fe.OutputExpression()
assert.NotNil(err)

// For invalid date values, it will always return -1
// For invalid date values, it will be nil and should be smaller than any other valids
fe = &FilterExpression{}
err = parser.ParseString("DATE(fieldpath.path) < DATE(\"2019-01-01\")", fe)
expr, err = fe.OutputExpression()
Expand All @@ -761,4 +767,9 @@ func TestFilterExpressionParser(t *testing.T) {
udMarsh, _ = json.Marshal(userData)
match, err = m.Match(udMarsh)
assert.True(match)

// Invalid parenthesis
_, fe, err = NewFilterExpressionParser("((TRUE OR FALSE () AND TRUE))")
_, err = fe.OutputExpression()
assert.NotNil(err)
}

0 comments on commit 9f6a955

Please sign in to comment.