forked from go-kratos/kratos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.go
55 lines (45 loc) · 1.07 KB
/
register.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
package paladin
import (
"fmt"
"sort"
"sync"
)
var (
driversMu sync.RWMutex
drivers = make(map[string]Driver)
)
// Register makes a paladin driver available by the provided name.
// If Register is called twice with the same name or if driver is nil,
// it panics.
func Register(name string, driver Driver) {
driversMu.Lock()
defer driversMu.Unlock()
if driver == nil {
panic("paladin: driver is nil")
}
if _, dup := drivers[name]; dup {
panic("paladin: Register called twice for driver " + name)
}
drivers[name] = driver
}
// Drivers returns a sorted list of the names of the registered paladin driver.
func Drivers() []string {
driversMu.RLock()
defer driversMu.RUnlock()
var list []string
for name := range drivers {
list = append(list, name)
}
sort.Strings(list)
return list
}
// GetDriver returns a driver implement by name.
func GetDriver(name string) (Driver, error) {
driversMu.RLock()
driveri, ok := drivers[name]
driversMu.RUnlock()
if !ok {
return nil, fmt.Errorf("paladin: unknown driver %q (forgotten import?)", name)
}
return driveri, nil
}