Skip to content

Commit

Permalink
Merge pull request #3 from huttotw/feature/slice-interfaces
Browse files Browse the repository at this point in the history
fix slice evaluator to use interfaces instead of the underlying type
  • Loading branch information
huttotw authored Jan 8, 2018
2 parents ea22392 + afe28a0 commit 11358f4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 38 deletions.
52 changes: 26 additions & 26 deletions comparators.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,168 +166,168 @@ func contains(a, b interface{}) bool {
}

func containsString(a, b interface{}) bool {
as, ok := a.([]string)
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if elem == b.(string) {
if val, ok := elem.(string); ok && val == b.(string) {
return true
}
}
return false
}

func containsInt(a, b interface{}) bool {
as, ok := a.([]int)
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if elem == b.(int) {
if val, ok := elem.(int); ok && val == b.(int) {
return true
}
}
return false
}

func containsInt8(a, b interface{}) bool {
as, ok := a.([]int8)
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if elem == b.(int8) {
if val, ok := elem.(int8); ok && val == b.(int8) {
return true
}
}
return false
}

func containsInt16(a, b interface{}) bool {
as, ok := a.([]int16)
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if elem == b.(int16) {
if val, ok := elem.(int16); ok && val == b.(int16) {
return true
}
}
return false
}

func containsInt32(a, b interface{}) bool {
as, ok := a.([]int32)
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if elem == b.(int32) {
if val, ok := elem.(int32); ok && val == b.(int32) {
return true
}
}
return false
}

func containsInt64(a, b interface{}) bool {
as, ok := a.([]int64)
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if elem == b.(int64) {
if val, ok := elem.(int64); ok && val == b.(int64) {
return true
}
}
return false
}

func containsUint(a, b interface{}) bool {
as, ok := a.([]uint)
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if elem == b.(uint) {
if val, ok := elem.(uint); ok && val == b.(uint) {
return true
}
}
return false
}

func containsUint8(a, b interface{}) bool {
as, ok := a.([]uint8)
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if elem == b.(uint8) {
if val, ok := elem.(uint8); ok && val == b.(uint8) {
return true
}
}
return false
}

func containsUint16(a, b interface{}) bool {
as, ok := a.([]uint16)
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if elem == b.(uint16) {
if val, ok := elem.(uint16); ok && val == b.(uint16) {
return true
}
}
return false
}

func containsUint32(a, b interface{}) bool {
as, ok := a.([]uint32)
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if elem == b.(uint32) {
if val, ok := elem.(uint32); ok && val == b.(uint32) {
return true
}
}
return false
}

func containsUint64(a, b interface{}) bool {
as, ok := a.([]uint64)
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if elem == b.(uint64) {
if val, ok := elem.(uint64); ok && val == b.(uint64) {
return true
}
}
return false
}

func containsFloat32(a, b interface{}) bool {
as, ok := a.([]float32)
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if elem == b.(float32) {
if val, ok := elem.(float32); ok && val == b.(float32) {
return true
}
}
return false
}

func containsFloat64(a, b interface{}) bool {
as, ok := a.([]float64)
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if elem == b.(float64) {
if val, ok := elem.(float64); ok && val == b.(float64) {
return true
}
}
Expand Down
24 changes: 12 additions & 12 deletions comparators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ func BenchmarkGreaterThanEqual(b *testing.B) {

func TestContains(t *testing.T) {
cases := []testCase{
testCase{args: []interface{}{[]string{"a", "b"}, "a"}, expected: true},
testCase{args: []interface{}{[]string{"a", "b"}, "c"}, expected: false},
testCase{args: []interface{}{[]string{"a", "b"}, 1}, expected: false},
testCase{args: []interface{}{[]int{1, 2}, 1}, expected: true},
testCase{args: []interface{}{[]int{1, 2}, 3}, expected: false},
testCase{args: []interface{}{[]float64{1.01, 1.02}, 1.01}, expected: true},
testCase{args: []interface{}{[]interface{}{"a", "b"}, "a"}, expected: true},
testCase{args: []interface{}{[]interface{}{"a", "b"}, "c"}, expected: false},
testCase{args: []interface{}{[]interface{}{"a", "b"}, 1}, expected: false},
testCase{args: []interface{}{[]interface{}{1, 2}, 1}, expected: true},
testCase{args: []interface{}{[]interface{}{1, 2}, 3}, expected: false},
testCase{args: []interface{}{[]interface{}{1.01, 1.02}, 1.01}, expected: true},
}

for i, c := range cases {
Expand Down Expand Up @@ -201,12 +201,12 @@ func BenchmarkContainsLong50000(b *testing.B) {

func TestOneOf(t *testing.T) {
cases := []testCase{
testCase{args: []interface{}{"a", []string{"a", "b"}}, expected: true},
testCase{args: []interface{}{"c", []string{"a", "b"}}, expected: false},
testCase{args: []interface{}{1, []string{"a", "b"}}, expected: false},
testCase{args: []interface{}{1, []int{1, 2}}, expected: true},
testCase{args: []interface{}{3, []int{1, 2}}, expected: false},
testCase{args: []interface{}{1.01, []float64{1.01, 1.02}}, expected: true},
testCase{args: []interface{}{"a", []interface{}{"a", "b"}}, expected: true},
testCase{args: []interface{}{"c", []interface{}{"a", "b"}}, expected: false},
testCase{args: []interface{}{1, []interface{}{"a", "b"}}, expected: false},
testCase{args: []interface{}{1, []interface{}{1, 2}}, expected: true},
testCase{args: []interface{}{3, []interface{}{1, 2}}, expected: false},
testCase{args: []interface{}{1.01, []interface{}{1.01, 1.02}}, expected: true},
}
for i, c := range cases {
res := oneOf(c.args[0], c.args[1])
Expand Down
31 changes: 31 additions & 0 deletions rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,37 @@ func TestEngineEvaluate(t *testing.T) {
})

t.Run("1 composite, 1 rule", func(t *testing.T) {
props := map[string]interface{}{
"address": map[string]interface{}{
"bedroom": map[string]interface{}{
"furniture": []interface{}{
"bed",
"tv",
"dresser",
},
},
},
}
e := NewEngine()
e.Composites = []Composite{
Composite{
Operator: OperatorAnd,
Rules: []Rule{
Rule{
Comparator: "contains",
Path: "address.bedroom.furniture",
Value: "tv",
},
},
},
}
res := e.Evaluate(props)
if res != true {
t.Fatal("expected engine to pass")
}
})

t.Run("2 composites, 1 rule", func(t *testing.T) {
props := map[string]interface{}{
"user": map[string]interface{}{
"email": "test@test.com",
Expand Down

0 comments on commit 11358f4

Please sign in to comment.