Implement namespace optional arg

This commit is contained in:
Bel LaPointe
2019-03-20 09:54:26 -06:00
parent b79cb97ba6
commit b7a231feaf
10 changed files with 101 additions and 45 deletions

30
map.go
View File

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