util/context/attributes.go
crusader ea2b6398a7 ing
2017-11-27 14:57:26 +09:00

70 lines
1.3 KiB
Go

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.Destroy()
}
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
}
}