From 0eefc4bd276d4819c5193da371ab4c4faa1d0261 Mon Sep 17 00:00:00 2001 From: crusader Date: Mon, 27 Nov 2017 19:51:03 +0900 Subject: [PATCH] ing --- context/attributes.go | 104 ------------------------------------------ context/context.go | 90 ++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 104 deletions(-) delete mode 100644 context/attributes.go diff --git a/context/attributes.go b/context/attributes.go deleted file mode 100644 index 2ecd244..0000000 --- a/context/attributes.go +++ /dev/null @@ -1,104 +0,0 @@ -package context - -import "sync" - -type AttributeManager interface { - Attributes() Attributes -} - -type Attributes interface { - initialize() - Set(key string, value interface{}) - Get(key string) (value interface{}) - Remove(key string) - Contains(key string) (exist bool) -} - -func NewAttributes(parent Attributes) Attributes { - am := &attributes{ - Parent: parent, - } - am.initialize() - - return am -} - -type attributes struct { - Parent Attributes - values map[string]interface{} - - mtx sync.RWMutex -} - -func (am *attributes) initialize() { - if nil != am.Parent { - am.Parent.initialize() - } - - am.values = make(map[string]interface{}) -} - -func (am *attributes) Set(key string, value interface{}) { - am.checkInitialized() - - am.mtx.Lock() - defer am.mtx.Unlock() - - am.values[key] = value -} - -func (am *attributes) Get(key string) (value interface{}) { - am.checkInitialized() - - am.mtx.RLock() - defer am.mtx.RUnlock() - - if _, ok := am.values[key]; ok { - return am.values[key] - } - - if nil == am.Parent { - return nil - } - return am.Parent.Get(key) -} - -func (am *attributes) Remove(key string) { - am.checkInitialized() - - am.mtx.Lock() - defer am.mtx.Unlock() - - if _, ok := am.values[key]; ok { - delete(am.values, key) - return - } - - if nil == am.Parent { - return - } - - am.Parent.Remove(key) -} - -func (am *attributes) Contains(key string) (exist bool) { - am.checkInitialized() - - am.mtx.RLock() - defer am.mtx.RUnlock() - - if _, ok := am.values[key]; ok { - return true - } - - if nil == am.Parent { - return false - } - return am.Parent.Contains(key) -} - -func (am *attributes) checkInitialized() { - if nil == am.values { - panic("Attribute Manager: must be initialized") - } -} diff --git a/context/context.go b/context/context.go index af2a83c..a0ef5d4 100644 --- a/context/context.go +++ b/context/context.go @@ -1,7 +1,97 @@ package context +import "sync" + type ContextKey string func (c ContextKey) String() string { return string(c) } + +func NewContext(parent Context) Context { + c := &defaultContext{ + parent: parent, + } + c.attributes = make(map[string]interface{}) + + return c +} + +type Context interface { + SetAttribute(key string, value interface{}) + GetAttribute(key string) (value interface{}) + RemoveAttribute(key string) + ContainsAttribute(key string) (exist bool) +} + +type defaultContext struct { + parent Context + attributes map[string]interface{} + + mtx sync.RWMutex +} + +func (dc *defaultContext) SetAttribute(key string, value interface{}) { + dc.checkInitialized() + + dc.mtx.Lock() + defer dc.mtx.Unlock() + + dc.attributes[key] = value +} + +func (dc *defaultContext) GetAttribute(key string) (value interface{}) { + dc.checkInitialized() + + dc.mtx.RLock() + defer dc.mtx.RUnlock() + + if _, ok := dc.attributes[key]; ok { + return dc.attributes[key] + } + + if nil == dc.parent { + return nil + } + return dc.parent.GetAttribute(key) +} + +func (dc *defaultContext) RemoveAttribute(key string) { + dc.checkInitialized() + + dc.mtx.Lock() + defer dc.mtx.Unlock() + + if _, ok := dc.attributes[key]; ok { + delete(dc.attributes, key) + return + } + + if nil == dc.parent { + return + } + + dc.parent.RemoveAttribute(key) +} + +func (dc *defaultContext) ContainsAttribute(key string) (exist bool) { + dc.checkInitialized() + + dc.mtx.RLock() + defer dc.mtx.RUnlock() + + if _, ok := dc.attributes[key]; ok { + return true + } + + if nil == dc.parent { + return false + } + return dc.parent.ContainsAttribute(key) +} + +func (dc *defaultContext) checkInitialized() { + if nil == dc.attributes { + panic("Attribute Manager: must be initialized") + } +}