Started implementing getIndex, now with TDD (brought to you by DI)!
This commit is contained in:
+19
-1
@@ -1,9 +1,27 @@
|
||||
package utils
|
||||
|
||||
import "github.com/karlkfi/inject"
|
||||
import (
|
||||
"github.com/karlkfi/inject"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
var Graph inject.Graph
|
||||
|
||||
var (
|
||||
definitions map[reflect.Type]interface{}
|
||||
)
|
||||
|
||||
func DefineSingleton(ptr interface{}, constructor interface{}, args ...interface{}) {
|
||||
typ := reflect.TypeOf(ptr)
|
||||
if definitions[typ] == nil {
|
||||
Graph.Define(ptr, inject.NewProvider(constructor, args...))
|
||||
} else {
|
||||
Graph.Define(definitions[typ], inject.NewProvider(constructor, args...))
|
||||
}
|
||||
definitions[typ] = ptr
|
||||
}
|
||||
|
||||
func init() {
|
||||
definitions = make(map[reflect.Type]interface{})
|
||||
Graph = inject.NewGraph()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user