/
registry.go
37 lines (30 loc) · 979 Bytes
/
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
// Copyright 2014 Simon Zimmermann. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package carry
import (
"fmt"
"sync"
"github.com/simonz05/carry/config"
)
// A StorageConstructor returns a Storage implementation from a configuration.
type StorageConstructor func(*config.Config) (Storage, error)
var mapLock sync.Mutex
var storageConstructors = make(map[string]StorageConstructor)
func RegisterStorageConstructor(typ string, ctor StorageConstructor) {
mapLock.Lock()
defer mapLock.Unlock()
if _, ok := storageConstructors[typ]; ok {
panic("StorageConstructor already registered for type: " + typ)
}
storageConstructors[typ] = ctor
}
func CreateStorage(typ string, config *config.Config) (Storage, error) {
mapLock.Lock()
ctor, ok := storageConstructors[typ]
mapLock.Unlock()
if !ok {
return nil, fmt.Errorf("Storage type %s not known or loaded", typ)
}
return ctor(config)
}