-
Notifications
You must be signed in to change notification settings - Fork 16
/
router.go
59 lines (48 loc) · 1.19 KB
/
router.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
package data
import (
"errors"
"github.com/Oneledger/protocol/storage"
)
type Type string
var (
errInvalidInput = errors.New("invalid store type or interface")
errKeyNotFound = errors.New("store does not exist for key")
)
// Router interface supplies functionality to add a store to the Data package.
type Router interface {
Add(Type, ExtStore) error
Get(Type) (ExtStore, error)
}
var _ Router = &StorageRouter{}
type StorageRouter struct {
router map[Type]ExtStore
}
// Add a new store to the router
func (s StorageRouter) Add(storeType Type, storeObj ExtStore) error {
if storeType == "" || storeObj == nil {
return errInvalidInput
}
s.router[storeType] = storeObj
return nil
}
// Get the structure of store ,using the Type
func (s StorageRouter) Get(storeType Type) (ExtStore, error) {
if store, ok := s.router[storeType]; ok {
return store, nil
}
return nil, errKeyNotFound
}
func NewStorageRouter() StorageRouter {
return StorageRouter{
router: make(map[Type]ExtStore),
}
}
func (s *StorageRouter) WithState(state *storage.State) *StorageRouter {
for _, v := range s.router {
v.WithState(state)
}
return s
}
type ExtStore interface {
WithState(*storage.State) ExtStore
}