Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,16 @@ var evalTests = []evalTest{
nil,
false,
},
{
`"bar" in "foobar"`,
nil,
true,
},
{
`"bar" in "world"`,
nil,
false,
},
{
`A == nil`,
struct{ A interface{} }{nil},
Expand Down Expand Up @@ -450,7 +460,7 @@ var evalErrorTests = []evalErrorTest{
{
`1 in "a"`,
nil,
`operator "in" not defined on string`,
`cannot use float64 with "in" operator and string`,
},
{
`nil in map`,
Expand Down
7 changes: 7 additions & 0 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package expr
import (
"fmt"
"reflect"
"strings"
)

func toNumber(n Node, val interface{}) float64 {
Expand Down Expand Up @@ -153,6 +154,12 @@ func contains(needle interface{}, array interface{}) (bool, error) {
return contains(needle, value.Interface())
}
return false, nil
case reflect.String:
n := reflect.ValueOf(needle)
if !n.IsValid() || n.Kind() != reflect.String {
return false, fmt.Errorf("cannot use %T with \"in\" operator and string", needle)
}
return strings.Contains(v.String(), n.String()), nil
}
return false, fmt.Errorf("operator \"in\" not defined on %T", array)
}
Expand Down
3 changes: 3 additions & 0 deletions type.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ func (n binaryNode) Type(table typesTable) (reflect.Type, error) {
if isArrayType(rtype) || isMapType(rtype) || isInterfaceType(rtype) {
return boolType, nil
}
if isStringType(ltype) && isStringType(rtype) {
return boolType, nil
}
return nil, fmt.Errorf(`invalid operation: %v (mismatched types %v and %v)`, n, ltype, rtype)

case "<", ">", ">=", "<=":
Expand Down
5 changes: 1 addition & 4 deletions type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ var typeTests = []typeTest{
"Str in Foo",
"Str in Arr",
"nil in Arr",
"Str in Str",
"Str not in Foo2p",
"Int | Num",
"Int ^ Num",
Expand Down Expand Up @@ -279,10 +280,6 @@ var typeErrorTests = []typeErrorTest{
"NilFn() and OkFn()",
"invalid operation: NilFn() and OkFn() (mismatched types <nil> and bool)",
},
{
"'str' in Str",
`invalid operation: "str" in Str (mismatched types string and string)`,
},
{
"1 in Foo",
"invalid operation: 1 in Foo (mismatched types float64 and *expr_test.foo)",
Expand Down