diff --git a/cmp.go b/cmp.go index 45dfc17..b5869bd 100644 --- a/cmp.go +++ b/cmp.go @@ -25,9 +25,9 @@ const ( func cmpInt(actual, expect int64, op string) bool { switch op { - case opEqual,opIn: + case opEqual, opIn: return actual == expect - case opNotEqual,opNotIn: + case opNotEqual, opNotIn: return actual != expect case opLarger: return actual > expect @@ -63,9 +63,9 @@ func cmpFloat(actual, expect float64, op string) bool { func cmpStr(actual, expect string, op string) bool { switch op { - case opEqual,opIn: + case opEqual, opIn: return actual == expect - case opNotEqual,opNotIn: + case opNotEqual, opNotIn: return actual != expect case opLarger: return actual > expect @@ -91,12 +91,19 @@ func cmpBool(actual, expect bool, op string) bool { } } -func compareSet(actual interface{}, expect []string, op string) bool { +func shouldCompareSet(op string) bool { switch op { case opEqual, opNotEqual, opInter, opNotInter, opIn, opNotIn: + return true default: return false } +} + +func compareSet(actual interface{}, expect []string, op string) bool { + if !shouldCompareSet(op) { + return false + } switch actualArr := actual.(type) { case int: return cmpIntSet([]int64{int64(actualArr)}, expect, op) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..57b8ed3 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/caibirdme/yql + +go 1.15 + +require ( + github.com/antlr/antlr4 v0.0.0-20210121092344-5dce78c87a9e + github.com/stretchr/testify v1.7.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..179d3b9 --- /dev/null +++ b/go.sum @@ -0,0 +1,13 @@ +github.com/antlr/antlr4 v0.0.0-20210121092344-5dce78c87a9e h1:1YJFJAhOCHWLME6YEBM0BI96x4P5mKEl6i6pdgg36WI= +github.com/antlr/antlr4 v0.0.0-20210121092344-5dce78c87a9e/go.mod h1:T7PbCXFs94rrTttyxjbyT5+/1V8T2TYDejxUfHJjw1Y= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/yql.go b/yql.go index bb0ffea..d9aa7f8 100644 --- a/yql.go +++ b/yql.go @@ -2,6 +2,7 @@ package yql import ( "fmt" + "reflect" "strconv" "github.com/antlr/antlr4/runtime/Go/antlr" @@ -180,6 +181,9 @@ func compare(actualValue interface{}, expectValue []string, op string) bool { if len(expectValue) > 1 { return compareSet(actualValue, expectValue, op) } + if reflect.TypeOf(actualValue).Kind() == reflect.Slice { + return compareSet(actualValue, expectValue, op) + } e := removeQuote(expectValue[0]) switch actual := actualValue.(type) { case int: diff --git a/yql_test.go b/yql_test.go index ae08232..79cd964 100644 --- a/yql_test.go +++ b/yql_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestHandleSyntaxErr(t *testing.T) { @@ -1151,3 +1152,46 @@ func TestRule_Match_Multi(t *testing.T) { ass.Equal(tc.out, ok, "data=%+v", tc.data) } } + +func Test_Compare_Slice_And_One_Element(t *testing.T) { + should := require.New(t) + var testData = []struct { + rawYql string + data map[string]interface{} + out bool + }{ + { + rawYql: `letter ∩ ('a')`, + data: map[string]interface{}{ + "letter": []string{"a", "b", "c"}, + }, + out: true, + }, + { + rawYql: `letter ∩ ('a', 'b')`, + data: map[string]interface{}{ + "letter": []string{"a", "b", "c"}, + }, + out: true, + }, + { + rawYql: `letter ∩ ('d')`, + data: map[string]interface{}{ + "letter": []string{"a", "b", "c"}, + }, + out: false, + }, + { + rawYql: `letter in ('a')`, + data: map[string]interface{}{ + "letter": []string{"a", "b", "c"}, + }, + out: false, + }, + } + for _, tc := range testData { + actual, err := Match(tc.rawYql, tc.data) + should.NoError(err) + should.Equal(tc.out, actual) + } +}