Skip to content

Commit

Permalink
rename Initialize to NewInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
Ompluscator committed Oct 18, 2022
1 parent 21d6f4b commit 7b001ce
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 48 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ if err != nil {
return err
}

instance, err := genjector.Initialize[ServiceInterface]()
instance, err := genjector.NewInstance[ServiceInterface]()
if err != nil {
return err
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion _benchmark/benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
10 changes: 5 additions & 5 deletions binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions examples/annotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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")
}
Expand Down
4 changes: 2 additions & 2 deletions examples/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ 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")
}
if instance != nil {
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")
}
Expand Down
4 changes: 2 additions & 2 deletions examples/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion examples/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
4 changes: 2 additions & 2 deletions examples/pointer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand Down
4 changes: 2 additions & 2 deletions examples/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand Down
8 changes: 4 additions & 4 deletions examples/singleton_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion examples/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
4 changes: 2 additions & 2 deletions examples/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand Down
10 changes: 5 additions & 5 deletions injection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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]{}

Expand Down Expand Up @@ -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)
}
Expand Down
28 changes: 14 additions & 14 deletions injection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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")
}
Expand All @@ -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) {
Expand All @@ -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")
}
Expand All @@ -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) {
Expand All @@ -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")
}
Expand All @@ -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) {
Expand All @@ -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)
}
Expand All @@ -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) {
Expand All @@ -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",
Expand Down

0 comments on commit 7b001ce

Please sign in to comment.