diff options
Diffstat (limited to 'vendor/src/github.com/alexedwards/stack/context.go')
| -rw-r--r-- | vendor/src/github.com/alexedwards/stack/context.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/vendor/src/github.com/alexedwards/stack/context.go b/vendor/src/github.com/alexedwards/stack/context.go new file mode 100644 index 0000000..07afe21 --- /dev/null +++ b/vendor/src/github.com/alexedwards/stack/context.go @@ -0,0 +1,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 +} |
