Skip to content

Commit

Permalink
fix ∩ comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
caibirdme committed Jan 22, 2021
1 parent 238f3c9 commit be341a7
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 5 deletions.
17 changes: 12 additions & 5 deletions cmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
4 changes: 4 additions & 0 deletions yql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package yql

import (
"fmt"
"reflect"
"strconv"

"github.com/antlr/antlr4/runtime/Go/antlr"
Expand Down Expand Up @@ -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:
Expand Down
44 changes: 44 additions & 0 deletions yql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

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

func TestHandleSyntaxErr(t *testing.T) {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit be341a7

Please sign in to comment.