...
1 package exec
2
3 type Context struct {
4 data map[string]interface{}
5 parent *Context
6 }
7
8 func NewContext(data map[string]interface{}) *Context {
9 return &Context{data: data}
10 }
11
12 func EmptyContext() *Context {
13 return &Context{data: map[string]interface{}{}}
14 }
15
16 func (ctx *Context) Has(name string) bool {
17 _, exists := ctx.data[name]
18 if !exists && ctx.parent != nil {
19 return ctx.parent.Has(name)
20 }
21 return exists
22 }
23
24 func (ctx *Context) Get(name string) interface{} {
25 value, exists := ctx.data[name]
26 if exists {
27 return value
28 } else if ctx.parent != nil {
29 return ctx.parent.Get(name)
30 } else {
31 return nil
32 }
33 }
34
35 func (ctx *Context) Set(name string, value interface{}) {
36 ctx.data[name] = value
37 }
38
39 func (ctx *Context) Inherit() *Context {
40 return &Context{
41 data: map[string]interface{}{},
42 parent: ctx,
43 }
44 }
45
46
47 func (ctx *Context) Update(other map[string]interface{}) *Context {
48 for k, v := range other {
49 ctx.data[k] = v
50 }
51 return ctx
52 }
53
54
55 func (ctx *Context) Merge(other *Context) *Context {
56 return ctx.Update(other.data)
57 }
58
View as plain text