-
Notifications
You must be signed in to change notification settings - Fork 8
/
type_registry.go
58 lines (49 loc) · 1.96 KB
/
type_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
package goldi
import (
"fmt"
"reflect"
)
// The TypeRegistry is effectively a map of typeID strings to TypeFactory
type TypeRegistry map[string]TypeFactory
// NewTypeRegistry creates a new empty TypeRegistry
func NewTypeRegistry() TypeRegistry {
return TypeRegistry{}
}
// RegisterType is convenience method for TypeRegistry.Register
// It tries to create the correct TypeFactory and passes this to TypeRegistry.Register
// This function panics if the given generator function and arguments can not be used to create a new type factory.
func (r TypeRegistry) RegisterType(typeID string, factory interface{}, arguments ...interface{}) {
var typeFactory TypeFactory
factoryType := reflect.TypeOf(factory)
kind := factoryType.Kind()
switch {
case kind == reflect.Struct:
fallthrough
case kind == reflect.Ptr && factoryType.Elem().Kind() == reflect.Struct:
typeFactory = NewStructType(factory, arguments...)
case kind == reflect.Func:
typeFactory = NewType(factory, arguments...)
default:
panic(fmt.Errorf("could not register type %q: could not determine TypeFactory for factory type %T", typeID, factory))
}
r.Register(typeID, typeFactory)
}
// Register saves a type under the given symbolic typeID so it can be retrieved later.
// It is perfectly legal to call Register multiple times with the same typeID.
// In this case you overwrite existing type definitions with new once
func (r TypeRegistry) Register(typeID string, typeDef TypeFactory) {
r[typeID] = typeDef
}
// RegisterAll will register all given type factories under the mapped type ID
// It uses TypeRegistry.Register internally
func (r TypeRegistry) RegisterAll(factories map[string]TypeFactory) {
for typeID, typeDef := range factories {
r.Register(typeID, typeDef)
}
}
// InjectInstance enables you to inject type instances.
// If instance is nil an error is returned
func (r TypeRegistry) InjectInstance(typeID string, instance interface{}) {
factory := NewInstanceType(instance)
r.Register(typeID, factory)
}