Skip to content

Commit

Permalink
Adding some coverage and badge
Browse files Browse the repository at this point in the history
  • Loading branch information
nikunjy committed May 3, 2019
1 parent 74a0b59 commit 4e0219b
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 47 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Golang Rules Engine [![Build Status][ci-img]][ci]
# Golang Rules Engine [![Build Status][ci-img]][ci] [![codecov](https://codecov.io/gh/nikunjy/rules/branch/master/graph/badge.svg)](https://codecov.io/gh/nikunjy/rules)
Rules engine written in golang with the help of antlr.

This package will be very helpful in situations where you have a generic rule and want to verify if your values (specified using `map[string]interface{}`) satisfy the rule.
Expand Down
28 changes: 0 additions & 28 deletions parser/bool_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,3 @@ func (o *BoolOperation) NE(left Operand, right Operand) (bool, error) {
}
return l != r, nil
}

func (o *BoolOperation) GT(left Operand, right Operand) (bool, error) {
return false, ErrInvalidOperation
}

func (o *BoolOperation) LT(left Operand, right Operand) (bool, error) {
return false, ErrInvalidOperation
}

func (o *BoolOperation) GE(left Operand, right Operand) (bool, error) {
return false, ErrInvalidOperation
}

func (o *BoolOperation) LE(left Operand, right Operand) (bool, error) {
return false, ErrInvalidOperation
}

func (o *BoolOperation) CO(left Operand, right Operand) (bool, error) {
return false, ErrInvalidOperation
}

func (o *BoolOperation) SW(left Operand, right Operand) (bool, error) {
return false, ErrInvalidOperation
}

func (o *BoolOperation) EW(left Operand, right Operand) (bool, error) {
return false, ErrInvalidOperation
}
13 changes: 1 addition & 12 deletions parser/int_operation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parser

type IntOperation struct {
NullOperation
}

func (o *IntOperation) get(left Operand, right Operand) (int, int, error) {
Expand Down Expand Up @@ -67,18 +68,6 @@ func (o *IntOperation) LE(left Operand, right Operand) (bool, error) {
return l <= r, nil
}

func (o *IntOperation) CO(left Operand, right Operand) (bool, error) {
return false, ErrInvalidOperation
}

func (o *IntOperation) SW(left Operand, right Operand) (bool, error) {
return false, ErrInvalidOperation
}

func (o *IntOperation) EW(left Operand, right Operand) (bool, error) {
return false, ErrInvalidOperation
}

func (o *IntOperation) IN(left Operand, right Operand) (bool, error) {
leftVal, ok := left.(int)
if !ok {
Expand Down
5 changes: 5 additions & 0 deletions parser/jsonquery_visitor_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ func (j *JsonQueryVisitorImpl) VisitCompareExp(ctx *CompareExpContext) interface
if err != nil {
switch err {
case ErrInvalidOperation:
// in case of invalid operation lets rather
// be conservative and return false because the rule doesn't even make
// sense. It can be argued that it would be false positive if we were
// to return true
j.setErr(err)
j.setDebugErr(
newDebugError(err, "Not a valid operation for datatypes").Set(ErrVals{
"operation": ctx.op.GetTokenType(),
Expand Down
7 changes: 7 additions & 0 deletions parser/parse_logical_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestLogicalExpWithAnd(t *testing.T) {
"y": 2,
},
true,
false,
},
{
`x eq 1 and y gt 1`,
Expand All @@ -24,6 +25,7 @@ func TestLogicalExpWithAnd(t *testing.T) {
"y": 1,
},
false,
false,
},
{
`x eq 1 and not (y gt 1)`,
Expand All @@ -32,6 +34,7 @@ func TestLogicalExpWithAnd(t *testing.T) {
"y": 1,
},
true,
false,
},
{
`x eq 1 and not (y gt 1)`,
Expand All @@ -40,6 +43,7 @@ func TestLogicalExpWithAnd(t *testing.T) {
"y": 2,
},
false,
false,
},
{
`(x eq 1 and y gt 1) and z eq 3`,
Expand All @@ -49,6 +53,7 @@ func TestLogicalExpWithAnd(t *testing.T) {
"z": 3,
},
true,
false,
},
{
`(x eq 1 and y gt 1) and z eq 3 or a gt 4`,
Expand All @@ -58,6 +63,7 @@ func TestLogicalExpWithAnd(t *testing.T) {
"a": 5,
},
true,
false,
},
{
`(x eq 1 and y gt 1) and (z eq 3 or a gt 4)`,
Expand All @@ -67,6 +73,7 @@ func TestLogicalExpWithAnd(t *testing.T) {
"a": 5,
},
true,
false,
},
}

Expand Down
Loading

0 comments on commit 4e0219b

Please sign in to comment.