Skip to content

Commit

Permalink
Removing panics from operations
Browse files Browse the repository at this point in the history
  • Loading branch information
nikunjy committed May 3, 2019
1 parent b238563 commit a92cf80
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 154 deletions.
40 changes: 20 additions & 20 deletions parser/bool_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,46 @@ func (o *BoolOperation) get(left Operand, right Operand) (bool, bool, bool) {
return leftVal, rightVal, (ok && ok1)
}

func (o *BoolOperation) EQ(left Operand, right Operand) bool {
func (o *BoolOperation) EQ(left Operand, right Operand) (bool, error) {
l, r, ok := o.get(left, right)
if !ok {
return ok
return ok, nil
}
return l == r
return l == r, nil
}

func (o *BoolOperation) NE(left Operand, right Operand) bool {
func (o *BoolOperation) NE(left Operand, right Operand) (bool, error) {
l, r, ok := o.get(left, right)
if !ok {
return ok
return ok, nil
}
return l != r
return l != r, nil
}

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

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

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

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

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

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

func (o *BoolOperation) EW(left Operand, right Operand) bool {
panic("not supported")
func (o *BoolOperation) EW(left Operand, right Operand) (bool, error) {
return false, ErrInvalidOperation
}
46 changes: 23 additions & 23 deletions parser/float_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,67 +26,67 @@ func (o *FloatOperation) get(left Operand, right Operand) (float64, float64, boo

}

func (o *FloatOperation) EQ(left Operand, right Operand) bool {
func (o *FloatOperation) EQ(left Operand, right Operand) (bool, error) {
l, r, ok := o.get(left, right)
if !ok {
return ok
return ok, nil
}
return l == r
return l == r, nil
}

func (o *FloatOperation) NE(left Operand, right Operand) bool {
func (o *FloatOperation) NE(left Operand, right Operand) (bool, error) {
l, r, ok := o.get(left, right)
if !ok {
return ok
return ok, nil
}
return l != r
return l != r, nil
}

func (o *FloatOperation) GT(left Operand, right Operand) bool {
func (o *FloatOperation) GT(left Operand, right Operand) (bool, error) {
l, r, ok := o.get(left, right)
if !ok {
return ok
return ok, nil
}
return l > r
return l > r, nil
}

func (o *FloatOperation) LT(left Operand, right Operand) bool {
func (o *FloatOperation) LT(left Operand, right Operand) (bool, error) {
l, r, ok := o.get(left, right)
if !ok {
return ok
return ok, nil
}
return l < r
return l < r, nil
}

func (o *FloatOperation) GE(left Operand, right Operand) bool {
func (o *FloatOperation) GE(left Operand, right Operand) (bool, error) {
l, r, ok := o.get(left, right)
if !ok {
return ok
return ok, nil
}
return l >= r
return l >= r, nil
}

func (o *FloatOperation) LE(left Operand, right Operand) bool {
func (o *FloatOperation) LE(left Operand, right Operand) (bool, error) {
l, r, ok := o.get(left, right)
if !ok {
return ok
return ok, nil
}
return l <= r
return l <= r, nil
}

func (o *FloatOperation) IN(left Operand, right Operand) bool {
func (o *FloatOperation) IN(left Operand, right Operand) (bool, error) {
leftVal, ok := toNum(left)
if !ok {
return ok
return ok, nil
}
rightVal, ok := right.([]float64)
if !ok {
return ok
return ok, nil
}
for _, num := range rightVal {
if num == leftVal {
return true
return true, nil
}
}
return false
return false, nil
}
58 changes: 29 additions & 29 deletions parser/int_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,79 +13,79 @@ func (o *IntOperation) get(left Operand, right Operand) (int, int, bool) {

}

func (o *IntOperation) EQ(left Operand, right Operand) bool {
func (o *IntOperation) EQ(left Operand, right Operand) (bool, error) {
l, r, ok := o.get(left, right)
if !ok {
return ok
return ok, nil
}
return l == r
return l == r, nil
}

func (o *IntOperation) NE(left Operand, right Operand) bool {
func (o *IntOperation) NE(left Operand, right Operand) (bool, error) {
l, r, ok := o.get(left, right)
if !ok {
return ok
return ok, nil
}
return l != r
return l != r, nil
}

func (o *IntOperation) GT(left Operand, right Operand) bool {
func (o *IntOperation) GT(left Operand, right Operand) (bool, error) {
l, r, ok := o.get(left, right)
if !ok {
return ok
return ok, nil
}
return l > r
return l > r, nil
}

func (o *IntOperation) LT(left Operand, right Operand) bool {
func (o *IntOperation) LT(left Operand, right Operand) (bool, error) {
l, r, ok := o.get(left, right)
if !ok {
return ok
return ok, nil
}
return l < r
return l < r, nil
}

func (o *IntOperation) GE(left Operand, right Operand) bool {
func (o *IntOperation) GE(left Operand, right Operand) (bool, error) {
l, r, ok := o.get(left, right)
if !ok {
return ok
return ok, nil
}
return l >= r
return l >= r, nil
}

func (o *IntOperation) LE(left Operand, right Operand) bool {
func (o *IntOperation) LE(left Operand, right Operand) (bool, error) {
l, r, ok := o.get(left, right)
if !ok {
return ok
return ok, nil
}
return l <= r
return l <= r, nil
}

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

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

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

func (o *IntOperation) IN(left Operand, right Operand) bool {
func (o *IntOperation) IN(left Operand, right Operand) (bool, error) {
leftVal, ok := left.(int)
if !ok {
return ok
return ok, nil
}
rightVal, ok := right.([]int)
if !ok {
return ok
return ok, nil
}
for _, num := range rightVal {
if num == leftVal {
return true
return true, nil
}
}
return false
return false, nil
}
8 changes: 6 additions & 2 deletions parser/jsonquery_visitor_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (j *JsonQueryVisitorImpl) VisitPresentExp(ctx *PresentExpContext) interface
func (j *JsonQueryVisitorImpl) VisitCompareExp(ctx *CompareExpContext) interface{} {
ctx.AttrPath().Accept(j)
ctx.Value().Accept(j)
var apply func(Operand, Operand) bool
var apply func(Operand, Operand) (bool, error)
currentOp := j.currentOperation
switch ctx.op.GetTokenType() {
case JsonQueryParserEQ:
Expand All @@ -128,7 +128,11 @@ func (j *JsonQueryVisitorImpl) VisitCompareExp(ctx *CompareExpContext) interface
panic("unknown operation")
}
defer func() { j.rightOp = nil }()
return apply(j.leftOp, j.rightOp)
ret, err := apply(j.leftOp, j.rightOp)
if err != nil {
panic(err)
}
return ret
}

func (j *JsonQueryVisitorImpl) VisitAttrPath(ctx *AttrPathContext) interface{} {
Expand Down
40 changes: 20 additions & 20 deletions parser/null_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,42 @@ package parser
type NullOperation struct {
}

func (o *NullOperation) EQ(left Operand, right Operand) bool {
return left == nil
func (o *NullOperation) EQ(left Operand, right Operand) (bool, error) {
return left == nil, nil
}

func (o *NullOperation) NE(left Operand, right Operand) bool {
return left != nil
func (o *NullOperation) NE(left Operand, right Operand) (bool, error) {
return left != nil, nil
}

func (o *NullOperation) GT(left Operand, right Operand) bool {
panic("not supported")
func (o *NullOperation) GT(left Operand, right Operand) (bool, error) {
return false, ErrInvalidOperation
}

func (o *NullOperation) LT(left Operand, right Operand) bool {
panic("not supported")
func (o *NullOperation) LT(left Operand, right Operand) (bool, error) {
return false, ErrInvalidOperation
}

func (o *NullOperation) GE(left Operand, right Operand) bool {
panic("not supported")
func (o *NullOperation) GE(left Operand, right Operand) (bool, error) {
return false, ErrInvalidOperation
}

func (o *NullOperation) LE(left Operand, right Operand) bool {
panic("not supported")
func (o *NullOperation) LE(left Operand, right Operand) (bool, error) {
return false, ErrInvalidOperation
}

func (o *NullOperation) CO(left Operand, right Operand) bool {
panic("not supported")
func (o *NullOperation) CO(left Operand, right Operand) (bool, error) {
return false, ErrInvalidOperation
}

func (o *NullOperation) SW(left Operand, right Operand) bool {
panic("not supported")
func (o *NullOperation) SW(left Operand, right Operand) (bool, error) {
return false, ErrInvalidOperation
}

func (o *NullOperation) EW(left Operand, right Operand) bool {
panic("not supported")
func (o *NullOperation) EW(left Operand, right Operand) (bool, error) {
return false, ErrInvalidOperation
}

func (o *NullOperation) IN(left Operand, right Operand) bool {
panic("not supported")
func (o *NullOperation) IN(left Operand, right Operand) (bool, error) {
return false, ErrInvalidOperation
}
26 changes: 16 additions & 10 deletions parser/operation.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package parser

import "errors"

type Operand interface{}

var (
ErrInvalidOperation = errors.New("Invalid operation on the type")
)

type Operation interface {
EQ(left Operand, right Operand) bool
NE(left Operand, right Operand) bool
GT(left Operand, right Operand) bool
LT(left Operand, right Operand) bool
GE(left Operand, right Operand) bool
LE(left Operand, right Operand) bool
CO(left Operand, right Operand) bool
SW(left Operand, right Operand) bool
EW(left Operand, right Operand) bool
IN(left Operand, right Operand) bool
EQ(left Operand, right Operand) (bool, error)
NE(left Operand, right Operand) (bool, error)
GT(left Operand, right Operand) (bool, error)
LT(left Operand, right Operand) (bool, error)
GE(left Operand, right Operand) (bool, error)
LE(left Operand, right Operand) (bool, error)
CO(left Operand, right Operand) (bool, error)
SW(left Operand, right Operand) (bool, error)
EW(left Operand, right Operand) (bool, error)
IN(left Operand, right Operand) (bool, error)
}
Loading

0 comments on commit a92cf80

Please sign in to comment.