forked from tuna/tunasync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
61 lines (51 loc) · 1.42 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
package worker
// Context object aims to store runtime configurations
import "errors"
// A Context object is a layered key-value storage
// when enters a context, the changes to the storage would be stored
// in a new layer and when exits, the top layer poped and the storage
// returned to the state before entering this context
type Context struct {
parent *Context
store map[string]interface{}
}
// NewContext returns a new context object
func NewContext() *Context {
return &Context{
parent: nil,
store: make(map[string]interface{}),
}
}
// Enter generates a new layer of context
func (ctx *Context) Enter() *Context {
return &Context{
parent: ctx,
store: make(map[string]interface{}),
}
}
// Exit return the upper layer of context
func (ctx *Context) Exit() (*Context, error) {
if ctx.parent == nil {
return nil, errors.New("Cannot exit the bottom layer context")
}
return ctx.parent, nil
}
// Get returns the value corresponding to key, if it's
// not found in the current layer, return the lower layer
// context's value
func (ctx *Context) Get(key string) (interface{}, bool) {
if ctx.parent == nil {
if value, ok := ctx.store[key]; ok {
return value, true
}
return nil, false
}
if value, ok := ctx.store[key]; ok {
return value, true
}
return ctx.parent.Get(key)
}
// Set sets the value to the key at current layer
func (ctx *Context) Set(key string, value interface{}) {
ctx.store[key] = value
}