-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
51 lines (45 loc) · 968 Bytes
/
context.go
File metadata and controls
51 lines (45 loc) · 968 Bytes
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
package deliver
type Context struct {
Keys map[string]interface{}
}
// Initialize new context.
func NewContext() *Context {
return &Context{}
}
// Sets new key/value pair.
// Initializes a new hash table in case not already specified.
func (c *Context) Set(key string, item interface{}) {
if c.Keys == nil {
c.Keys = make(map[string]interface{})
}
c.Keys[key] = item
}
// Returns value for the given key.
func (c *Context) Get(key string) interface{} {
if c.Keys != nil {
value, ok := c.Keys[key]
if ok {
return value
}
}
return nil
}
// Returns value for the given key.
// Returns error in case the key does not exist.
func (c *Context) GetOk(key string) (interface{}, bool) {
if c.Keys != nil {
value, ok := c.Keys[key]
if ok {
return value, ok
}
}
return nil, false
}
// Does context have the given key specified.
func (c *Context) Has(key string) bool {
if c.Keys != nil {
_, ok := c.Keys[key]
return ok
}
return false
}