1
0
mirror of https://github.com/danog/liquid.git synced 2024-12-04 06:17:51 +01:00
liquid/expressions/builders.go

49 lines
1.2 KiB
Go
Raw Normal View History

2017-07-14 02:18:23 +02:00
package expressions
import (
"reflect"
2017-07-13 20:58:36 +02:00
"github.com/osteele/liquid/evaluator"
)
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))
return evaluator.NewRange(b.(int), e.(int))
2017-07-13 20:58:36 +02:00
}
}
func makeContainsExpr(e1, e2 func(Context) interface{}) func(Context) interface{} {
return func(ctx Context) interface{} {
s, ok := e2((ctx)).(string)
2017-07-03 18:48:00 +02:00
if !ok {
return false
}
return evaluator.ContainsString(e1(ctx), s)
}
}
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
}
}
func makeIndexExpr(sequenceFn, indexFn func(Context) interface{}) func(Context) interface{} {
return func(ctx Context) interface{} {
return evaluator.Index(sequenceFn(ctx), indexFn(ctx))
}
}
func makeObjectPropertyExpr(objFn func(Context) interface{}, name string) func(Context) interface{} {
return func(ctx Context) interface{} {
return evaluator.ObjectProperty(objFn(ctx), name)
}
}