2017-07-04 17:12:40 +02:00
|
|
|
package expression
|
2017-06-28 02:32:55 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2017-06-30 17:21:10 +02:00
|
|
|
"strings"
|
2017-06-28 02:32:55 +02:00
|
|
|
)
|
|
|
|
|
2017-07-04 13:41:17 +02:00
|
|
|
func makeContainsExpr(e1, e2 func(Context) interface{}) func(Context) interface{} { // nolint: gocyclo
|
2017-06-28 02:32:55 +02:00
|
|
|
return func(ctx Context) interface{} {
|
2017-07-03 18:48:00 +02:00
|
|
|
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
|
2017-06-28 02:32:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 22:13:18 +02:00
|
|
|
func makeFilter(fn valueFn, name string, args []valueFn) valueFn {
|
|
|
|
return func(ctx Context) interface{} {
|
|
|
|
return ctx.Filters().runFilter(ctx, fn, name, args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 17:21:10 +02:00
|
|
|
func makeIndexExpr(obj, index func(Context) interface{}) func(Context) interface{} {
|
2017-06-28 02:32:55 +02:00
|
|
|
return func(ctx Context) interface{} {
|
|
|
|
ref := reflect.ValueOf(obj(ctx))
|
2017-06-28 15:50:04 +02:00
|
|
|
i := reflect.ValueOf(index(ctx))
|
2017-06-28 02:32:55 +02:00
|
|
|
switch ref.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
2017-06-28 15:50:04 +02:00
|
|
|
switch i.Kind() {
|
2017-06-28 02:32:55 +02:00
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2017-06-28 15:50:04 +02:00
|
|
|
n := int(i.Int())
|
2017-06-28 02:32:55 +02:00
|
|
|
if n < 0 {
|
|
|
|
n = ref.Len() + n
|
|
|
|
}
|
|
|
|
if 0 <= n && n < ref.Len() {
|
2017-07-03 18:48:00 +02:00
|
|
|
return ToLiquid(ref.Index(n).Interface())
|
2017-06-28 02:32:55 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-29 20:21:06 +02:00
|
|
|
case reflect.Map:
|
|
|
|
if i.Type().ConvertibleTo(ref.Type().Key()) {
|
|
|
|
item := ref.MapIndex(i.Convert(ref.Type().Key()))
|
|
|
|
if item.IsValid() {
|
2017-07-03 18:48:00 +02:00
|
|
|
return ToLiquid(item.Interface())
|
2017-06-29 20:21:06 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-28 02:32:55 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2017-06-30 17:21:10 +02:00
|
|
|
|
|
|
|
func makeObjectPropertyExpr(obj func(Context) interface{}, attr string) func(Context) interface{} {
|
|
|
|
return func(ctx Context) interface{} {
|
|
|
|
ref := reflect.ValueOf(obj(ctx))
|
|
|
|
switch ref.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
if ref.Len() == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch attr {
|
|
|
|
case "first":
|
2017-07-03 18:48:00 +02:00
|
|
|
return ToLiquid(ref.Index(0).Interface())
|
2017-06-30 17:21:10 +02:00
|
|
|
case "last":
|
2017-07-03 18:48:00 +02:00
|
|
|
return ToLiquid(ref.Index(ref.Len() - 1).Interface())
|
2017-06-30 17:21:10 +02:00
|
|
|
case "size":
|
2017-07-03 18:48:00 +02:00
|
|
|
return ToLiquid(ref.Len())
|
2017-06-30 17:21:10 +02:00
|
|
|
}
|
|
|
|
case reflect.String:
|
|
|
|
if attr == "size" {
|
2017-07-03 18:48:00 +02:00
|
|
|
return ToLiquid(ref.Len())
|
2017-06-30 17:21:10 +02:00
|
|
|
}
|
|
|
|
case reflect.Map:
|
|
|
|
value := ref.MapIndex(reflect.ValueOf(attr))
|
|
|
|
if value.Kind() != reflect.Invalid {
|
2017-07-03 18:48:00 +02:00
|
|
|
return ToLiquid(value.Interface())
|
2017-06-30 17:21:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|