-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
57 lines (48 loc) · 1.27 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
package juliet
import (
"fmt"
)
// Context hold a map[interface{}]interface{} to pass along the middleware chain.
type Context struct {
values map[interface{}]interface{}
}
// NewContext create a new context instance.
func NewContext() (ctx *Context) {
ctx = new(Context)
ctx.values = make(map[interface{}]interface{})
return
}
// Get return the value matching the key from the context.
func (ctx *Context) Get(key interface{}) (value interface{}, ok bool) {
value, ok = ctx.values[key]
return
}
// Set add a value to the context or overrides a parent value.
func (ctx *Context) Set(key interface{}, val interface{}) {
ctx.values[key] = val
}
// Delete remove a value from the context.
func (ctx *Context) Delete(key interface{}) {
delete(ctx.values, key)
}
// Clear remove all values from the context.
func (ctx *Context) Clear() {
for key := range ctx.values {
delete(ctx.values, key)
}
}
// Copy create a new copy of the context.
func (ctx *Context) Copy() *Context {
nc := NewContext()
for key, value := range ctx.values {
nc.values[key] = value
}
return nc
}
// String return a string representation of the context values.
func (ctx *Context) String() (str string) {
for key, value := range ctx.values {
str += fmt.Sprintf("%v => %v\n", key, value)
}
return
}