Skip to content

Commit be341a7

Browse files
committed
fix ∩ comparison
1 parent 238f3c9 commit be341a7

5 files changed

Lines changed: 81 additions & 5 deletions

File tree

cmp.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ const (
2525

2626
func cmpInt(actual, expect int64, op string) bool {
2727
switch op {
28-
case opEqual,opIn:
28+
case opEqual, opIn:
2929
return actual == expect
30-
case opNotEqual,opNotIn:
30+
case opNotEqual, opNotIn:
3131
return actual != expect
3232
case opLarger:
3333
return actual > expect
@@ -63,9 +63,9 @@ func cmpFloat(actual, expect float64, op string) bool {
6363

6464
func cmpStr(actual, expect string, op string) bool {
6565
switch op {
66-
case opEqual,opIn:
66+
case opEqual, opIn:
6767
return actual == expect
68-
case opNotEqual,opNotIn:
68+
case opNotEqual, opNotIn:
6969
return actual != expect
7070
case opLarger:
7171
return actual > expect
@@ -91,12 +91,19 @@ func cmpBool(actual, expect bool, op string) bool {
9191
}
9292
}
9393

94-
func compareSet(actual interface{}, expect []string, op string) bool {
94+
func shouldCompareSet(op string) bool {
9595
switch op {
9696
case opEqual, opNotEqual, opInter, opNotInter, opIn, opNotIn:
97+
return true
9798
default:
9899
return false
99100
}
101+
}
102+
103+
func compareSet(actual interface{}, expect []string, op string) bool {
104+
if !shouldCompareSet(op) {
105+
return false
106+
}
100107
switch actualArr := actual.(type) {
101108
case int:
102109
return cmpIntSet([]int64{int64(actualArr)}, expect, op)

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/caibirdme/yql
2+
3+
go 1.15
4+
5+
require (
6+
github.com/antlr/antlr4 v0.0.0-20210121092344-5dce78c87a9e
7+
github.com/stretchr/testify v1.7.0
8+
)

go.sum

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
github.com/antlr/antlr4 v0.0.0-20210121092344-5dce78c87a9e h1:1YJFJAhOCHWLME6YEBM0BI96x4P5mKEl6i6pdgg36WI=
2+
github.com/antlr/antlr4 v0.0.0-20210121092344-5dce78c87a9e/go.mod h1:T7PbCXFs94rrTttyxjbyT5+/1V8T2TYDejxUfHJjw1Y=
3+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
4+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
8+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
9+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
10+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
11+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
12+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
13+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

yql.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package yql
22

33
import (
44
"fmt"
5+
"reflect"
56
"strconv"
67

78
"github.com/antlr/antlr4/runtime/Go/antlr"
@@ -180,6 +181,9 @@ func compare(actualValue interface{}, expectValue []string, op string) bool {
180181
if len(expectValue) > 1 {
181182
return compareSet(actualValue, expectValue, op)
182183
}
184+
if reflect.TypeOf(actualValue).Kind() == reflect.Slice {
185+
return compareSet(actualValue, expectValue, op)
186+
}
183187
e := removeQuote(expectValue[0])
184188
switch actual := actualValue.(type) {
185189
case int:

yql_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
78
)
89

910
func TestHandleSyntaxErr(t *testing.T) {
@@ -1151,3 +1152,46 @@ func TestRule_Match_Multi(t *testing.T) {
11511152
ass.Equal(tc.out, ok, "data=%+v", tc.data)
11521153
}
11531154
}
1155+
1156+
func Test_Compare_Slice_And_One_Element(t *testing.T) {
1157+
should := require.New(t)
1158+
var testData = []struct {
1159+
rawYql string
1160+
data map[string]interface{}
1161+
out bool
1162+
}{
1163+
{
1164+
rawYql: `letter ∩ ('a')`,
1165+
data: map[string]interface{}{
1166+
"letter": []string{"a", "b", "c"},
1167+
},
1168+
out: true,
1169+
},
1170+
{
1171+
rawYql: `letter ∩ ('a', 'b')`,
1172+
data: map[string]interface{}{
1173+
"letter": []string{"a", "b", "c"},
1174+
},
1175+
out: true,
1176+
},
1177+
{
1178+
rawYql: `letter ∩ ('d')`,
1179+
data: map[string]interface{}{
1180+
"letter": []string{"a", "b", "c"},
1181+
},
1182+
out: false,
1183+
},
1184+
{
1185+
rawYql: `letter in ('a')`,
1186+
data: map[string]interface{}{
1187+
"letter": []string{"a", "b", "c"},
1188+
},
1189+
out: false,
1190+
},
1191+
}
1192+
for _, tc := range testData {
1193+
actual, err := Match(tc.rawYql, tc.data)
1194+
should.NoError(err)
1195+
should.Equal(tc.out, actual)
1196+
}
1197+
}

0 commit comments

Comments
 (0)