diff --git a/README.md b/README.md index 56d8b0a..ec07178 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,12 @@ container.Init(func(person Person){ ``` The Init method initializes the container and builds the dependency tree. +The Lookup method is an alternative to the Init method. Lookup lets you search for dependencies and initialize the container automatically. + +```go +person := cronos.Lookup(container, Person{}) +``` + ### Injecting Dependencies Dependencies must be injected through constructor functions diff --git a/cronos.go b/cronos.go index 9e64a49..ce8d228 100644 --- a/cronos.go +++ b/cronos.go @@ -12,6 +12,7 @@ type key struct { } type Cronos struct { + initialized bool cache } @@ -28,17 +29,22 @@ type cache struct { // New - initializes the dependency injection container func New() Cronos { return Cronos{ - cache{ + cache: cache{ components: map[key]component{}, constructors: map[key]constructor{}, options: OptionsList{}, notSingletons: map[key]bool{}, }, + initialized: false, } } func (cronos *Cronos) proccessOptions() { + if cronos.initialized { + return + } + sort.Sort(cronos.cache.options) for i := 0; i < len(cronos.cache.options); i++ { @@ -46,6 +52,8 @@ func (cronos *Cronos) proccessOptions() { task := op.task task(op.key, cronos) } + + cronos.initialized = true } func (cronos *Cronos) invokeConstructor(constructor constructor, args []reflect.Type) (returns []reflect.Value) { @@ -160,8 +168,13 @@ func (cronos *Cronos) Fetch(typed reflect.Type) interface{} { // Init - initializes the dependency injection container func (cronos *Cronos) Init(initFunc constructor) { - cronos.proccessOptions() args := cronos.getArgs(initFunc) cronos.invokeConstructor(initFunc, args) } + +func Lookup[T interface{}](container Cronos, typed T) T { + container.proccessOptions() + typec := reflect.TypeOf(typed) + return container.Fetch(typec).(T) +} diff --git a/cronos_test.go b/cronos_test.go index 651950c..1fa642b 100644 --- a/cronos_test.go +++ b/cronos_test.go @@ -347,3 +347,35 @@ func TestConstructorNotFunction(t *testing.T) { assert.Panics(t, func() { container.Register(person{}) }) } + +func TestLookup(t *testing.T) { + type id struct{ number string } + + type employer struct{ id } + + type worker struct{ id } + + newID := func() id { + return id{number: uuidGenerator()} + } + + newEmployer := func(id id) employer { + return employer{id} + } + + newWorker := func(id id) worker { + return worker{id} + } + + container := New() + + container.Register(newEmployer) + container.Register(newWorker) + container.Register(newID, Singleton(false)) + + instanceWorker := Lookup(container, worker{}) + instanceEmployer := Lookup(container, employer{}) + if instanceWorker.id.number == instanceEmployer.id.number { + t.Error() + } +} diff --git a/go.mod b/go.mod index 933336e..f973b92 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,11 @@ module github.com/ribeiro-rodrigo/cronos -go 1.14 +go 1.18 require github.com/stretchr/testify v1.5.1 + +require ( + github.com/davecgh/go-spew v1.1.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v2 v2.2.2 // indirect +) diff --git a/go.sum b/go.sum index 5589603..331fa69 100644 --- a/go.sum +++ b/go.sum @@ -2,10 +2,10 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/test.sh b/test.sh old mode 100644 new mode 100755