2017-07-14 02:18:23 +02:00
|
|
|
package expressions
|
2017-06-28 02:32:55 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2017-07-13 20:58:36 +02:00
|
|
|
|
|
|
|
"github.com/osteele/liquid/evaluator"
|
2017-06-28 02:32:55 +02:00
|
|
|
)
|
|
|
|
|
2017-07-13 20:58:36 +02:00
|
|
|
func makeRangeExpr(startFn, endFn func(Context) interface{}) func(Context) interface{} {
|
|
|
|
return func(ctx Context) interface{} {
|
|
|
|
var proto int
|
|
|
|
b := evaluator.MustConvert(startFn(ctx), reflect.TypeOf(proto))
|
|
|
|
e := evaluator.MustConvert(endFn(ctx), reflect.TypeOf(proto))
|
2017-07-14 05:46:12 +02:00
|
|
|
return evaluator.NewRange(b.(int), e.(int))
|
2017-07-13 20:58:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 05:46:12 +02:00
|
|
|
func makeContainsExpr(e1, e2 func(Context) interface{}) func(Context) interface{} {
|
2017-06-28 02:32:55 +02:00
|
|
|
return func(ctx Context) interface{} {
|
2017-07-14 05:46:12 +02:00
|
|
|
s, ok := e2((ctx)).(string)
|
2017-07-03 18:48:00 +02:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2017-07-14 05:46:12 +02:00
|
|
|
return evaluator.ContainsString(e1(ctx), s)
|
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{} {
|
2017-07-16 19:47:06 +02:00
|
|
|
result, err := ctx.ApplyFilter(name, fn, args)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return result
|
2017-06-30 22:13:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 05:46:12 +02:00
|
|
|
func makeIndexExpr(sequenceFn, indexFn func(Context) interface{}) func(Context) interface{} {
|
2017-06-28 02:32:55 +02:00
|
|
|
return func(ctx Context) interface{} {
|
2017-07-14 05:46:12 +02:00
|
|
|
return evaluator.Index(sequenceFn(ctx), indexFn(ctx))
|
2017-06-28 02:32:55 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-30 17:21:10 +02:00
|
|
|
|
2017-07-14 05:46:12 +02:00
|
|
|
func makeObjectPropertyExpr(objFn func(Context) interface{}, name string) func(Context) interface{} {
|
2017-06-30 17:21:10 +02:00
|
|
|
return func(ctx Context) interface{} {
|
2017-07-14 05:46:12 +02:00
|
|
|
return evaluator.ObjectProperty(objFn(ctx), name)
|
2017-06-30 17:21:10 +02:00
|
|
|
}
|
|
|
|
}
|