From b221ead4e44672fdac42ce0070bbb8b3bbf7738d Mon Sep 17 00:00:00 2001 From: crusader Date: Mon, 27 Nov 2017 14:53:04 +0900 Subject: [PATCH] Attribute Manager has been added. --- context/attributes.go | 68 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 context/attributes.go diff --git a/context/attributes.go b/context/attributes.go new file mode 100644 index 0000000..d440050 --- /dev/null +++ b/context/attributes.go @@ -0,0 +1,68 @@ +package context + +type AttributeManager interface { + Init() + SetAttribute(key string, value interface{}) + GetAttribute(key string) (value interface{}) + GetAttributeNames() []string + RemoveAttribute(key string) + Contains(key string) (exist bool) + Destroy() +} + +type AttributeManagers struct { + attributes map[string]interface{} +} + +func (am *AttributeManagers) Init() { + if nil == am.attributes { + am.attributes = make(map[string]interface{}) + } +} + +func (am *AttributeManagers) SetAttribute(key string, value interface{}) { + if nil == am.attributes { + am.attributes = make(map[string]interface{}) + } + am.attributes[key] = value +} + +func (am *AttributeManagers) GetAttribute(key string) (value interface{}) { + if nil == am.attributes { + return nil + } + return am.attributes[key] +} + +func (am *AttributeManagers) GetAttributeNames() []string { + if nil == am.attributes { + return nil + } + keys := []string{} + for k, _ := range am.attributes { + keys = append(keys, k) + } + + return keys +} + +func (am *AttributeManagers) RemoveAttribute(key string) { + if nil == am.attributes { + return + } + delete(am.attributes, key) +} + +func (am *AttributeManagers) Contains(key string) (exist bool) { + if nil == am.attributes { + return false + } + _, ok := am.attributes[key] + return ok +} + +func (am *AttributeManagers) Destroy() { + if nil == am.attributes { + am.attributes = nil + } +}