util/context/attributes.go

69 lines
1.3 KiB
Go
Raw Normal View History

2017-11-27 05:53:04 +00:00
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
}
}