Skip to content

Commit

Permalink
Merge pull request #2 from ribeiro-rodrigo/feat/#1-support-pulling-de…
Browse files Browse the repository at this point in the history
…pendencies

Feat/#1 support pulling dependencies
  • Loading branch information
ribeiro-rodrigo committed Aug 6, 2023
2 parents 68034f5 + 25a86dd commit 0abe289
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions cronos.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type key struct {
}

type Cronos struct {
initialized bool
cache
}

Expand All @@ -28,24 +29,31 @@ 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++ {
op := cronos.cache.options[i]
task := op.task
task(op.key, cronos)
}

cronos.initialized = true
}

func (cronos *Cronos) invokeConstructor(constructor constructor, args []reflect.Type) (returns []reflect.Value) {
Expand Down Expand Up @@ -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)
}
32 changes: 32 additions & 0 deletions cronos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
)
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Empty file modified test.sh
100644 → 100755
Empty file.

0 comments on commit 0abe289

Please sign in to comment.