-
Notifications
You must be signed in to change notification settings - Fork 11
/
context.go
79 lines (69 loc) · 1.71 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package velvet
// Context holds all of the data for the template that is being rendered.
type Context struct {
data map[string]interface{}
options map[string]interface{}
outer *Context
}
func (c *Context) export() map[string]interface{} {
m := map[string]interface{}{}
if c.outer != nil {
for k, v := range c.outer.export() {
m[k] = v
}
}
for k, v := range c.data {
m[k] = v
}
if c.options != nil {
for k, v := range c.options {
m[k] = v
}
}
return m
}
// New context containing the current context. Values set on the new context
// will not be set onto the original context, however, the original context's
// values will be available to the new context.
func (c *Context) New() *Context {
cc := NewContext()
cc.outer = c
return cc
}
// Set a value onto the context
func (c *Context) Set(key string, value interface{}) {
c.data[key] = value
}
// Get a value from the context, or it's parent's context if one exists.
func (c *Context) Get(key string) interface{} {
if v, ok := c.data[key]; ok {
return v
}
if c.outer != nil {
return c.outer.Get(key)
}
return nil
}
// Has checks the existence of the key in the context.
func (c *Context) Has(key string) bool {
return c.Get(key) != nil
}
// Options are the values passed into a helper.
func (c *Context) Options() map[string]interface{} {
return c.options
}
// NewContext returns a fully formed context ready to go
func NewContext() *Context {
return &Context{
data: map[string]interface{}{},
options: map[string]interface{}{},
outer: nil,
}
}
// NewContextWith returns a fully formed context using the data
// provided.
func NewContextWith(data map[string]interface{}) *Context {
c := NewContext()
c.data = data
return c
}