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 + } +}