forked from SpectoLabs/hoverfly
/
metadata.go
65 lines (56 loc) · 1.4 KB
/
metadata.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
package hoverfly
// Metadata - interface to store and retrieve any metadata that is related to Hoverfly
type Metadata interface {
Set(key, value string) error
Get(key string) (string, error)
Delete(key string) error
GetAll() (map[string]string, error)
DeleteData() error
CloseDB()
}
// NewMetadata - default metadata store
func NewMetadata(cache Cache) *Meta {
return &Meta{
DS: cache,
}
}
// Meta - metadata backend that uses Cache interface
type Meta struct {
DS Cache
}
// CloseDB - closes database
func (m *Meta) CloseDB() {
m.DS.CloseDB()
}
// Set - saves given key and value pair to BoltDB
func (m *Meta) Set(key, value string) error {
return m.DS.Set([]byte(key), []byte(value))
}
// Get - gets value for given key
func (m *Meta) Get(key string) (value string, err error) {
val, err := m.DS.Get([]byte(key))
if err != nil {
return "", nil
}
return string(val), err
}
// GetAll - returns all key/value pairs
func (m *Meta) GetAll() (map[string]string, error) {
entries, err := m.DS.GetAllEntries()
newEntries := make(map[string]string)
if err != nil {
return newEntries, err
}
for k, v := range entries {
newEntries[k] = string(v)
}
return newEntries, nil
}
// Delete - deletes given metadata key
func (m *Meta) Delete(key string) error {
return m.DS.Delete([]byte(key))
}
// DeleteData - deletes bucket with all saved data
func (m *Meta) DeleteData() error {
return m.DS.DeleteData()
}