2017-06-26 21:36:05 +02:00
|
|
|
package expressions
|
|
|
|
|
|
|
|
// Context is the expression evaluation context. It maps variables names to values.
|
|
|
|
type Context interface {
|
|
|
|
Get(string) interface{}
|
|
|
|
Set(string, interface{})
|
2017-07-02 05:52:23 +02:00
|
|
|
Filters() *filterDictionary
|
2017-06-26 21:36:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type context struct {
|
2017-07-01 03:05:48 +02:00
|
|
|
bindings map[string]interface{}
|
2017-06-30 22:13:18 +02:00
|
|
|
Settings
|
|
|
|
}
|
|
|
|
|
2017-07-02 06:10:54 +02:00
|
|
|
// Settings holds configuration information for expression interpretation.
|
2017-06-30 22:13:18 +02:00
|
|
|
type Settings struct {
|
2017-07-02 05:52:23 +02:00
|
|
|
filters *filterDictionary
|
2017-06-30 22:13:18 +02:00
|
|
|
}
|
|
|
|
|
2017-07-02 06:10:54 +02:00
|
|
|
// NewSettings creates a new Settings.
|
2017-06-30 22:13:18 +02:00
|
|
|
func NewSettings() Settings {
|
2017-07-02 05:52:23 +02:00
|
|
|
return Settings{newFilterDictionary()}
|
2017-06-30 22:13:18 +02:00
|
|
|
}
|
|
|
|
|
2017-07-02 06:10:54 +02:00
|
|
|
// AddFilter adds a filter function to settings.
|
2017-06-30 22:13:18 +02:00
|
|
|
func (s Settings) AddFilter(name string, fn interface{}) {
|
2017-07-02 05:52:23 +02:00
|
|
|
s.filters.addFilter(name, fn)
|
2017-06-26 21:36:05 +02:00
|
|
|
}
|
|
|
|
|
2017-06-27 19:36:38 +02:00
|
|
|
// NewContext makes a new expression evaluation context.
|
2017-06-30 22:13:18 +02:00
|
|
|
func NewContext(vars map[string]interface{}, s Settings) Context {
|
2017-07-01 03:05:48 +02:00
|
|
|
return &context{vars, s}
|
2017-06-30 22:13:18 +02:00
|
|
|
}
|
|
|
|
|
2017-07-02 05:52:23 +02:00
|
|
|
func (c *context) Filters() *filterDictionary {
|
2017-06-30 22:13:18 +02:00
|
|
|
return c.filters
|
2017-06-26 21:36:05 +02:00
|
|
|
}
|
|
|
|
|
2017-06-27 19:36:38 +02:00
|
|
|
// Get looks up a variable value in the expression context.
|
2017-06-26 21:36:05 +02:00
|
|
|
func (c *context) Get(name string) interface{} {
|
2017-07-01 03:05:48 +02:00
|
|
|
return c.bindings[name]
|
2017-06-26 21:36:05 +02:00
|
|
|
}
|
|
|
|
|
2017-06-27 19:36:38 +02:00
|
|
|
// Set sets a variable value in the expression context.
|
2017-06-26 21:36:05 +02:00
|
|
|
func (c *context) Set(name string, value interface{}) {
|
2017-07-01 03:05:48 +02:00
|
|
|
c.bindings[name] = value
|
2017-06-26 21:36:05 +02:00
|
|
|
}
|