forked from oleiade/trousseau
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv.go
78 lines (61 loc) · 1.73 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package trousseau
import (
"errors"
"fmt"
"sort"
)
type KVStore map[string]interface{}
// Get method fetches a key from the trousseau file store
func (kvs *KVStore) Get(key string) (interface{}, error) {
value, ok := (*kvs)[key]
if !ok {
return "", errors.New(fmt.Sprintf("Key %s does not exist", key))
}
return value, nil
}
// Set method sets a key value pair in the store
func (kvs *KVStore) Set(key string, value interface{}) {
(*kvs)[key] = value
}
// Del deletes a key value pair from the trousseau file
func (kvs *KVStore) Del(key string) {
delete((*kvs), key)
}
// Rename a data store key to dest. If overwrite parameter
// is provided with a true value, any existing destination key-value
// pair will be overriden, otherwise (false) an error will be returned.
func (kvs *KVStore) Rename(src, dest string, overwrite bool) error {
srcValue, ok := (*kvs)[src]
if !ok {
return errors.New(fmt.Sprintf("Source key %s does not exist", src))
}
// If destination key already exists, and overwrite flag is
// set to false, then return an error
_, ok = (*kvs)[dest]
if ok && overwrite == false {
return errors.New(fmt.Sprintf("Destination key %s already exists an overwrite flag was not provided.", dest))
}
// Otherwise update dest value
kvs.Set(dest, srcValue)
kvs.Del(src)
return nil
}
// Keys lists the keys contained in the trousseau store file
func (kvs *KVStore) Keys() []string {
index := 0
keys := make([]string, len((*kvs)))
for key, _ := range *kvs {
keys[index] = key
index++
}
// Sort in alphabetical order
sort.Strings(keys)
return keys
}
func (kvs *KVStore) Items() map[string]interface{} {
items := make(map[string]interface{})
for key, value := range *kvs {
items[key] = value
}
return items
}