This commit is contained in:
crusader 2017-11-28 01:05:04 +09:00
parent e115c8a9b6
commit 653262bd7e

View File

@ -1,6 +1,9 @@
package context package context
import "sync" import (
"reflect"
"sync"
)
type ContextKey string type ContextKey string
@ -12,22 +15,21 @@ func NewContext(parent Context) Context {
c := &defaultContext{ c := &defaultContext{
Context: parent, Context: parent,
} }
c.attributes = make(map[string]interface{}) c.attributes = make(map[interface{}]interface{})
return c return c
} }
type Context interface { type Context interface {
Parent() Context Parent() Context
SetAttribute(key string, value interface{}) SetAttribute(key interface{}, value interface{})
GetAttribute(key string) (value interface{}) GetAttribute(key interface{}) (value interface{})
RemoveAttribute(key string) RemoveAttribute(key interface{})
ContainsAttribute(key string) (exist bool) ContainsAttribute(key interface{}) (exist bool)
} }
type defaultContext struct { type defaultContext struct {
Context Context
attributes map[string]interface{} attributes map[interface{}]interface{}
mtx sync.RWMutex mtx sync.RWMutex
} }
@ -36,16 +38,23 @@ func (dc *defaultContext) Parent() Context {
return dc.Context return dc.Context
} }
func (dc *defaultContext) SetAttribute(key string, value interface{}) { func (dc *defaultContext) SetAttribute(key interface{}, value interface{}) {
dc.checkInitialized() dc.checkInitialized()
if key == nil {
panic("nil key")
}
if !reflect.TypeOf(key).Comparable() {
panic("key is not comparable")
}
dc.mtx.Lock() dc.mtx.Lock()
defer dc.mtx.Unlock() defer dc.mtx.Unlock()
dc.attributes[key] = value dc.attributes[key] = value
} }
func (dc *defaultContext) GetAttribute(key string) (value interface{}) { func (dc *defaultContext) GetAttribute(key interface{}) (value interface{}) {
dc.checkInitialized() dc.checkInitialized()
dc.mtx.RLock() dc.mtx.RLock()
@ -61,7 +70,7 @@ func (dc *defaultContext) GetAttribute(key string) (value interface{}) {
return dc.Context.GetAttribute(key) return dc.Context.GetAttribute(key)
} }
func (dc *defaultContext) RemoveAttribute(key string) { func (dc *defaultContext) RemoveAttribute(key interface{}) {
dc.checkInitialized() dc.checkInitialized()
dc.mtx.Lock() dc.mtx.Lock()
@ -79,7 +88,7 @@ func (dc *defaultContext) RemoveAttribute(key string) {
dc.Context.RemoveAttribute(key) dc.Context.RemoveAttribute(key)
} }
func (dc *defaultContext) ContainsAttribute(key string) (exist bool) { func (dc *defaultContext) ContainsAttribute(key interface{}) (exist bool) {
dc.checkInitialized() dc.checkInitialized()
dc.mtx.RLock() dc.mtx.RLock()