Skip to content

Commit

Permalink
Fix json.Number type in evaluator
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouzhuojie committed Nov 17, 2017
1 parent 1f49951 commit c70410a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
33 changes: 33 additions & 0 deletions evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,39 @@ func evaluateSubtree(expr Expr, args map[string]interface{}) (Expr, error) {
snl.Val = append(snl.Val, float64(v))
}
return snl, nil
case []json.Number:
snl := &SliceNumberLiteral{}
for _, v := range args[index].([]json.Number) {
f, _ := v.Float64()
snl.Val = append(snl.Val, f)
}
return snl, nil
case []interface{}:
items := args[index].([]interface{})
if len(items) != 0 {
item := items[0]
switch item.(type) {
case string:
snl := &SliceStringLiteral{}
for _, v := range items {
snl.Val = append(snl.Val, v.(string))
}
return snl, nil
case float64:
snl := &SliceNumberLiteral{}
for _, v := range items {
snl.Val = append(snl.Val, v.(float64))
}
return snl, nil
case json.Number:
snl := &SliceNumberLiteral{}
for _, v := range items {
f, _ := v.(json.Number).Float64()
snl.Val = append(snl.Val, f)
}
return snl, nil
}
}
}
}
return falseExpr, fmt.Errorf("Unsupported argument %s type: %s", n.Val, kind)
Expand Down
48 changes: 47 additions & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ var validTestData = []struct {
{"{foo}{dfs}{a} == true and {bar} == true", map[string]interface{}{"foo.dfs.a": true, "bar": true}, true, false},
{"{@foo}{a} == true and {bar} == true", map[string]interface{}{"@foo.a": true, "bar": true}, true, false},
{"{foo}{unknow} == true and {bar} == true", map[string]interface{}{"foo.dfs": true, "bar": true}, false, true},
{"{foo} == 123", map[string]interface{}{"foo": json.Number("123"), "bar": "something"}, true, false},
{"{foo} == 123", map[string]interface{}{"foo": json.Number("123"), "bar": true}, true, false},

// OR
{"{foo} == true OR {foo} > 1", map[string]interface{}{"foo": true}, false, true},
Expand All @@ -114,6 +114,8 @@ var validTestData = []struct {

// IN
{"{foo} in {foobar}", map[string]interface{}{"foo": "findme", "foobar": []string{"notme", "may", "findme", "lol"}}, true, false},
{"{foo} in [123]", map[string]interface{}{"foo": json.Number("123"), "baz": true}, true, false},
{"{foo} in [123]", map[string]interface{}{"foo": json.Number("124"), "baz": true}, false, false},

// NOT IN
{"{foo} not in {foobar}", map[string]interface{}{"foo": "dontfindme", "foobar": []string{"notme", "may", "findme", "lol"}}, true, false},
Expand Down Expand Up @@ -149,6 +151,11 @@ var validTestData = []struct {
{`{foo} contains {bar}`, map[string]interface{}{"foo": []int{1, 2}, "bar": int32(1)}, true, false},
{`{foo} contains {bar}`, map[string]interface{}{"foo": []int{1, 2, 3}, "bar": float32(1.0 + 2.0)}, true, false},
{`{foo} contains {bar}`, map[string]interface{}{"foo": []float64{0.29}, "bar": float32(29.0 / 100)}, true, false},
{`{foo} contains 2`, map[string]interface{}{"foo": []json.Number{"2"}}, true, false},
{`{foo} contains 2`, map[string]interface{}{"foo": []json.Number{"2", "3"}}, true, false},
{`{foo} contains 2`, map[string]interface{}{"foo": []json.Number{"3"}}, false, false},
{`{foo} contains 2`, map[string]interface{}{"foo": []interface{}{json.Number("2")}}, true, false},
{`{foo} contains 2`, map[string]interface{}{"foo": []interface{}{json.Number("3")}}, false, false},

//{NOT}CONTAINS
{`{foo} not contains "2"`, map[string]interface{}{"foo": []string{"1", "2"}}, false, false},
Expand Down Expand Up @@ -292,6 +299,45 @@ func TestReadmeExample(t *testing.T) {
fmt.Println("Evaluation result:", r)
}

func TestJSON(t *testing.T) {
var tests = []struct {
cond string
jsonStr string
result bool
isErr bool
}{
{`{foo} == 123`, `{"foo": 123}`, true, false},
{`{foo} in [123]`, `{"foo": 123}`, true, false},
{`{foo} in [124]`, `{"foo": 123}`, false, false},
{`{foo} in [123]`, `{"foo": 123, "bar": "baz"}`, true, false},
{`{foo} in [124]`, `{"foo": 123, "bar": "baz"}`, false, false},

{`{foo} == "123"`, `{"foo": 123}`, false, true},
{`{foo} == "123"`, `{"foo": "123"}`, true, false},
{`{foo} not in ["123"]`, `{"foo": "123"}`, false, false},

{`{foo} contains "123"`, `{"foo": ["123"]}`, true, false},
{`{foo} contains 123`, `{"foo": [123]}`, true, false},
{`{foo} contains 123`, `{"foo": ["123"]}`, false, true},
{`{foo} not contains 123`, `{"foo": [124]}`, true, false},
{`{foo} not contains "123"`, `{"foo": ["124"]}`, true, false},
}

for _, test := range tests {
p := NewParser(strings.NewReader(test.cond))
expr, _ := p.Parse()
data := make(map[string]interface{})
json.Unmarshal([]byte(test.jsonStr), &data)
r, err := Evaluate(expr, data)
assert.Equal(t, r, test.result, test.cond, test.jsonStr)
if test.isErr {
assert.Error(t, err, test.cond, test.jsonStr)
} else {
assert.NoError(t, err, test.cond)
}
}
}

func BenchmarkParser(b *testing.B) {
cond := "({foo}{dfs}{a} == true AND {bar} == true) AND false"
args := map[string]interface{}{"foo.dfs.a": true, "bar": true, "something": 1.0}
Expand Down

0 comments on commit c70410a

Please sign in to comment.