aboutsummaryrefslogtreecommitdiff
path: root/vendor/src/github.com/alexedwards/stack/context.go
blob: 07afe2122707cdc81dd821383f651322af7c2561 (plain)
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
package stack

import (
	"sync"
)

type Context struct {
	mu sync.RWMutex
	m  map[string]interface{}
}

func NewContext() *Context {
	m := make(map[string]interface{})
	return &Context{m: m}
}

func (c *Context) Get(key string) interface{} {
	if !c.Exists(key) {
		return nil
	}
	c.mu.RLock()
	defer c.mu.RUnlock()
	return c.m[key]
}

func (c *Context) Put(key string, val interface{}) *Context {
	c.mu.Lock()
	defer c.mu.Unlock()
	c.m[key] = val
	return c
}

func (c *Context) Delete(key string) *Context {
	c.mu.Lock()
	defer c.mu.Unlock()
	delete(c.m, key)
	return c
}

func (c *Context) Exists(key string) bool {
	c.mu.RLock()
	defer c.mu.RUnlock()
	_, ok := c.m[key]
	return ok
}

func (c *Context) copy() *Context {
	nc := NewContext()
	c.mu.RLock()
	defer c.mu.RUnlock()
	for k, v := range c.m {
		nc.m[k] = v
	}
	return nc
}