1
0
mirror of https://github.com/danog/liquid.git synced 2024-12-11 17:39:39 +01:00
liquid/expressions/filters.go

85 lines
2.2 KiB
Go
Raw Normal View History

2017-06-27 00:55:12 +02:00
package expressions
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-02 05:52:23 +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-02 05:52:23 +02:00
// addFilter defines a filter.
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
}
func isClosureInterfaceType(t reflect.Type) bool {
closureType := reflect.TypeOf(closure{})
interfaceType := reflect.TypeOf([]interface{}{}).Elem()
return closureType.ConvertibleTo(t) && !interfaceType.ConvertibleTo(t)
}
2017-07-02 05:52:23 +02:00
func (d *filterDictionary) runFilter(ctx Context, f valueFn, name string, params []valueFn) interface{} {
2017-06-30 22:13:18 +02:00
filter, ok := d.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)
args := []interface{}{f(ctx)}
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)
}
switch out := out.(type) {
case []byte:
return string(out)
default:
return out
2017-06-27 00:55:12 +02:00
}
}