-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkv.go
55 lines (48 loc) · 1.17 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
package coreapi
import (
"encoding/json"
)
type ModuleKV struct {
rf requestFunc
}
// Put the value to the storage
func (kv ModuleKV) Put(key, value string) error {
_, err := kv.rf("kv/put/"+key, "text/plain", []byte(value))
return err
}
// Upsert the value in the storage
func (kv ModuleKV) Upsert(key, value string) error {
_, err := kv.rf("kv/upsert/"+key, "text/plain", []byte(value))
return err
}
// Delete the value from the storage
func (kv ModuleKV) Delete(key string) error {
_, err := kv.rf("kv/delete/"+key, "", nil)
return err
}
// Get a value from the storage
func (kv ModuleKV) Get(key string) (string, error) {
resp, err := kv.rf("kv/get/"+key, "", nil)
if err != nil {
return "", err
}
var result string
errUnmarshal := json.Unmarshal(resp, &result)
if errUnmarshal != nil {
return "", errUnmarshal
}
return result, nil
}
// All returns all the values from the storage
func (kv ModuleKV) All() (map[string]string, error) {
resp, err := kv.rf("kv/all", "", nil)
if err != nil {
return nil, err
}
result := map[string]string{}
errUnmarshal := json.Unmarshal(resp, &result)
if errUnmarshal != nil {
return nil, errUnmarshal
}
return result, nil
}