forked from danryan/hal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
58 lines (48 loc) · 1.11 KB
/
store.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
package hal
import (
"fmt"
)
// Store interface for storage backends to implement
type Store interface {
Open() error
Close() error
Get(string) ([]byte, error)
Set(key string, data []byte) error
Delete(string) error
}
type store struct {
name string
newFunc func(*Robot) (Store, error)
}
// BasicStore struct to be embedded in other stores
type BasicStore struct {
Robot *Robot
}
// SetRobot sets the adapter's Robot
func (s *BasicStore) SetRobot(r *Robot) {
s.Robot = r
}
// Stores is a map of registered stores
var Stores = map[string]store{}
// RegisterStore registers a new store
func RegisterStore(name string, newFunc func(*Robot) (Store, error)) {
Stores[name] = store{
name: name,
newFunc: newFunc,
}
}
// NewStore returns an initialized store
func NewStore(robot *Robot) (Store, error) {
name := Config.StoreName
if _, ok := Stores[name]; !ok {
return nil, fmt.Errorf("%s is not a registered store", Config.StoreName)
}
store, err := Stores[name].newFunc(robot)
if err != nil {
return nil, err
}
return store, nil
}
func (s *BasicStore) String() string {
return Config.StoreName
}