diff --git a/README.md b/README.md index d8606bd..bb96205 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ if err != nil { return err } -instance, err := genjector.Initialize[ServiceInterface]() +instance, err := genjector.NewInstance[ServiceInterface]() if err != nil { return err } @@ -123,7 +123,7 @@ if err != nil { return err } -instance, err := genjector.Initialize[ServiceInterface]( +instance, err := genjector.NewInstance[ServiceInterface]( genjector.WithAnnotation("service"), ) if err != nil { diff --git a/_benchmark/benchmarks_test.go b/_benchmark/benchmarks_test.go index 7f4450e..2ac95f6 100644 --- a/_benchmark/benchmarks_test.go +++ b/_benchmark/benchmarks_test.go @@ -128,7 +128,7 @@ func Benchmark(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - variable = genjector.MustInitialize[BenchmarkInterface]() + variable = genjector.MustNewInstance[BenchmarkInterface]() } variable = &BenchmarkStruct{} diff --git a/binding.go b/binding.go index c1be0d0..8c3713d 100644 --- a/binding.go +++ b/binding.go @@ -113,7 +113,7 @@ func AsPointer[T any, S *R, R any]() BindingSource[T] { // ProviderMethod defines a type of a method that should delivers // an instance od type S. This method acts as an constructor method -// and it is executed at the time of Initialize method. +// and it is executed at the time of NewInstance method. // // It respects Binding interface. type ProviderMethod[S any] func() (S, error) @@ -235,7 +235,7 @@ func (b *singletonBinding) Instance(initialize bool) (interface{}, error) { // AsSingleton delivers a BindingOption that defines the instance of desired // Binding as a singleton. That means only first time the Init method (or ProviderMethod) // will be called, and every next time the same instance will be delivered -// as a result of Initialize method. +// as a result of NewInstance method. // // Example: // err := genjector.Bind( @@ -246,7 +246,7 @@ func (b *singletonBinding) Instance(initialize bool) (interface{}, error) { // ) // // AsSingleton should be only used as a BindingOption for Bind method, as it -// does not affect functionality if it is used in Initialize method. +// does not affect functionality if it is used in NewInstance method. func AsSingleton() BindingOption { return &bindingOption{ bindingFunc: func(binding Binding) (Binding, error) { @@ -270,7 +270,7 @@ func AsSingleton() BindingOption { // ) // // To properly use a customer Container, WithAnnotation should be used in both -// Bind and Initialize methods. +// Bind and NewInstance methods. func WithAnnotation(annotation string) BindingOption { return &bindingOption{ bindingFunc: func(binding Binding) (Binding, error) { @@ -295,7 +295,7 @@ func WithAnnotation(annotation string) BindingOption { // ) // // To properly use a customer Container, WithContainer should be used in both -// Bind and Initialize methods. +// Bind and NewInstance methods. func WithContainer(container Container) BindingOption { return &bindingOption{ bindingFunc: func(binding Binding) (Binding, error) { diff --git a/chain.go b/chain.go index 309b7ac..a5c94a1 100644 --- a/chain.go +++ b/chain.go @@ -100,7 +100,7 @@ func (b *sliceBindingSource[T]) Key() Key { // InSlice delivers a BindingSource for a slice of types T.. It is used as a wrapping // BindingSource for any other inner. It creates complex Binding in the background -// that stores all T types in a slice and delivers it upon request by executing Initialize +// that stores all T types in a slice and delivers it upon request by executing NewInstance // method for a slice of T types. // // Example: @@ -220,7 +220,7 @@ func (b *mapBindingSource[K, T]) Key() Key { // InMap delivers a BindingSource for a type T and key's type K, that creates a map // of K-T pairs. It is used as a wrapping BindingSource for any other inner. It creates // complex Binding in the background that stores all T types in a map and delivers it -// upon request by executing Initialize method for a K-T map. +// upon request by executing NewInstance method for a K-T map. // // Example: // err := :genjector.Bind( diff --git a/examples/annotation_test.go b/examples/annotation_test.go index 0acf123..48a4b20 100644 --- a/examples/annotation_test.go +++ b/examples/annotation_test.go @@ -15,8 +15,8 @@ type AnnotationStruct struct { } func (s *AnnotationStruct) Init() { - firstChild := genjector.MustInitialize[*AnnotationChildStruct](genjector.WithAnnotation("first")) - secondChild := genjector.MustInitialize[*AnnotationChildStruct](genjector.WithAnnotation("second")) + firstChild := genjector.MustNewInstance[*AnnotationChildStruct](genjector.WithAnnotation("first")) + secondChild := genjector.MustNewInstance[*AnnotationChildStruct](genjector.WithAnnotation("second")) s.value = fmt.Sprintf("%s | %s", firstChild.value, secondChild.value) } @@ -59,7 +59,7 @@ func TestAsAnnotation(t *testing.T) { t.Error("binding should not cause an error") } - instance, err := genjector.Initialize[AnnotationInterface]() + instance, err := genjector.NewInstance[AnnotationInterface]() if err != nil { t.Error("initialization should not cause an error") } diff --git a/examples/container_test.go b/examples/container_test.go index bf484ff..b58ad20 100644 --- a/examples/container_test.go +++ b/examples/container_test.go @@ -35,7 +35,7 @@ func TestAsContainer(t *testing.T) { t.Error("binding should not cause an error") } - instance, err := genjector.Initialize[ContainerInterface]() + instance, err := genjector.NewInstance[ContainerInterface]() if err == nil { t.Error("expected an error, but got nil") } @@ -43,7 +43,7 @@ func TestAsContainer(t *testing.T) { t.Errorf(`unexpected instance received: "%s"`, instance) } - instance, err = genjector.Initialize[ContainerInterface](genjector.WithContainer(customContainer)) + instance, err = genjector.NewInstance[ContainerInterface](genjector.WithContainer(customContainer)) if err != nil { t.Error("initialization should not cause an error") } diff --git a/examples/instance_test.go b/examples/instance_test.go index 7e46c4f..526af1d 100644 --- a/examples/instance_test.go +++ b/examples/instance_test.go @@ -33,7 +33,7 @@ func TestAsInstance(t *testing.T) { t.Error("binding should not cause an error") } - instance, err := genjector.Initialize[InstanceInterface]() + instance, err := genjector.NewInstance[InstanceInterface]() if err != nil { t.Error("initialization should not cause an error") } @@ -54,7 +54,7 @@ func TestAsInstance(t *testing.T) { t.Error("binding should not cause an error") } - instance, err := genjector.Initialize[*InstanceStruct]() + instance, err := genjector.NewInstance[*InstanceStruct]() if err != nil { t.Error("initialization should not cause an error") } diff --git a/examples/map_test.go b/examples/map_test.go index a226642..adcc3c8 100644 --- a/examples/map_test.go +++ b/examples/map_test.go @@ -52,7 +52,7 @@ func TestAsMap(t *testing.T) { t.Error("binding should not cause an error") } - instance, err := genjector.Initialize[map[string]MapInterface]() + instance, err := genjector.NewInstance[map[string]MapInterface]() if err != nil { t.Error("initialization should not cause an error") } diff --git a/examples/pointer_test.go b/examples/pointer_test.go index a286f19..69bf57c 100644 --- a/examples/pointer_test.go +++ b/examples/pointer_test.go @@ -30,7 +30,7 @@ func TestAsPointer(t *testing.T) { t.Error("binding should not cause an error") } - instance, err := genjector.Initialize[PointerInterface]() + instance, err := genjector.NewInstance[PointerInterface]() if err != nil { t.Error("initialization should not cause an error") } @@ -49,7 +49,7 @@ func TestAsPointer(t *testing.T) { t.Error("binding should not cause an error") } - instance, err := genjector.Initialize[*PointerStruct]() + instance, err := genjector.NewInstance[*PointerStruct]() if err != nil { t.Error("initialization should not cause an error") } diff --git a/examples/provider_test.go b/examples/provider_test.go index ff58e9b..f91cc56 100644 --- a/examples/provider_test.go +++ b/examples/provider_test.go @@ -35,7 +35,7 @@ func TestAsProvider(t *testing.T) { t.Error("binding should not cause an error") } - instance, err := genjector.Initialize[ProviderInterface]() + instance, err := genjector.NewInstance[ProviderInterface]() if err != nil { t.Error("initialization should not cause an error") } @@ -55,7 +55,7 @@ func TestAsProvider(t *testing.T) { }, nil })) - instance, err := genjector.Initialize[*ProviderStruct]() + instance, err := genjector.NewInstance[*ProviderStruct]() if err != nil { t.Error("initialization should not cause an error") } diff --git a/examples/singleton_test.go b/examples/singleton_test.go index e0bdb15..87553f2 100644 --- a/examples/singleton_test.go +++ b/examples/singleton_test.go @@ -37,7 +37,7 @@ func TestAsSingleton(t *testing.T) { t.Error("binding should not cause an error") } - instance, err := genjector.Initialize[SingletonInterface]() + instance, err := genjector.NewInstance[SingletonInterface]() if err != nil { t.Error("initialization should not cause an error") } @@ -50,7 +50,7 @@ func TestAsSingleton(t *testing.T) { t.Errorf(`unexpected counter received: "%d"`, counter) } - instance, err = genjector.Initialize[SingletonInterface]() + instance, err = genjector.NewInstance[SingletonInterface]() if err != nil { t.Error("initialization should not cause an error") } @@ -73,7 +73,7 @@ func TestAsSingleton(t *testing.T) { t.Error("binding should not cause an error") } - instance, err := genjector.Initialize[SingletonInterface]() + instance, err := genjector.NewInstance[SingletonInterface]() if err != nil { t.Error("initialization should not cause an error") } @@ -86,7 +86,7 @@ func TestAsSingleton(t *testing.T) { t.Errorf(`unexpected counter received: "%d"`, counter) } - instance, err = genjector.Initialize[SingletonInterface]() + instance, err = genjector.NewInstance[SingletonInterface]() if err != nil { t.Error("initialization should not cause an error") } diff --git a/examples/slice_test.go b/examples/slice_test.go index 873c3fe..a341477 100644 --- a/examples/slice_test.go +++ b/examples/slice_test.go @@ -52,7 +52,7 @@ func TestAsSlice(t *testing.T) { t.Error("binding should not cause an error") } - instance, err := genjector.Initialize[[]SliceInterface]() + instance, err := genjector.NewInstance[[]SliceInterface]() if err != nil { t.Error("initialization should not cause an error") } diff --git a/examples/value_test.go b/examples/value_test.go index 61bb621..ce374f5 100644 --- a/examples/value_test.go +++ b/examples/value_test.go @@ -30,7 +30,7 @@ func TestAsValue(t *testing.T) { t.Error("binding should not cause an error") } - instance, err := genjector.Initialize[ValueInterface]() + instance, err := genjector.NewInstance[ValueInterface]() if err != nil { t.Error("initialization should not cause an error") } @@ -49,7 +49,7 @@ func TestAsValue(t *testing.T) { t.Error("binding should not cause an error") } - instance, err := genjector.Initialize[ValueStruct]() + instance, err := genjector.NewInstance[ValueStruct]() if err != nil { t.Error("initialization should not cause an error") } diff --git a/injection.go b/injection.go index 58681d5..53f0ef3 100644 --- a/injection.go +++ b/injection.go @@ -128,7 +128,7 @@ func MustBind[T any](source BindingSource[T], options ...BindingOption) { } } -// Initialize executes complete logic for initializing value (or pointer) for +// NewInstance executes complete logic for initializing value (or pointer) for // desired interface (or struct). By default, it uses Binding instance from default // inner Container. If such Binding can not be found, it tries to make its own // fallback Binding. @@ -139,7 +139,7 @@ func MustBind[T any](source BindingSource[T], options ...BindingOption) { // it uses its own fallback Binding. Still, it works fully only for values, not pointers, // as for pointers it returns nil value. That means that pointer Binding // should be always defined. -func Initialize[T any](options ...KeyOption) (T, error) { +func NewInstance[T any](options ...KeyOption) (T, error) { var empty T source := &baseKeySource[T]{} @@ -175,11 +175,11 @@ func Initialize[T any](options ...KeyOption) (T, error) { return result, nil } -// MustInitialize wraps Initialize method, by making sure error is not returned as an arguement. +// MustNewInstance wraps NewInstance method, by making sure error is not returned as an arguement. // // Still, in case of error, it panics. -func MustInitialize[T any](options ...KeyOption) T { - instance, err := Initialize[T](options...) +func MustNewInstance[T any](options ...KeyOption) T { + instance, err := NewInstance[T](options...) if err != nil { panic(err) } diff --git a/injection_test.go b/injection_test.go index 8748a7e..6b44e60 100644 --- a/injection_test.go +++ b/injection_test.go @@ -140,8 +140,8 @@ func TestMustBind(t *testing.T) { }) } -func TestInitialize_defaultValue(t *testing.T) { - instance, err := Initialize[testStruct]() +func TestNewInstance_defaultValue(t *testing.T) { + instance, err := NewInstance[testStruct]() if err != nil { t.Errorf("expected nil, got error %s", err) } @@ -154,8 +154,8 @@ func TestInitialize_defaultValue(t *testing.T) { } } -func TestInitialize_defaultPointer(t *testing.T) { - instance, err := Initialize[*testStruct]() +func TestNewInstance_defaultPointer(t *testing.T) { + instance, err := NewInstance[*testStruct]() if err != nil { t.Errorf("expected nil, got error %s", err) } @@ -165,8 +165,8 @@ func TestInitialize_defaultPointer(t *testing.T) { } } -func TestInitialize_defaultError(t *testing.T) { - instance, err := Initialize[interface{}]() +func TestNewInstance_defaultError(t *testing.T) { + instance, err := NewInstance[interface{}]() if err == nil { t.Error("expected error, got nil") } @@ -176,7 +176,7 @@ func TestInitialize_defaultError(t *testing.T) { } } -func TestInitialize_success(t *testing.T) { +func TestNewInstance_success(t *testing.T) { inner := Container{ (*int)(nil): &testBinding{ instance: func(initialize bool) (interface{}, error) { @@ -185,7 +185,7 @@ func TestInitialize_success(t *testing.T) { }, } - instance, err := Initialize[int](WithContainer(inner)) + instance, err := NewInstance[int](WithContainer(inner)) if err == nil { t.Error("expected error, got nil") } @@ -195,7 +195,7 @@ func TestInitialize_success(t *testing.T) { } } -func TestInitialize_invalid(t *testing.T) { +func TestNewInstance_invalid(t *testing.T) { inner := Container{ (*int)(nil): &testBinding{ instance: func(initialize bool) (interface{}, error) { @@ -204,7 +204,7 @@ func TestInitialize_invalid(t *testing.T) { }, } - instance, err := Initialize[int](WithContainer(inner)) + instance, err := NewInstance[int](WithContainer(inner)) if err == nil { t.Error("expected error, got nil") } @@ -214,7 +214,7 @@ func TestInitialize_invalid(t *testing.T) { } } -func TestInitialize_simple_success(t *testing.T) { +func TestNewInstance_simple_success(t *testing.T) { inner := Container{ (*int)(nil): &testBinding{ instance: func(initialize bool) (interface{}, error) { @@ -223,7 +223,7 @@ func TestInitialize_simple_success(t *testing.T) { }, } - instance, err := Initialize[int](WithContainer(inner)) + instance, err := NewInstance[int](WithContainer(inner)) if err != nil { t.Errorf("expected nil, got error %s", err) } @@ -233,7 +233,7 @@ func TestInitialize_simple_success(t *testing.T) { } } -func TestInitialize_complex_success(t *testing.T) { +func TestNewInstance_complex_success(t *testing.T) { inner := Container{ [2]interface{}{"annotation", nil}: &testBinding{ instance: func(initialize bool) (interface{}, error) { @@ -242,7 +242,7 @@ func TestInitialize_complex_success(t *testing.T) { }, } - instance, err := Initialize[int](&testKeyOption{ + instance, err := NewInstance[int](&testKeyOption{ key: func(key Key) Key { return Key{ Annotation: "annotation",