...

Source file src/github.com/noirbizarre/gonja/exec/filters.go

Documentation: github.com/noirbizarre/gonja/exec

     1  package exec
     2  
     3  import (
     4  	"github.com/pkg/errors"
     5  
     6  	"github.com/noirbizarre/gonja/nodes"
     7  )
     8  
     9  // FilterFunction is the type filter functions must fulfil
    10  type FilterFunction func(e *Evaluator, in *Value, params *VarArgs) *Value
    11  
    12  type FilterSet map[string]FilterFunction
    13  
    14  // Exists returns true if the given filter is already registered
    15  func (fs FilterSet) Exists(name string) bool {
    16  	_, existing := fs[name]
    17  	return existing
    18  }
    19  
    20  // Register registers a new filter. If there's already a filter with the same
    21  // name, Register will panic. You usually want to call this
    22  // function in the filter's init() function:
    23  // http://golang.org/doc/effective_go.html#init
    24  //
    25  // See http://www.florian-schlachter.de/post/gonja/ for more about
    26  // writing filters and tags.
    27  func (fs *FilterSet) Register(name string, fn FilterFunction) error {
    28  	if fs.Exists(name) {
    29  		return errors.Errorf("filter with name '%s' is already registered", name)
    30  	}
    31  	(*fs)[name] = fn
    32  	return nil
    33  }
    34  
    35  // Replace replaces an already registered filter with a new implementation. Use this
    36  // function with caution since it allows you to change existing filter behaviour.
    37  func (fs *FilterSet) Replace(name string, fn FilterFunction) error {
    38  	if !fs.Exists(name) {
    39  		return errors.Errorf("filter with name '%s' does not exist (therefore cannot be overridden)", name)
    40  	}
    41  	(*fs)[name] = fn
    42  	return nil
    43  }
    44  
    45  func (fs *FilterSet) Update(other FilterSet) FilterSet {
    46  	for name, filter := range other {
    47  		(*fs)[name] = filter
    48  	}
    49  	return *fs
    50  }
    51  
    52  // EvaluateFiltered evaluate a filtered expression
    53  func (e *Evaluator) EvaluateFiltered(expr *nodes.FilteredExpression) *Value {
    54  	value := e.Eval(expr.Expression)
    55  
    56  	for _, filter := range expr.Filters {
    57  		value = e.ExecuteFilter(filter, value)
    58  		if value.IsError() {
    59  			return AsValue(errors.Wrapf(value, `Unable to evaluate filter %s`, filter))
    60  		}
    61  	}
    62  
    63  	// if value.IsError() {
    64  	// 	return AsValue(errors.Wrapf(value, `Unable to filter chain`, expr.Expression))
    65  	// }
    66  	return value
    67  }
    68  
    69  // ExecuteFilter execute a filter node
    70  func (e *Evaluator) ExecuteFilter(fc *nodes.FilterCall, v *Value) *Value {
    71  	params := NewVarArgs()
    72  
    73  	for _, param := range fc.Args {
    74  		value := e.Eval(param)
    75  		if value.IsError() {
    76  			return AsValue(errors.Wrapf(value, `Unable to evaluate parameter %s`, param))
    77  		}
    78  		params.Args = append(params.Args, value)
    79  	}
    80  
    81  	for key, param := range fc.Kwargs {
    82  		value := e.Eval(param)
    83  		if value.IsError() {
    84  			return AsValue(errors.Wrapf(value, `Unable to evaluate parameter %s=%s`, key, param))
    85  		}
    86  		params.KwArgs[key] = value
    87  	}
    88  	return e.ExecuteFilterByName(fc.Name, v, params)
    89  }
    90  
    91  // ExecuteFilterByName execute a filter given its name
    92  func (e *Evaluator) ExecuteFilterByName(name string, in *Value, params *VarArgs) *Value {
    93  	if !e.Filters.Exists(name) {
    94  		return AsValue(errors.Errorf(`Filter "%s" not found`, name))
    95  	}
    96  	fn, _ := (*e.Filters)[name]
    97  
    98  	return fn(e, in, params)
    99  }
   100  

View as plain text