-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/inject non registered types (#5)
* Extends the resolver to support injection of objects of unregistered types
- Loading branch information
1 parent
b780037
commit 037cf96
Showing
11 changed files
with
210 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package pkg | ||
|
||
import ( | ||
"github.com/matzefriedrich/parsley/internal" | ||
"github.com/matzefriedrich/parsley/pkg/types" | ||
"reflect" | ||
) | ||
|
||
func CreateServiceActivatorFrom[T any](instance T) (func() T, error) { | ||
if internal.IsNil(instance) { | ||
return nil, types.NewRegistryError(types.ErrorInstanceCannotBeNil) | ||
} | ||
t := reflect.TypeOf((*T)(nil)).Elem() | ||
if t.Kind() != reflect.Interface { | ||
return nil, types.NewRegistryError(types.ErrorActivatorFunctionsMustReturnAnInterface) | ||
} | ||
instanceFunc := func() T { | ||
return instance | ||
} | ||
return instanceFunc, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package pkg | ||
|
||
import ( | ||
"github.com/matzefriedrich/parsley/pkg/types" | ||
"reflect" | ||
) | ||
|
||
type multiRegistryAccessor struct { | ||
registries []types.ServiceRegistryAccessor | ||
} | ||
|
||
func (m *multiRegistryAccessor) TryGetServiceRegistration(serviceType reflect.Type) (types.ServiceRegistration, bool) { | ||
for _, registry := range m.registries { | ||
registration, ok := registry.TryGetServiceRegistration(serviceType) | ||
if ok { | ||
return registration, ok | ||
} | ||
} | ||
return nil, false | ||
} | ||
|
||
var _ types.ServiceRegistryAccessor = &multiRegistryAccessor{} | ||
|
||
func NewMultiRegistryAccessor(registries ...types.ServiceRegistryAccessor) types.ServiceRegistryAccessor { | ||
serviceRegistries := make([]types.ServiceRegistryAccessor, 0) | ||
serviceRegistries = append(serviceRegistries, registries...) | ||
return &multiRegistryAccessor{ | ||
registries: serviceRegistries, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package pkg | ||
|
||
import ( | ||
"github.com/matzefriedrich/parsley/pkg/types" | ||
) | ||
|
||
func applyResolverOptions(registry types.ServiceRegistry, options ...types.ResolverOptionsFunc) error { | ||
for _, option := range options { | ||
err := option(registry) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func WithInstance[T any](instance T) types.ResolverOptionsFunc { | ||
return func(registry types.ServiceRegistry) error { | ||
err := RegisterInstance[T](registry, instance) | ||
if err != nil { | ||
return types.NewRegistryError(types.ErrorCannotRegisterTypeWithResolverOptions, types.WithCause(err)) | ||
} | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package pkg | ||
|
||
import ( | ||
"context" | ||
"github.com/matzefriedrich/parsley/internal" | ||
"github.com/matzefriedrich/parsley/pkg/types" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func Test_Resolver_ResolveWithOptions_inject_unregistered_service_instance(t *testing.T) { | ||
|
||
// Arrange | ||
sut := NewServiceRegistry() | ||
|
||
// Act | ||
_ = RegisterScoped(sut, NewFooConsumer) | ||
|
||
// Assert | ||
r := sut.BuildResolver() | ||
parsleyContext := internal.NewScopedContext(context.Background()) | ||
consumer1, _ := r.ResolveWithOptions(parsleyContext, types.ServiceType[FooConsumer](), WithInstance[Foo](NewFoo())) | ||
assert.NotNil(t, consumer1) | ||
|
||
actual, ok := consumer1.(FooConsumer) | ||
assert.True(t, ok) | ||
assert.NotNil(t, actual) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters