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

46 lines
1.2 KiB
Go
Raw Normal View History

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