-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
42 lines (35 loc) · 1.06 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
package module
import (
"fmt"
"sort"
)
var registry = map[string]InitFunc{}
// Config is the conduit for providing initialization information to a module
type Config map[string]interface{}
// InitFunc is a factory function that returns a module
type InitFunc func(Config) (Patcher, error)
// Lookup retrieves a module's initialization function from the Registry
func Lookup(name string) (InitFunc, error) {
c, ok := registry[name]
if !ok {
return nil, fmt.Errorf("module type not registered: %s", name)
}
return c, nil
}
// Register registers a module under a specified name. This is then exposed as a constructor in the `eolian.synth`
// package at the Lua layer.
func Register(name string, fn func(Config) (Patcher, error)) {
if _, ok := registry[name]; ok {
panic(fmt.Sprintf("%s already registered as a module", name))
}
registry[name] = fn
}
// RegisteredTypes returns a list of all registered module types
func RegisteredTypes() []string {
types := []string{}
for k := range registry {
types = append(types, k)
}
sort.Strings(types)
return types
}