Skip to content

Commit

Permalink
Merge pull request #3 from nikunjy/coverage_increase
Browse files Browse the repository at this point in the history
Attempts to increase coverage to 75-85%
  • Loading branch information
nikunjy authored May 9, 2019
2 parents fa25b92 + 578b4f5 commit c2f6bc1
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 99 deletions.
8 changes: 1 addition & 7 deletions parser/float_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ func toNum(op Operand) (float64, error) {
return float64(val), nil
case float64:
return val, nil
case int64:
return float64(val), nil
}
var exp float64
return 0, newErrInvalidOperand(op, exp)
Expand All @@ -26,11 +24,7 @@ func (o *FloatOperation) get(left Operand, right Operand) (float64, float64, err
return 0, 0, err
}
rightVal, err := toNum(right)
if err != nil {
return 0, 0, err
}
return leftVal, rightVal, nil

return leftVal, rightVal, err
}

func (o *FloatOperation) EQ(left Operand, right Operand) (bool, error) {
Expand Down
99 changes: 23 additions & 76 deletions parser/jsonquery_visitor_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func (j *JsonQueryVisitorImpl) hasErr() bool {
}

func (j *JsonQueryVisitorImpl) Visit(tree antlr.ParseTree) interface{} {
if j.hasErr() {
return false
}
switch val := tree.(type) {
case *LogicalExpContext:
return val.Accept(j).(bool)
Expand All @@ -79,16 +82,12 @@ func (j *JsonQueryVisitorImpl) Visit(tree antlr.ParseTree) interface{} {
case *PresentExpContext:
return val.Accept(j).(bool)
default:
fmt.Println("Came here ", tree.GetText())
j.setErr(errors.New("Invalid rule"))
return false
}
}

func (j *JsonQueryVisitorImpl) VisitParenExp(ctx *ParenExpContext) interface{} {
if j.hasErr() {
return false
}
result := ctx.Query().Accept(j).(bool)
if ctx.NOT() != nil {
return !result
Expand All @@ -97,9 +96,6 @@ func (j *JsonQueryVisitorImpl) VisitParenExp(ctx *ParenExpContext) interface{} {
}

func (j *JsonQueryVisitorImpl) VisitLogicalExp(ctx *LogicalExpContext) interface{} {
if j.hasErr() {
return false
}
left := ctx.Query(0).Accept(j).(bool)
op := ctx.LOGICAL_OPERATOR().GetText()
if op == "or" {
Expand All @@ -116,19 +112,16 @@ func (j *JsonQueryVisitorImpl) VisitLogicalExp(ctx *LogicalExpContext) interface
}

func (j *JsonQueryVisitorImpl) VisitPresentExp(ctx *PresentExpContext) interface{} {
if j.hasErr() {
return false
}
ctx.AttrPath().Accept(j)
return j.leftOp != nil
}

func (j *JsonQueryVisitorImpl) VisitCompareExp(ctx *CompareExpContext) interface{} {
ctx.AttrPath().Accept(j)
ctx.Value().Accept(j)
if j.hasErr() {
return false
}
ctx.AttrPath().Accept(j)
ctx.Value().Accept(j)
var apply func(Operand, Operand) (bool, error)
currentOp := j.currentOperation
switch ctx.op.GetTokenType() {
Expand Down Expand Up @@ -167,31 +160,31 @@ func (j *JsonQueryVisitorImpl) VisitCompareExp(ctx *CompareExpContext) interface
// to return true
j.setErr(err)
j.setDebugErr(
newDebugError(err, "Not a valid operation for datatypes").Set(ErrVals{
newNestedError(err, "Not a valid operation for datatypes").Set(ErrVals{
"operation": ctx.op.GetTokenType(),
"object_path_operand": j.leftOp,
"rule_operand": j.rightOp,
}),
)
case ErrEvalOperandMissing:
j.setDebugErr(
newDebugError(err, "Eval operand missing in input object").Set(ErrVals{
newNestedError(err, "Eval operand missing in input object").Set(ErrVals{
"attr_path": ctx.AttrPath().GetText(),
}),
)
default:
switch err.(type) {
case *ErrInvalidOperand:
j.setDebugErr(
newDebugError(err, "operands are not the right value type").Set(ErrVals{
newNestedError(err, "operands are not the right value type").Set(ErrVals{
"attr_path": ctx.AttrPath().GetText(),
"object_path_operand": j.leftOp,
"rule_operand": j.rightOp,
}),
)
default:
j.setDebugErr(
newDebugError(err, "unknown error").Set(ErrVals{
newNestedError(err, "unknown error").Set(ErrVals{
"attr_path": ctx.AttrPath().GetText(),
"object_path_operand": j.leftOp,
"rule_operand": j.rightOp,
Expand All @@ -207,9 +200,6 @@ func (j *JsonQueryVisitorImpl) VisitCompareExp(ctx *CompareExpContext) interface
}

func (j *JsonQueryVisitorImpl) VisitAttrPath(ctx *AttrPathContext) interface{} {
if j.hasErr() {
return false
}
var item interface{}
if ctx.SubAttr() == nil || ctx.SubAttr().IsEmpty() {
if !j.stack.empty() {
Expand All @@ -218,76 +208,63 @@ func (j *JsonQueryVisitorImpl) VisitAttrPath(ctx *AttrPathContext) interface{} {
item = j.item
}
if item == nil {
return false
return nil
}
m := item.(map[string]interface{})
j.leftOp = m[ctx.ATTRNAME().GetText()]
j.stack.clear()
return true
return nil
}
if !j.stack.empty() {
item = j.stack.peek()
} else {
item = j.item
}
if item == nil {
return false
return nil
}
m := item.(map[string]interface{})
j.stack.push(m[ctx.ATTRNAME().GetText()])
return ctx.SubAttr().Accept(j).(bool)
return ctx.SubAttr().Accept(j)
}

func (j *JsonQueryVisitorImpl) VisitSubAttr(ctx *SubAttrContext) interface{} {
if j.hasErr() {
return false
}
return ctx.AttrPath().Accept(j).(bool)
return ctx.AttrPath().Accept(j)
}

func (j *JsonQueryVisitorImpl) VisitBoolean(ctx *BooleanContext) interface{} {
if j.hasErr() {
return false
}
j.currentOperation = &BoolOperation{}

val, err := strconv.ParseBool(ctx.GetText())
if err != nil {
j.rightOp = nil
j.setErr(err)
return false
j.setErr(newNestedError(err, "Error converting boolean"))
return nil
}
j.rightOp = val
return true
return nil
}

func (j *JsonQueryVisitorImpl) VisitNull(ctx *NullContext) interface{} {
if j.hasErr() {
return false
}
j.currentOperation = &NullOperation{}
j.rightOp = nil
return true
return nil
}

func getString(s string) string {
if len(s) > 2 {
return s[1 : len(s)-1]
}

return ""
}

func (j *JsonQueryVisitorImpl) VisitString(ctx *StringContext) interface{} {
j.currentOperation = &StringOperation{}
j.rightOp = getString(ctx.GetText())
return true
return nil
}

func (j *JsonQueryVisitorImpl) VisitDouble(ctx *DoubleContext) interface{} {
if j.hasErr() {
return false
}
j.currentOperation = &FloatOperation{}
val, err := strconv.ParseFloat(ctx.GetText(), 10)
if err != nil {
Expand All @@ -296,52 +273,37 @@ func (j *JsonQueryVisitorImpl) VisitDouble(ctx *DoubleContext) interface{} {
return false
}
j.rightOp = val
return true
return nil
}

func (j *JsonQueryVisitorImpl) VisitVersion(ctx *VersionContext) interface{} {
if j.hasErr() {
return false
}
j.currentOperation = &VersionOperation{}
j.rightOp = ctx.VERSION().GetText()
return true
return nil
}

func (j *JsonQueryVisitorImpl) VisitLong(ctx *LongContext) interface{} {
if j.hasErr() {
return false
}
j.currentOperation = &IntOperation{}
val, err := strconv.ParseInt(ctx.GetText(), 10, 64)
if err != nil {
j.rightOp = nil
j.setErr(err)
return false
return nil
}
j.rightOp = int(val)
return true
return nil
}

func (j *JsonQueryVisitorImpl) VisitListOfInts(ctx *ListOfIntsContext) interface{} {
if j.hasErr() {
return false
}
j.currentOperation = &IntOperation{}
return ctx.ListInts().Accept(j)
}

func (j *JsonQueryVisitorImpl) VisitListInts(ctx *ListIntsContext) interface{} {
if j.hasErr() {
return nil
}
return ctx.SubListOfInts().Accept(j)
}

func (j *JsonQueryVisitorImpl) VisitSubListOfInts(ctx *SubListOfIntsContext) interface{} {
if j.hasErr() {
return nil
}
if j.rightOp == nil {
j.rightOp = make([]int, 0)
}
Expand All @@ -359,9 +321,6 @@ func (j *JsonQueryVisitorImpl) VisitSubListOfInts(ctx *SubListOfIntsContext) int
}

func (j *JsonQueryVisitorImpl) VisitListOfDoubles(ctx *ListOfDoublesContext) interface{} {
if j.hasErr() {
return nil
}
j.currentOperation = &FloatOperation{}
return ctx.ListDoubles().Accept(j)
}
Expand All @@ -371,9 +330,6 @@ func (j *JsonQueryVisitorImpl) VisitListDoubles(ctx *ListDoublesContext) interfa
}

func (j *JsonQueryVisitorImpl) VisitSubListOfDoubles(ctx *SubListOfDoublesContext) interface{} {
if j.hasErr() {
return nil
}
if j.rightOp == nil {
j.rightOp = make([]float64, 0)
}
Expand All @@ -391,24 +347,15 @@ func (j *JsonQueryVisitorImpl) VisitSubListOfDoubles(ctx *SubListOfDoublesContex
}

func (j *JsonQueryVisitorImpl) VisitListOfStrings(ctx *ListOfStringsContext) interface{} {
if j.hasErr() {
return nil
}
j.currentOperation = &StringOperation{}
return ctx.ListStrings().Accept(j)
}

func (j *JsonQueryVisitorImpl) VisitListStrings(ctx *ListStringsContext) interface{} {
if j.hasErr() {
return nil
}
return ctx.SubListOfStrings().Accept(j)
}

func (j *JsonQueryVisitorImpl) VisitSubListOfStrings(ctx *SubListOfStringsContext) interface{} {
if j.hasErr() {
return nil
}
if j.rightOp == nil {
j.rightOp = make([]string, 0)
}
Expand Down
14 changes: 7 additions & 7 deletions parser/nester_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ func (e ErrVals) Merge(vals ErrVals) {
}
}

type DebugError struct {
type NestedError struct {
Err error
Msg string
Vals ErrVals
}

func (e *DebugError) Original() error {
func (e *NestedError) Original() error {
switch val := e.Err.(type) {
case *DebugError:
case *NestedError:
return val.Original()
default:
return e.Err
}
}

func (e *DebugError) Error() string {
func (e *NestedError) Error() string {
if e.Vals == nil {
e.Vals = make(map[string]interface{})
}
Expand All @@ -49,16 +49,16 @@ func (e *DebugError) Error() string {
return string(data)
}

func (e *DebugError) Set(vals ErrVals) *DebugError {
func (e *NestedError) Set(vals ErrVals) *NestedError {
if e.Vals == nil {
e.Vals = make(ErrVals)
}
e.Vals.Merge(vals.Dupe())
return e
}

func newDebugError(err error, msg string) *DebugError {
return &DebugError{
func newNestedError(err error, msg string) *NestedError {
return &NestedError{
Err: err,
Msg: msg,
}
Expand Down
8 changes: 6 additions & 2 deletions parser/panic_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package parser

import (
"encoding/json"
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

func TestDebugError(t *testing.T) {
func TestNestedError(t *testing.T) {
err := errors.New("some random error")
ne := newDebugError(newDebugError(err, "a"), "b")
ne := newNestedError(newNestedError(err, "a"), "b")
assert.EqualValues(t, ne.Original(), err)
m := make(map[string]interface{})
assert.NoError(t, json.Unmarshal([]byte(ne.Error()), &m))
assert.True(t, len(m) > 0)
}

func TestEvaluatorPanic(t *testing.T) {
Expand Down
Loading

0 comments on commit c2f6bc1

Please sign in to comment.