2017-07-14 02:18:23 +02:00
|
|
|
package expressions
|
2017-06-26 21:36:05 +02:00
|
|
|
|
2022-01-06 18:43:17 +01:00
|
|
|
import "github.com/danog/liquid/values"
|
2017-07-28 00:13:39 +02:00
|
|
|
|
2017-06-26 21:36:05 +02:00
|
|
|
// Context is the expression evaluation context. It maps variables names to values.
|
|
|
|
type Context interface {
|
2017-07-16 19:47:06 +02:00
|
|
|
ApplyFilter(string, valueFn, []valueFn) (interface{}, error)
|
2017-07-09 16:36:32 +02:00
|
|
|
// Clone returns a copy with a new variable binding map
|
|
|
|
// (so that copy.Set does effect the source context.)
|
|
|
|
Clone() Context
|
2017-06-26 21:36:05 +02:00
|
|
|
Get(string) interface{}
|
|
|
|
Set(string, interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type context struct {
|
2017-07-04 17:08:57 +02:00
|
|
|
Config
|
2017-07-05 17:17:31 +02:00
|
|
|
bindings map[string]interface{}
|
2017-06-26 21:36:05 +02:00
|
|
|
}
|
|
|
|
|
2017-06-27 19:36:38 +02:00
|
|
|
// NewContext makes a new expression evaluation context.
|
2017-07-09 16:36:32 +02:00
|
|
|
func NewContext(vars map[string]interface{}, cfg Config) Context {
|
|
|
|
return &context{cfg, vars}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) Clone() Context {
|
|
|
|
bindings := map[string]interface{}{}
|
|
|
|
for k, v := range c.bindings {
|
|
|
|
bindings[k] = v
|
|
|
|
}
|
|
|
|
return &context{c.Config, bindings}
|
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-28 00:13:39 +02:00
|
|
|
return values.ToLiquid(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
|
|
|
}
|