Skip to content

Commit

Permalink
"contains" tests for arrays too
Browse files Browse the repository at this point in the history
  • Loading branch information
osteele committed Jul 3, 2017
1 parent ba874de commit 24d83f1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
40 changes: 30 additions & 10 deletions expressions/builders.go
Expand Up @@ -7,9 +7,29 @@ import (

func makeContainsExpr(e1, e2 func(Context) interface{}) func(Context) interface{} {
return func(ctx Context) interface{} {
a, aok := e1(ctx).(string)
b, bok := e2(ctx).(string)
return aok && bok && strings.Contains(a, b)
search, ok := e2((ctx)).(string)
if !ok {
return false
}
switch container := e1((ctx)).(type) {
case string:
return strings.Contains(container, search)
case []string:
for _, s := range container {
if s == search {
return true
}
}
case []interface{}:
for _, k := range container {
if s, ok := k.(string); ok && s == search {
return true
}
}
default:
return false
}
return false
}
}

Expand All @@ -32,14 +52,14 @@ func makeIndexExpr(obj, index func(Context) interface{}) func(Context) interface
n = ref.Len() + n
}
if 0 <= n && n < ref.Len() {
return ref.Index(n).Interface()
return ToLiquid(ref.Index(n).Interface())
}
}
case reflect.Map:
if i.Type().ConvertibleTo(ref.Type().Key()) {
item := ref.MapIndex(i.Convert(ref.Type().Key()))
if item.IsValid() {
return item.Interface()
return ToLiquid(item.Interface())
}
}
}
Expand All @@ -57,20 +77,20 @@ func makeObjectPropertyExpr(obj func(Context) interface{}, attr string) func(Con
}
switch attr {
case "first":
return ref.Index(0).Interface()
return ToLiquid(ref.Index(0).Interface())
case "last":
return ref.Index(ref.Len() - 1).Interface()
return ToLiquid(ref.Index(ref.Len() - 1).Interface())
case "size":
return ref.Len()
return ToLiquid(ref.Len())
}
case reflect.String:
if attr == "size" {
return ref.Len()
return ToLiquid(ref.Len())
}
case reflect.Map:
value := ref.MapIndex(reflect.ValueOf(attr))
if value.Kind() != reflect.Invalid {
return value.Interface()
return ToLiquid(value.Interface())
}
}
return nil
Expand Down
2 changes: 2 additions & 0 deletions expressions/expressions_test.go
Expand Up @@ -92,6 +92,8 @@ var evaluatorTests = []struct {

{`"seafood" contains "foo"`, true},
{`"seafood" contains "bar"`, false},
{`array contains "first"`, true},
{`"foo" contains "missing"`, false},

// filters
{`"seafood" | length`, 8},
Expand Down

0 comments on commit 24d83f1

Please sign in to comment.