Skip to content

Commit

Permalink
Add some operators and fix IS NOT MISSING
Browse files Browse the repository at this point in the history
  • Loading branch information
nelio2k committed Apr 4, 2019
1 parent c0fbc1c commit 542cb7a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
17 changes: 12 additions & 5 deletions filterExprParser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// BooleanExpr = Boolean | BooleanFuncExpr
// LHS = ConstFuncExpr | Boolean | Field | Value
// RHS = ConstFuncExpr | Boolean | Value | Field
// CompareOp = "=" | "<>" | ">" | ">=" | "<" | "<="
// CompareOp = "=" | "==" | "<>" | "!=" | ">" | ">=" | "<" | "<="
// CheckOp = ( "IS" [ "NOT" ] ( NULL | MISSING ) )
// Field = { @"-" } OnePath { "." OnePath } { MathOp MathValue }
// OnePath = ( PathFuncExpression | StringType ){ ArrayIndex }
Expand Down Expand Up @@ -776,7 +776,8 @@ func (f *FEValue) OutputExpression() (Expression, error) {
// and go to the other type of operands

type FEOpChar struct {
Equal *bool `( @"=" |`
Not *bool `( @"!" |`
Equal *bool `@"=" |`
LessThan *bool `@"<" |`
GreaterThan *bool `@">" )`
}
Expand All @@ -788,12 +789,18 @@ type FECompareOp struct {

func (feo *FECompareOp) IsEqual() bool {
// =
return feo.OpChars0 != nil && feo.OpChars0.Equal != nil && feo.OpChars1 == nil
singleEq := feo.OpChars0 != nil && feo.OpChars0.Equal != nil && feo.OpChars1 == nil
// ==
doubleEq := feo.OpChars0 != nil && feo.OpChars0.Equal != nil && feo.OpChars1 != nil && feo.OpChars1.Equal != nil
return singleEq || doubleEq
}

func (feo *FECompareOp) IsNotEqual() bool {
// !=
notEqual0 := feo.OpChars0 != nil && feo.OpChars0.Not != nil && feo.OpChars1 != nil && feo.OpChars1.Equal != nil
// <>
return feo.OpChars0 != nil && feo.OpChars0.LessThan != nil && feo.OpChars1 != nil && feo.OpChars1.GreaterThan != nil
notEqual1 := feo.OpChars0 != nil && feo.OpChars0.LessThan != nil && feo.OpChars1 != nil && feo.OpChars1.GreaterThan != nil
return notEqual0 || notEqual1
}

func (feo *FECompareOp) IsGreaterThan() bool {
Expand Down Expand Up @@ -887,7 +894,7 @@ func (feco *FECheckOp) isMissingInternal() bool {
}

func (feco *FECheckOp) IsNotMissing() bool {
return feco.isNot() && feco.IsMissing()
return feco.isNot() && feco.isMissingInternal()
}

func (feco *FECheckOp) IsNull() bool {
Expand Down
47 changes: 47 additions & 0 deletions filterExprParser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ func TestFilterExpressionParser(t *testing.T) {
assert.Equal("path", fe.AndConditions[0].OrConditions[0].Operand.LHS.Field.Path[1].String())
assert.True(fe.AndConditions[0].OrConditions[0].Operand.Op.IsEqual())
assert.Equal("value", fe.AndConditions[0].OrConditions[0].Operand.RHS.Value.String())
// Test double equal is the same as single eq
err = parser.ParseString("fieldpath.path == \"value\"", fe)
assert.Nil(err)
assert.True(fe.AndConditions[0].OrConditions[0].Operand.Op.IsEqual())
expr, err = fe.OutputExpression()
assert.Nil(err)
matchDef = trans.Transform([]Expression{expr})
Expand Down Expand Up @@ -421,6 +425,45 @@ func TestFilterExpressionParser(t *testing.T) {
udMarsh, _ = json.Marshal(userData)
match, err = m.Match(udMarsh)
assert.True(match)
// Use same data as above, test IS NOT NULL
err = parser.ParseString("fieldpath.path IS NOT NULL AND fieldpath.path IS NOT MISSING", fe)
assert.Nil(err)
expr, err = fe.OutputExpression()
assert.Nil(err)
matchDef = trans.Transform([]Expression{expr})
assert.NotNil(matchDef)
m = NewFastMatcher(matchDef)
match, err = m.Match(udMarsh)
assert.True(match)

// Test IS NULL
fe = &FilterExpression{}
err = parser.ParseString("fieldpath.path IS NULL", fe)
assert.Nil(err)
expr, err = fe.OutputExpression()
assert.Nil(err)
matchDef = trans.Transform([]Expression{expr})
assert.NotNil(matchDef)
m = NewFastMatcher(matchDef)
userData = map[string]interface{}{
"fieldpath": map[string]interface{}{
"path": nil,
},
}
udMarsh, _ = json.Marshal(userData)
match, err = m.Match(udMarsh)
assert.True(match)
// Use above data, test IS MISSING
fe = &FilterExpression{}
err = parser.ParseString("fieldpath.path2 IS MISSING", fe)
assert.Nil(err)
expr, err = fe.OutputExpression()
assert.Nil(err)
matchDef = trans.Transform([]Expression{expr})
assert.NotNil(matchDef)
m = NewFastMatcher(matchDef)
match, err = m.Match(udMarsh)
assert.True(match)

fe = &FilterExpression{}
err = parser.ParseString("fieldpath.path <> POW(ABS(CEIL(PI())),2)", fe)
Expand All @@ -430,6 +473,10 @@ func TestFilterExpressionParser(t *testing.T) {
assert.True(fe.AndConditions[0].OrConditions[0].Operand.Op.IsNotEqual())
assert.Equal("POW", fe.AndConditions[0].OrConditions[0].Operand.RHS.Func.ConstFuncTwoArgs.ConstFuncTwoArgsName.String())
assert.Equal("ABS", fe.AndConditions[0].OrConditions[0].Operand.RHS.Func.ConstFuncTwoArgs.Argument0.SubFunc.ConstFuncOneArg.ConstFuncOneArgName.String())
// Test second not equals
err = parser.ParseString("fieldpath.path != POW(ABS(CEIL(PI())),2)", fe)
assert.Nil(err)
assert.True(fe.AndConditions[0].OrConditions[0].Operand.Op.IsNotEqual())

fe = &FilterExpression{}
err = parser.ParseString("REGEXP_CONTAINS(`[$%XDCRInternalKey*%$]`, \"^xyz*\")", fe)
Expand Down

0 comments on commit 542cb7a

Please sign in to comment.