migrate/driver/registry.go
Philippe Lafoucrière 9754378aa5 Move the registry to driver package
We can't test driver.New methods because of circular imports.
That's probably why the initial code had a map[string]interface{} as
registry: it removes a dependency import.
I prefer the remove the registry and have a registry returning a real
Driver. It will ease the development later.
2015-10-22 16:29:26 -04:00

45 lines
968 B
Go

package driver
import (
"sort"
"sync"
)
var driversMu sync.Mutex
var drivers = make(map[string]Driver)
// Registers a driver so it can be created from its name. Drivers should
// call this from an init() function so that they registers themselvse on
// import
func RegisterDriver(name string, driver Driver) {
driversMu.Lock()
defer driversMu.Unlock()
if driver == nil {
panic("driver: Register driver is nil")
}
if _, dup := drivers[name]; dup {
panic("sql: Register called twice for driver " + name)
}
drivers[name] = driver
}
// Retrieves a registered driver by name
func GetDriver(name string) Driver {
driversMu.Lock()
defer driversMu.Unlock()
driver := drivers[name]
return driver
}
// Drivers returns a sorted list of the names of the registered drivers.
func Drivers() []string {
driversMu.Lock()
defer driversMu.Unlock()
var list []string
for name := range drivers {
list = append(list, name)
}
sort.Strings(list)
return list
}