-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathregistry.go
68 lines (52 loc) · 1.58 KB
/
registry.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
66
67
68
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package dsl
import (
"errors"
"sync"
)
type registryItem struct {
New func(config Config) (Client, error)
Desc ClientDesc
}
var (
registryItems map[ClientType]registryItem
registryMutex sync.Mutex
)
func init() {
registryItems = make(map[ClientType]registryItem)
}
// RegisterClient registers a new device client. This function is not intended for use from external
// packages.
func RegisterClient(identifier ClientType, newFunc func(config Config) (Client, error), desc ClientDesc) {
registryMutex.Lock()
defer registryMutex.Unlock()
if _, ok := registryItems[identifier]; ok {
panic(errors.New("client type identifier already in use"))
}
registryItems[identifier] = registryItem{New: newFunc, Desc: desc}
}
func getClientDesc(identifier ClientType) (desc ClientDesc, ok bool) {
registryMutex.Lock()
defer registryMutex.Unlock()
item, ok := registryItems[identifier]
desc = item.Desc
return
}
func getClientNewFunc(identifier ClientType) (newFunc func(config Config) (Client, error), ok bool) {
registryMutex.Lock()
defer registryMutex.Unlock()
item, ok := registryItems[identifier]
newFunc = item.New
return
}
func getClientTypes() []ClientType {
registryMutex.Lock()
defer registryMutex.Unlock()
clientTypes := make([]ClientType, 0, len(registryItems))
for clientType := range registryItems {
clientTypes = append(clientTypes, clientType)
}
return clientTypes
}