This repository has been archived by the owner on Aug 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
impl.go
57 lines (44 loc) · 1.82 KB
/
impl.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
package storageref
import (
"sync"
"github.com/pkg/errors"
)
// ErrDuplicateImpl is returned when a duplicate storageref implementation is registered.
var ErrDuplicateImpl = errors.New("duplicate storageref implementation")
// StorageRefImplCtor constructs a implementation from a reference.
type StorageRefImplCtor = func(storageRef *StorageRef) StorageRefImpl
// storageRefImplCtorsMtx is the mutex on the storageRefImplCtors map
var storageRefImplCtorsMtx sync.RWMutex
// storageRefImplCtors contains registered implementations.
var storageRefImplCtors = make(map[StorageType]StorageRefImplCtor)
// MustRegisterStorageRefImplCtor registers a storage ref implementation constructor or panics.
// expected to be called from Init(), but can be deferred
func MustRegisterStorageRefImplCtor(storageType StorageType, impl StorageRefImplCtor) {
if err := RegisterStorageRefImplCtor(storageType, impl); err != nil {
panic(err)
}
}
// RegisterStorageRefImplCtor registers a storage ref implementation constructor.
// expected to be called from Init(), but can be deferred
func RegisterStorageRefImplCtor(storageType StorageType, impl StorageRefImplCtor) error {
storageRefImplCtorsMtx.Lock()
defer storageRefImplCtorsMtx.Unlock()
if _, ok := storageRefImplCtors[storageType]; ok {
return ErrDuplicateImpl
}
storageRefImplCtors[storageType] = impl
return nil
}
// GetStorageRefImplCtor returns the registered implementation of the type.
func GetStorageRefImplCtor(kind StorageType) (impl StorageRefImplCtor, err error) {
if _, ok := StorageType_name[int32(kind)]; !ok {
return nil, errors.Errorf("storage type unknown: %v", kind.String())
}
storageRefImplCtorsMtx.RLock()
impl = storageRefImplCtors[kind]
storageRefImplCtorsMtx.RUnlock()
if impl == nil {
err = errors.Errorf("unimplemented storage type: %v", kind.String())
}
return
}