-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkv.go
56 lines (46 loc) · 1.2 KB
/
kv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package uinit
import "fmt"
var _ KeyValue = (*SimpleKV)(nil)
// NewSimpleKV creates a new, initialized SimpleKV
func NewSimpleKV() (kv *SimpleKV) {
kv = &SimpleKV{}
kv.Init()
return
}
// SimpleKV just provides an interface to a map
type SimpleKV struct {
store map[string]string
}
// Init performs initializes internal data for the store
func (kv *SimpleKV) Init() {
kv.store = make(map[string]string)
}
// Set sets a named value in a map (will override)
func (kv *SimpleKV) Set(name, val string) (err error) {
kv.store[name] = val
return
}
// Get tries to get a named value, returns an error if it doesn't exist
func (kv *SimpleKV) Get(name string) (val string, err error) {
var ok bool
if val, ok = kv.store[name]; !ok {
return "", fmt.Errorf("no such key: %s", name)
}
return
}
// GetDefault tries to get a named value, sets to dval if it doesn't exist
func (kv *SimpleKV) GetDefault(name, dval string) string {
if val, err := kv.Get(name); err != nil {
return val
}
return dval
}
// Clear clears the kv store
func (kv *SimpleKV) Clear() (err error) {
kv.Init()
return
}
// GetMap returns a map that can be used for templating
func (kv *SimpleKV) GetMap() map[string]string {
return kv.store
}