Implement namespace optional arg
This commit is contained in:
30
map.go
30
map.go
@@ -4,10 +4,10 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Map map[string][]byte
|
||||
type Map map[string]map[string][]byte
|
||||
|
||||
func NewMap() *Map {
|
||||
m := make(map[string][]byte)
|
||||
m := make(map[string]map[string][]byte)
|
||||
n := Map(m)
|
||||
return &n
|
||||
}
|
||||
@@ -28,20 +28,30 @@ func (m *Map) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Map) Get(key string) ([]byte, error) {
|
||||
if _, ok := (*m)[key]; !ok {
|
||||
func (m *Map) Get(key string, ns ...string) ([]byte, error) {
|
||||
namespace := resolveNamespace(ns)
|
||||
if _, ok := (*m)[namespace]; !ok {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return (*m)[key], nil
|
||||
if _, ok := (*m)[namespace][key]; !ok {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return (*m)[namespace][key], nil
|
||||
}
|
||||
|
||||
func (m *Map) Set(key string, value []byte) error {
|
||||
func (m *Map) Set(key string, value []byte, ns ...string) error {
|
||||
namespace := resolveNamespace(ns)
|
||||
if value == nil {
|
||||
if _, ok := (*m)[key]; ok {
|
||||
delete(*m, key)
|
||||
if _, ok := (*m)[namespace]; !ok {
|
||||
return nil
|
||||
} else if _, ok := (*m)[namespace][key]; ok {
|
||||
delete((*m)[namespace], key)
|
||||
}
|
||||
return nil
|
||||
} else {
|
||||
if _, ok := (*m)[namespace]; !ok {
|
||||
(*m)[namespace] = make(map[string][]byte)
|
||||
}
|
||||
(*m)[namespace][key] = value
|
||||
}
|
||||
(*m)[key] = value
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user