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

43 lines
1.2 KiB
Go
Raw Normal View History

2017-07-14 02:18:23 +02:00
package expressions
import (
2017-07-13 20:58:36 +02:00
"github.com/osteele/liquid/evaluator"
)
func makeRangeExpr(startFn, endFn func(Context) evaluator.Value) func(Context) evaluator.Value {
return func(ctx Context) evaluator.Value {
a := startFn(ctx).Int()
b := endFn(ctx).Int()
return evaluator.ValueOf(evaluator.NewRange(a, b))
2017-07-13 20:58:36 +02:00
}
}
func makeContainsExpr(e1, e2 func(Context) evaluator.Value) func(Context) evaluator.Value {
return func(ctx Context) evaluator.Value {
return evaluator.ValueOf(e1(ctx).Contains(e2(ctx)))
}
}
2017-06-30 22:13:18 +02:00
func makeFilter(fn valueFn, name string, args []valueFn) valueFn {
return func(ctx Context) evaluator.Value {
2017-07-16 19:47:06 +02:00
result, err := ctx.ApplyFilter(name, fn, args)
if err != nil {
panic(err)
}
return evaluator.ValueOf(result)
2017-06-30 22:13:18 +02:00
}
}
func makeIndexExpr(sequenceFn, indexFn func(Context) evaluator.Value) func(Context) evaluator.Value {
return func(ctx Context) evaluator.Value {
return sequenceFn(ctx).IndexValue(indexFn(ctx))
}
}
func makeObjectPropertyExpr(objFn func(Context) evaluator.Value, name string) func(Context) evaluator.Value {
index := evaluator.ValueOf(name)
return func(ctx Context) evaluator.Value {
return objFn(ctx).PropertyValue(index)
}
}