1
0
mirror of https://github.com/danog/liquid.git synced 2024-12-02 15:57:48 +01:00
liquid/expressions/context.go

33 lines
786 B
Go
Raw Normal View History

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 {
bindings map[string]interface{}
2017-07-04 17:08:57 +02:00
Config
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-04 17:08:57 +02:00
func NewContext(vars map[string]interface{}, s Config) Context {
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-03 18:00:43 +02:00
return 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{}) {
c.bindings[name] = value
2017-06-26 21:36:05 +02:00
}