1
0
mirror of https://github.com/danog/liquid.git synced 2024-12-02 21:57:47 +01:00
liquid/expressions/builders.go

79 lines
1.8 KiB
Go
Raw Normal View History

package expressions
import (
"reflect"
"strings"
)
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)
}
}
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)
}
}
func makeIndexExpr(obj, index func(Context) interface{}) func(Context) interface{} {
return func(ctx Context) interface{} {
ref := reflect.ValueOf(obj(ctx))
2017-06-28 15:50:04 +02:00
i := reflect.ValueOf(index(ctx))
switch ref.Kind() {
case reflect.Array, reflect.Slice:
2017-06-28 15:50:04 +02:00
switch i.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
2017-06-28 15:50:04 +02:00
n := int(i.Int())
if n < 0 {
n = ref.Len() + n
}
if 0 <= n && n < ref.Len() {
return ref.Index(n).Interface()
}
}
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() {
return item.Interface()
}
}
}
return nil
}
}
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":
return ref.Index(0).Interface()
case "last":
return ref.Index(ref.Len() - 1).Interface()
case "size":
return ref.Len()
}
case reflect.String:
if attr == "size" {
return ref.Len()
}
case reflect.Map:
value := ref.MapIndex(reflect.ValueOf(attr))
if value.Kind() != reflect.Invalid {
return value.Interface()
}
}
return nil
}
}