Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎨 improvements to the di code #107

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pkg/di/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ Exemplo básico:

func main() {

dependencies := []interface{}{NewFoo}
app := di.NewContainer()
app.AddDependencies(dependencies)
app.AddDependencies(NewFoo)
app.StartApp(InitializeAPP)
}

Expand Down
15 changes: 9 additions & 6 deletions pkg/di/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package di
import (
"fmt"
"log"
"maps"
"reflect"
)

Expand All @@ -11,21 +12,23 @@ type Container struct {
}

func NewContainer() Container {
return Container{}
return Container{
dependencies: make(map[string]DependencyBean),
}
}

func (c *Container) AddDependencies(deps []interface{}) {
func (c *Container) AddDependencies(deps ...interface{}) {
// Gera o array com as dependencias
ReflectTypeArray := generateDependenciesArray(deps, false)
c.checkingNameUnit(ReflectTypeArray)
c.dependencies = ReflectTypeArray
maps.Copy(c.dependencies, ReflectTypeArray)
}

func (c *Container) AddGlobalDependencies(deps []interface{}) {
func (c *Container) AddGlobalDependencies(deps ...interface{}) {
// Gera o array com as dependencias
ReflectTypeArray := generateDependenciesArray(deps, true)
c.checkingNameUnit(ReflectTypeArray)
c.dependencies = ReflectTypeArray
maps.Copy(c.dependencies, ReflectTypeArray)
}

func (f *Container) StartApp(startFunc interface{}) {
Expand All @@ -50,7 +53,7 @@ func (c *Container) getDependencyConstructorArgs(dependency DependencyBean) []re
args := []reflect.Value{}
fmt.Printf("constructor: %s, number of parameters: %d\n", dependency.Name, len(dependency.ParamTypes))
for position, paramType := range dependency.ParamTypes {

// Check if trhe variadic param
if dependency.IsVariadic {
if position == (len(dependency.ParamTypes) - 1) {
Expand Down
4 changes: 1 addition & 3 deletions pkg/di/examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ type Controller struct {
}

func main() {
// Criação de um array de funções de diferentes tipos
dependencies := []interface{}{newController, newService, newRepository}

app := di.NewContainer()

app.AddDependencies(dependencies)
app.AddDependencies(newController, newService, newRepository)

app.StartApp(InitializeAPP)
}
Expand Down
8 changes: 2 additions & 6 deletions pkg/di/tests/basics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@ import (

func Test_Bean_not_found(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{beanInt}
a.AddDependencies(funcs)
a.AddDependencies(beanInt)
assert.Panics(t, func() { a.StartApp(InitializeAPP) })
}

func Test_Success(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{beanInt, beanFloat32}
a.AddDependencies(funcs)
a.AddDependencies(beanInt, beanFloat32)
assert.NotPanics(t, func() { a.StartApp(InitializeAPP) })
}
19 changes: 4 additions & 15 deletions pkg/di/tests/disambiguation_and_interfaces_and_global_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,29 @@ import (

func Test_Interfaces_Disambiguation_Global_Bean_not_found(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{}
a.AddGlobalDependencies(funcs)
assert.Panics(t, func() { a.StartApp(NewBarObjectWithoutTag) })
}

func Test_Interfaces_Disambiguation_Global_Success(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{newFooImplementation1}
a.AddGlobalDependencies(funcs)
a.AddGlobalDependencies(newFooImplementation1)
assert.NotPanics(t, func() { a.StartApp(NewBarObjectWithoutTag) })
}

func Test_Interfaces_Disambiguation_Global_Tag_not_found(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{newFooImplementation1, newFooImplementation3}
a.AddGlobalDependencies(funcs)
a.AddGlobalDependencies(newFooImplementation1, newFooImplementation3)
assert.Panics(t, func() { a.StartApp(NewBarObjectWithTag) })
}

func Test_Interfaces_Disambiguation_Global_Not_Tag(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{newFooImplementation1, newFooImplementation2}
a.AddGlobalDependencies(funcs)
a.AddGlobalDependencies(newFooImplementation1, newFooImplementation2)
assert.Panics(t, func() { a.StartApp(NewBarObjectWithoutTag) })
}

func Test_Interfaces_Disambiguation_Global_Sucess_2(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{newFooImplementation1, newFooImplementation2}
a.AddGlobalDependencies(funcs)
a.AddGlobalDependencies(newFooImplementation1, newFooImplementation2)
assert.NotPanics(t, func() { a.StartApp(NewBarObjectWithTag) })
}
19 changes: 4 additions & 15 deletions pkg/di/tests/disambiguation_and_interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,29 @@ import (

func Test_Interfaces_Disambiguation_Bean_not_found(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{}
a.AddDependencies(funcs)
assert.Panics(t, func() { a.StartApp(NewBarObjectWithoutTag) })
}

func Test_Interfaces_Disambiguation_Success(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{newFooImplementation1}
a.AddDependencies(funcs)
a.AddDependencies(newFooImplementation1)
assert.NotPanics(t, func() { a.StartApp(NewBarObjectWithoutTag) })
}

func Test_Interfaces_Disambiguation_Tag_not_found(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{newFooImplementation1, newFooImplementation3}
a.AddDependencies(funcs)
a.AddDependencies(newFooImplementation1, newFooImplementation3)
assert.Panics(t, func() { a.StartApp(NewBarObjectWithTag) })
}

func Test_Interfaces_Disambiguation_Not_Tag(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{newFooImplementation1, newFooImplementation2}
a.AddDependencies(funcs)
a.AddDependencies(newFooImplementation1, newFooImplementation2)
assert.Panics(t, func() { a.StartApp(NewBarObjectWithoutTag) })
}

func Test_Interfaces_Disambiguation_Sucess_2(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{newFooImplementation1, newFooImplementation2}
a.AddDependencies(funcs)
a.AddDependencies(newFooImplementation1, newFooImplementation2)
assert.NotPanics(t, func() { a.StartApp(NewBarObjectWithTag) })
}
19 changes: 4 additions & 15 deletions pkg/di/tests/disambiguation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,29 @@ import (

func Test_metadata_Bean_not_found(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{}
a.AddDependencies(funcs)
assert.Panics(t, func() { a.StartApp(NewBeanWithMetadata) })
}

func Test_metadata_Success(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{NewBeanDependency1}
a.AddDependencies(funcs)
a.AddDependencies(NewBeanDependency1)
assert.NotPanics(t, func() { a.StartApp(NewBeanWithMetadata) })
}

func Test_Success_Disambiguation(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{NewBeanDependency1, NewBeanDependency2}
a.AddDependencies(funcs)
a.AddDependencies(NewBeanDependency1, NewBeanDependency2)
assert.NotPanics(t, func() { a.StartApp(NewBeanWithMetadata) })
}

func Test_Disambiguation_Tag_not_found(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{NewBeanDependency1, NewBeanDependency3}
a.AddDependencies(funcs)
a.AddDependencies(NewBeanDependency1, NewBeanDependency3)
assert.Panics(t, func() { a.StartApp(NewBeanWithMetadata) })
}

func Test_Disambiguation_Not_Tag(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{NewBeanDependency1, NewBeanDependency2, NewBeanDependency3}
a.AddDependencies(funcs)
a.AddDependencies(NewBeanDependency1, NewBeanDependency2, NewBeanDependency3)
assert.Panics(t, func() { a.StartApp(NewBeanWithoutMetadata) })
}
6 changes: 2 additions & 4 deletions pkg/di/tests/duplicate_constructor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import (

func Test_Duplicate_constructor(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{beanInt, beanFloat32}
assert.Panics(t, func() {
a.AddDependencies(funcs)
a.AddGlobalDependencies(funcs)
a.AddDependencies(beanInt, beanFloat32)
a.AddGlobalDependencies(beanInt, beanFloat32)
a.StartApp(InitializeAPP)
})
}
8 changes: 2 additions & 6 deletions pkg/di/tests/global_injection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@ import (

func Test_Global_Injection_Bean_not_found(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{GlobalBeanString, globalBeanInt}
a.AddGlobalDependencies(funcs)
a.AddGlobalDependencies(GlobalBeanString, globalBeanInt)
assert.Panics(t, func() { a.StartApp(GlobalInitializeAPP) })
}

func Test_Global_Injection_Success(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{globalBeanFloat32, GlobalBeanString, globalBeanInt}
a.AddGlobalDependencies(funcs)
a.AddGlobalDependencies(globalBeanFloat32, GlobalBeanString, globalBeanInt)
assert.NotPanics(t, func() { a.StartApp(GlobalInitializeAPP) })
}
7 changes: 1 addition & 6 deletions pkg/di/tests/global_interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@ import (

func Test_global_interfaces_Bean_not_found(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{}
a.AddGlobalDependencies(funcs)
assert.Panics(t, func() { a.StartApp(NewMyGlobalDependencyObject) })
}

func Test_global_interfaces_Success(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{newMyGlobalImplementation}
a.AddGlobalDependencies(funcs)
a.AddGlobalDependencies(newMyGlobalImplementation)
assert.NotPanics(t, func() { a.StartApp(NewMyGlobalDependencyObject) })
}
7 changes: 1 addition & 6 deletions pkg/di/tests/interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@ import (

func Test_interfaces_Bean_not_found(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{}
a.AddDependencies(funcs)
assert.Panics(t, func() { a.StartApp(NewMyDependencyObject) })
}

func Test_interfaces_Success(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{newMyImplementation}
a.AddDependencies(funcs)
a.AddDependencies(newMyImplementation)
assert.NotPanics(t, func() { a.StartApp(NewMyDependencyObject) })
}
13 changes: 5 additions & 8 deletions pkg/di/tests/variadic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,25 @@ import (
"github.com/stretchr/testify/assert"
)

func newInt2() int{
func newInt2() int {
return 2
}

func newInt1() int{
func newInt1() int {
return 1
}

func newStringV() string{
func newStringV() string {
return "stringv"
}

func NewV(a string, s ...int)string{
func NewV(a string, s ...int) string {
fmt.Println("recebi: ", len(s), " dependencias ")
return "s"
}

func Test_variadic(t *testing.T) {
a := di.NewContainer()
// Criação de um array de funções de diferentes tipos
funcs := []interface{}{newInt1, newInt2, newStringV}
a.AddDependencies(funcs)
a.AddDependencies(newInt1, newInt2, newStringV)
assert.NotPanics(t, func() { a.StartApp(NewV) })
}

Loading