Skip to content

Commit

Permalink
Issue 90 - add support of string comparison (#91)
Browse files Browse the repository at this point in the history
* added index for viewdoc

* pushing first viewdoc

* Added more formating for viewdoc

* add support comparison string values

* Added string comparisson for LT, LTE, GT, GTE

* merge test case from newm4n/feat/stringcompare

* upgrade go.mod

* simplify slice composite literal

Co-authored-by: Ferdinand Neman <[email protected]>
Co-authored-by: Xiao Xi XX22 Liu <[email protected]>
  • Loading branch information
3 people authored Aug 16, 2020
1 parent ff697a1 commit 26a18ff
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 11 deletions.
18 changes: 9 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ module github.com/hyperjumptech/grule-rule-engine
go 1.13

require (
github.com/antlr/antlr4 v0.0.0-20191212171830-8ae756a02574
github.com/bmatcuk/doublestar v1.2.2
github.com/antlr/antlr4 v0.0.0-20200801005519-2ba38605b949
github.com/bmatcuk/doublestar v1.3.2
github.com/golang/mock v1.4.3
github.com/google/uuid v1.1.1
github.com/imkira/go-observer v1.0.3
github.com/kr/pretty v0.2.0 // indirect
github.com/kr/pretty v0.2.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/stretchr/testify v1.4.0
golang.org/x/crypto v0.0.0-20200210222208-86ce3cb69678 // indirect
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 // indirect
golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4 // indirect
github.com/sirupsen/logrus v1.6.0
github.com/stretchr/testify v1.6.1
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de // indirect
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc // indirect
golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed // indirect
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
gopkg.in/src-d/go-billy.v4 v4.3.2
gopkg.in/src-d/go-git.v4 v4.13.1
gopkg.in/yaml.v2 v2.2.7 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ github.com/antlr/antlr4 v0.0.0-20191111162822-f7ffbdeea9b4 h1:UIqIlRKjXNF9+D7XL+
github.com/antlr/antlr4 v0.0.0-20191111162822-f7ffbdeea9b4/go.mod h1:T7PbCXFs94rrTttyxjbyT5+/1V8T2TYDejxUfHJjw1Y=
github.com/antlr/antlr4 v0.0.0-20191212171830-8ae756a02574 h1:8Zu0riRrXG4zyFZiQnXSjEgHNqMVQHwDMD1hZGkFXIU=
github.com/antlr/antlr4 v0.0.0-20191212171830-8ae756a02574/go.mod h1:T7PbCXFs94rrTttyxjbyT5+/1V8T2TYDejxUfHJjw1Y=
github.com/antlr/antlr4 v0.0.0-20200801005519-2ba38605b949/go.mod h1:T7PbCXFs94rrTttyxjbyT5+/1V8T2TYDejxUfHJjw1Y=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/bmatcuk/doublestar v1.2.2 h1:oC24CykoSAB8zd7XgruHo33E0cHJf/WhQA/7BeXj+x0=
github.com/bmatcuk/doublestar v1.2.2/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE=
github.com/bmatcuk/doublestar v1.3.2 h1:mzUncgFmpzNUhIITFqGdZ8nUU0O7JTJzRO8VdkeLCSo=
github.com/bmatcuk/doublestar v1.3.2/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -35,10 +38,12 @@ github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd h1:Coekwdh0v
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
Expand All @@ -57,6 +62,8 @@ github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/src-d/gcfg v1.4.0 h1:xXbNR5AlLSA315x2UO+fTSSAXCDf+Ar38/6oyGbDKQ4=
github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -67,6 +74,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70=
github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4=
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand All @@ -75,6 +83,9 @@ golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49N
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200210222208-86ce3cb69678 h1:wCWoJcFExDgyYx2m2hpHgwz8W3+FPdfldvIgzqDIhyg=
golang.org/x/crypto v0.0.0-20200210222208-86ce3cb69678/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de h1:ikNHVSjEfnvz6sxdSPCaPt572qowuyMDMJLLm3Db3ig=
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/lint v0.0.0-20190409202823-959b441ac422 h1:QzoH/1pFpZguR8NrRHLcO6jKqfv2zpuSqZLgdm7ZmjI=
golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand All @@ -84,6 +95,8 @@ golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 h1:Ao/3l156eZf2AW5wK8a7/smto
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc h1:zK/HqS5bZxDptfPJNq8v7vJfXtkU7r9TLIoSr1bXaP4=
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190221075227-b4e8571b14e0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -95,6 +108,9 @@ golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4 h1:sfkvUWPNGwSV+8/fNqctR5lS2AqCSqYwXdrjCxp/dXo=
golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed h1:J22ig1FUekjjkmZUM7pTKixYm8DvrYsvrBZdunYeIuQ=
golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
Expand Down Expand Up @@ -124,5 +140,7 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
36 changes: 36 additions & 0 deletions pkg/reflectmath.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,15 @@ func EvaluateBitOr(left, right reflect.Value) (reflect.Value, error) {
// EvaluateGreaterThan will evaluate GreaterThan operation over two value
func EvaluateGreaterThan(left, right reflect.Value) (reflect.Value, error) {
switch left.Kind() {
case reflect.String:
lv := left.String()
switch right.Kind() {
case reflect.String:
rv := right.String()
return reflect.ValueOf(lv > rv), nil
default:
return reflect.ValueOf(nil), fmt.Errorf("can not compare data type of string to %s in GT comparison", right.Kind().String())
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
lv := left.Int()
switch right.Kind() {
Expand Down Expand Up @@ -409,6 +418,15 @@ func EvaluateGreaterThan(left, right reflect.Value) (reflect.Value, error) {
// EvaluateLesserThan will evaluate LesserThan operation over two value
func EvaluateLesserThan(left, right reflect.Value) (reflect.Value, error) {
switch left.Kind() {
case reflect.String:
lv := left.String()
switch right.Kind() {
case reflect.String:
rv := right.String()
return reflect.ValueOf(lv < rv), nil
default:
return reflect.ValueOf(nil), fmt.Errorf("can not compare data type of string to %s in LT comparison", right.Kind().String())
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
lv := left.Int()
switch right.Kind() {
Expand Down Expand Up @@ -467,6 +485,15 @@ func EvaluateLesserThan(left, right reflect.Value) (reflect.Value, error) {
// EvaluateGreaterThanEqual will evaluate GreaterThanEqual operation over two value
func EvaluateGreaterThanEqual(left, right reflect.Value) (reflect.Value, error) {
switch left.Kind() {
case reflect.String:
lv := left.String()
switch right.Kind() {
case reflect.String:
rv := right.String()
return reflect.ValueOf(lv >= rv), nil
default:
return reflect.ValueOf(nil), fmt.Errorf("can not compare data type of string to %s in GTE comparison", right.Kind().String())
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
lv := left.Int()
switch right.Kind() {
Expand Down Expand Up @@ -525,6 +552,15 @@ func EvaluateGreaterThanEqual(left, right reflect.Value) (reflect.Value, error)
// EvaluateLesserThanEqual will evaluate LesserThanEqual operation over two value
func EvaluateLesserThanEqual(left, right reflect.Value) (reflect.Value, error) {
switch left.Kind() {
case reflect.String:
lv := left.String()
switch right.Kind() {
case reflect.String:
rv := right.String()
return reflect.ValueOf(lv <= rv), nil
default:
return reflect.ValueOf(nil), fmt.Errorf("can not compare data type of string to %s in LTE comparison", right.Kind().String())
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
lv := left.Int()
switch right.Kind() {
Expand Down
80 changes: 78 additions & 2 deletions pkg/reflectmath_test.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,84 @@ var (
uint16Val4 = reflect.ValueOf(uint16(0x7F))
uint32Val4 = reflect.ValueOf(uint32(0x7F))
uint64Val4 = reflect.ValueOf(uint64(0x7F))

StrCompareTest = []*StrCompare{
{"A", "A", true, false, false, true, false, true},
{"AA", "A", false, true, true, true, false, false},
{"A", "AA", false, true, false, false, true, true},
{" ", " ", false, true, false, false, true, true},
{" ", "A", false, true, false, false, true, true},
{"A", " ", false, true, true, true, false, false},
{"A", "aa", false, true, false, false, true, true},
{"aa", "A", false, true, true, true, false, false},
{"a", "AA", false, true, true, true, false, false},
{"AA", "a", false, true, false, false, true, true},
}
)

type StrCompare struct {
A string
B string
Eq bool
Neq bool
Gt bool
Gte bool
Lt bool
Lte bool
}

func TestStringComparison(t *testing.T) {
for i, v := range StrCompareTest {
val, err := EvaluateEqual(reflect.ValueOf(v.A), reflect.ValueOf(v.B))
if err != nil {
t.Errorf(err.Error())
t.Fail()
} else if val.Bool() != v.Eq {
t.Errorf("%d Expect \"%s\" and \"%s\" EQ expect %v but %v", i, v.A, v.B, v.Eq, !v.Eq)
}

val, err = EvaluateNotEqual(reflect.ValueOf(v.A), reflect.ValueOf(v.B))
if err != nil {
t.Errorf(err.Error())
t.Fail()
} else if val.Bool() != v.Neq {
t.Errorf("%d Expect \"%s\" and \"%s\" NEQ expect %v but %v", i, v.A, v.B, v.Neq, !v.Neq)
}

val, err = EvaluateGreaterThan(reflect.ValueOf(v.A), reflect.ValueOf(v.B))
if err != nil {
t.Errorf(err.Error())
t.Fail()
} else if val.Bool() != v.Gt {
t.Errorf("%d Expect \"%s\" and \"%s\" GT expect %v but %v", i, v.A, v.B, v.Gt, !v.Gt)
}

val, err = EvaluateGreaterThanEqual(reflect.ValueOf(v.A), reflect.ValueOf(v.B))
if err != nil {
t.Errorf(err.Error())
t.Fail()
} else if val.Bool() != v.Gte {
t.Errorf("%d Expect \"%s\" and \"%s\" GTE expect %v but %v", i, v.A, v.B, v.Gte, !v.Gte)
}

val, err = EvaluateLesserThan(reflect.ValueOf(v.A), reflect.ValueOf(v.B))
if err != nil {
t.Errorf(err.Error())
t.Fail()
} else if val.Bool() != v.Lt {
t.Errorf("%d Expect \"%s\" and \"%s\" LT expect %v but %v", i, v.A, v.B, v.Lt, !v.Lt)
}

val, err = EvaluateLesserThanEqual(reflect.ValueOf(v.A), reflect.ValueOf(v.B))
if err != nil {
t.Errorf(err.Error())
t.Fail()
} else if val.Bool() != v.Lte {
t.Errorf("%d Expect \"%s\" and \"%s\" LTE expect %v but %v", i, v.A, v.B, v.Lte, !v.Lte)
}
}
}

func TestValueAdd(t *testing.T) {
for _, va := range valuesA {
for _, vb := range valuesB {
Expand Down Expand Up @@ -368,7 +444,7 @@ func TestEvaluateGreaterThanEqual(t *testing.T) {
}

if vc.Kind() != reflect.Bool || vc.Bool() == false {
t.Errorf("12 > 3 == false")
t.Errorf("12 >= 3 == false")
}
}
}
Expand All @@ -384,7 +460,7 @@ func TestEvaluateLesserThanEqual(t *testing.T) {
}

if vc.Kind() != reflect.Bool || vc.Bool() == false {
t.Errorf("3 < 12 == false")
t.Errorf("3 <= 12 == false")
}
}
}
Expand Down

0 comments on commit 26a18ff

Please sign in to comment.