-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathcontext.go
More file actions
129 lines (106 loc) · 2.71 KB
/
context.go
File metadata and controls
129 lines (106 loc) · 2.71 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package plush
import (
"context"
"sync"
"github.com/gobuffalo/plush/v5/helpers/hctx"
)
var _ context.Context = &Context{}
// Context holds all of the data for the template that is being rendered.
type Context struct {
context.Context
data *SymbolTable
outer *Context
moot *sync.RWMutex
}
// 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() hctx.Context {
cc := NewContextWithOuter(map[string]interface{}{}, c)
return cc
}
// Set a value onto the context
func (c *Context) Set(key string, value interface{}) {
c.moot.Lock()
defer c.moot.Unlock()
c.data.Declare(key, value)
}
func (c *Context) Update(key string, value interface{}) bool {
c.moot.Lock()
defer c.moot.Unlock()
return c.data.Assign(key, value)
}
// Value from the context, or it's parent's context if one exists.
func (c *Context) Value(key interface{}) interface{} {
c.moot.RLock()
defer c.moot.RUnlock()
if s, ok := key.(string); ok {
gg, ok := c.data.Resolve(s)
if ok {
return gg
}
}
return c.Context.Value(key)
}
// Has checks the existence of the key in the context.
func (c *Context) Has(key string) bool {
c.moot.RLock()
defer c.moot.RUnlock()
return c.data.Has(key)
}
// Export all the known values in the context.
// Note this can't reach up into other implemenations
// of context.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
}
}
return m
}
// NewContext returns a fully formed context ready to go
func NewContext() *Context {
return NewContextWith(map[string]interface{}{})
}
// NewContextWith returns a fully formed context using the data
// provided.
func NewContextWith(data map[string]interface{}) *Context {
c := &Context{
Context: context.Background(),
data: NewScope(nil),
outer: nil,
moot: &sync.RWMutex{},
}
for k, v := range data {
c.Set(k, v)
}
for k, v := range Helpers.All() {
if !c.Has(k) {
c.Set(k, v)
}
}
return c
}
// NewContextWith returns a fully formed context using the data
// provided and setting the outer context with the passed
// seccond argument.
func NewContextWithOuter(data map[string]interface{}, out *Context) *Context {
c := &Context{
Context: context.Background(),
data: NewScope(out.data),
outer: out,
moot: &sync.RWMutex{},
}
for k, v := range data {
c.Set(k, v)
}
return c
}
// NewContextWithContext returns a new plush.Context given another context
func NewContextWithContext(ctx context.Context) *Context {
c := NewContext()
c.Context = ctx
return c
}