1
0
mirror of https://github.com/danog/liquid.git synced 2024-12-02 19:27:51 +01:00
liquid/expression/filters.go

87 lines
2.2 KiB
Go
Raw Normal View History

2017-07-04 17:12:40 +02:00
package expression
2017-06-27 00:55:12 +02:00
import (
"fmt"
"reflect"
2017-06-27 16:28:39 +02:00
"github.com/osteele/liquid/generics"
2017-06-27 00:55:12 +02:00
)
2017-06-27 19:36:38 +02:00
// An InterpreterError is an error during expression interpretation.
// It is used for errors in the input expression, to distinguish them
// from implementation errors in the interpreter.
2017-06-27 15:17:57 +02:00
type InterpreterError string
func (e InterpreterError) Error() string { return string(e) }
2017-06-29 13:54:31 +02:00
// UndefinedFilter is an error that the named filter is not defined.
type UndefinedFilter string
func (e UndefinedFilter) Error() string {
return fmt.Sprintf("undefined filter: %s", string(e))
}
2017-06-27 00:55:12 +02:00
type valueFn func(Context) interface{}
2017-07-02 05:52:23 +02:00
type filterDictionary struct {
2017-06-30 22:13:18 +02:00
filters map[string]interface{}
}
2017-07-05 17:17:31 +02:00
func newFilterDictionary() filterDictionary {
return filterDictionary{map[string]interface{}{}}
2017-06-30 22:13:18 +02:00
}
2017-06-27 03:32:08 +02:00
2017-07-05 17:17:31 +02:00
// AddFilter adds a filter to the filter dictionary.
func (d *filterDictionary) AddFilter(name string, fn interface{}) {
2017-06-27 03:32:08 +02:00
rf := reflect.ValueOf(fn)
2017-06-27 15:17:57 +02:00
switch {
case rf.Kind() != reflect.Func:
panic(fmt.Errorf("a filter must be a function"))
case rf.Type().NumIn() < 1:
panic(fmt.Errorf("a filter function must have at least one input"))
case rf.Type().NumOut() > 2:
panic(fmt.Errorf("a filter must be have one or two outputs"))
// case rf.Type().Out(1).Implements(…):
// panic(fmt.Errorf("a filter's second output must be type error"))
2017-06-27 03:32:08 +02:00
}
2017-06-30 22:13:18 +02:00
d.filters[name] = fn
2017-06-27 00:55:12 +02:00
}
2017-07-05 17:17:31 +02:00
var closureType = reflect.TypeOf(closure{})
var interfaceType = reflect.TypeOf([]interface{}{}).Elem()
func isClosureInterfaceType(t reflect.Type) bool {
return closureType.ConvertibleTo(t) && !interfaceType.ConvertibleTo(t)
}
2017-07-05 17:17:31 +02:00
func (ctx *context) ApplyFilter(name string, receiver valueFn, params []valueFn) interface{} {
filter, ok := ctx.filters[name]
2017-06-27 00:55:12 +02:00
if !ok {
2017-06-29 13:54:31 +02:00
panic(UndefinedFilter(name))
2017-06-27 00:55:12 +02:00
}
2017-06-30 22:13:18 +02:00
fr := reflect.ValueOf(filter)
2017-07-05 17:17:31 +02:00
args := []interface{}{receiver(ctx)}
2017-06-30 22:13:18 +02:00
for i, param := range params {
if i+1 < fr.Type().NumIn() && isClosureInterfaceType(fr.Type().In(i+1)) {
expr, err := Parse(param(ctx).(string))
if err != nil {
panic(err)
}
2017-06-30 22:13:18 +02:00
args = append(args, closure{expr, ctx})
} else {
args = append(args, param(ctx))
2017-06-27 03:32:08 +02:00
}
2017-06-30 22:13:18 +02:00
}
out, err := generics.Call(fr, args)
if err != nil {
panic(err)
}
2017-07-03 18:00:43 +02:00
out = ToLiquid(out)
2017-06-30 22:13:18 +02:00
switch out := out.(type) {
case []byte:
return string(out)
default:
return out
2017-06-27 00:55:12 +02:00
}
}