-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer_binding_creators.go
79 lines (63 loc) · 2.47 KB
/
container_binding_creators.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package container
import (
"log"
"reflect"
)
// addFunctionBinding - Create a new container binding from the function
// This resolves the return type of the function as the Abstract
// And the functions return value is our Concrete
func (container *ContainerInstance) addFunctionBinding(definition reflect.Type, resolver any) {
numOut := definition.NumOut()
if numOut == 0 {
log.Printf("Trying to register binding but it doesnt have a return type...")
return
}
if numOut > 1 {
log.Printf("Registering a function binding with > 1 return args. Only the first arg is handled.")
}
// if definition.NumIn() > 0 {
// log.Printf("Function binding has args... if these args cannot be found in the container when resolving, your code will error.")
// }
resolverType := reflect.TypeOf(resolver)
container.addBinding(indirectType(definition.Out(0)), &Binding{
bindingType: "Function",
resolverFunction: resolver,
isFunctionResolver: true,
abstractType: definition,
concreteType: resolverType,
invocable: CreateInvocableFunction(resolver),
})
}
// addConcreteBinding - Create a new container binding from the concrete value
// This will set our abstract type to the concrete type and the concrete type will be our concrete type..
// This just allows us to easily bind things to the container if we don't care about abstracts
func (container *ContainerInstance) addConcreteBinding(definition reflect.Type, concrete any) {
concreteType := definition
if definition.Kind() == reflect.Ptr {
concreteType = definition.Elem()
}
// concreteWrapperFuncType := reflect.TypeOf(func() any {
// return concrete
// })
//
// concreteFunc := reflect.MakeFunc(concreteWrapperFuncType, func(args []reflect.Value) []reflect.Value {
// return []reflect.Value{reflect.ValueOf(concrete)}
// })
container.addBinding(concreteType, &Binding{
bindingType: "Concrete",
isFunctionResolver: false,
abstractType: concreteType,
concreteType: definition,
invocable: CreateInvocable(concreteType),
})
}
// addBinding - Convenience function to add a Binding for the type &
// create a reverse lookup for Concrete -> Abstract
func (container *ContainerInstance) addBinding(abstractType reflect.Type, binding *Binding) {
container.bindings[abstractType] = binding
container.concretes[binding.concreteType] = abstractType
}
func (container *ContainerInstance) addSingletonBinding(singletonType reflect.Type, binding *Binding) {
binding.isSingleton = true
container.addBinding(singletonType, binding)
}