-
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/activate unregistered type (#9)
* Adds new activate.go resolver module * Renames test types and changes interface visibility
- Loading branch information
1 parent
2fd6730
commit 3be652b
Showing
4 changed files
with
102 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package tests | ||
|
||
import ( | ||
"context" | ||
"github.com/matzefriedrich/parsley/pkg/registration" | ||
"github.com/matzefriedrich/parsley/pkg/resolving" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func Test_Activate_resolve_unknown_service_type_using_resolve_options(t *testing.T) { | ||
|
||
// Arrange | ||
registry := registration.NewServiceRegistry() | ||
_ = registration.RegisterTransient(registry, newBar) | ||
|
||
sut := resolving.NewResolver(registry) | ||
scopedContext := resolving.NewScopedContext(context.Background()) | ||
|
||
// Act | ||
actual, err := resolving.Activate[bar0](sut, scopedContext, newFooWithBar) | ||
|
||
// Assert | ||
assert.NoError(t, err) | ||
assert.NotNil(t, actual) | ||
} | ||
|
||
type fooWithBar struct { | ||
bar bar0 | ||
} | ||
|
||
type bar struct{} | ||
|
||
type foo0 interface{} | ||
|
||
type bar0 interface{} | ||
|
||
func newFooWithBar(bar bar0) foo0 { | ||
return &fooWithBar{bar: bar} | ||
} | ||
|
||
func newBar() bar0 { | ||
return &bar{} | ||
} |
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,39 @@ | ||
package resolving | ||
|
||
import ( | ||
"context" | ||
"github.com/matzefriedrich/parsley/pkg/registration" | ||
"github.com/matzefriedrich/parsley/pkg/types" | ||
) | ||
|
||
func Activate[T any](resolver types.Resolver, ctx context.Context, activatorFunc any, options ...types.ResolverOptionsFunc) (T, error) { | ||
|
||
var nilInstance T | ||
|
||
lifetimeScope := types.LifetimeTransient | ||
registration, registrationErr := registration.CreateServiceRegistration(activatorFunc, lifetimeScope) | ||
if registrationErr != nil { | ||
return nilInstance, types.NewResolverError("failed to create instance of unregistered type", types.WithCause(registrationErr)) | ||
} | ||
|
||
serviceType := registration.ServiceType() | ||
resolveActivatorFuncOption := func(registry types.ServiceRegistry) error { | ||
return registry.Register(activatorFunc, lifetimeScope) | ||
} | ||
|
||
services, err := resolver.ResolveWithOptions(ctx, serviceType, resolveActivatorFuncOption) | ||
if err != nil { | ||
return nilInstance, err | ||
} | ||
|
||
if len(services) == 1 { | ||
compatible, ok := services[0].(T) | ||
if ok { | ||
return compatible, nil | ||
} | ||
} else if len(services) > 1 { | ||
return nilInstance, types.NewResolverError(types.ErrorAmbiguousServiceInstancesResolved) | ||
} | ||
|
||
return nilInstance, types.NewResolverError(types.ErrorCannotResolveService) | ||
} |