-
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.
* Extracts error message and object for failed service registrations * Extends the service_type.go module (changes registration lookup key) * Fixes the handling of types reflected from anonymous functions * Ignores the scope parameter in RegisterLazy (not supported yet) * Extracts error message and object from the activator.go module * Adds the RegisterList method to enable the resolver to inject lists of a specific dependency * Adds another test to verify that named services can be resolved as a list * Removes the scope parameter from the RegisterList method (lists must always be transient objects)
- Loading branch information
1 parent
91ab986
commit 9e92fb6
Showing
14 changed files
with
238 additions
and
68 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,30 @@ | ||
package features | ||
|
||
type dataService interface { | ||
FetchData() string | ||
} | ||
|
||
type remoteDataService struct { | ||
} | ||
|
||
func newRemoteDataService() dataService { | ||
return &remoteDataService{} | ||
} | ||
|
||
func (r *remoteDataService) FetchData() string { | ||
return "data from remote service" | ||
} | ||
|
||
var _ dataService = &remoteDataService{} | ||
|
||
type localDataService struct{} | ||
|
||
func newLocalDataService() dataService { | ||
return &localDataService{} | ||
} | ||
|
||
func (l *localDataService) FetchData() string { | ||
return "data from local service" | ||
} | ||
|
||
var _ dataService = &localDataService{} |
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,61 @@ | ||
package features | ||
|
||
import ( | ||
"context" | ||
"github.com/matzefriedrich/parsley/pkg/features" | ||
"github.com/matzefriedrich/parsley/pkg/registration" | ||
"github.com/matzefriedrich/parsley/pkg/resolving" | ||
"github.com/matzefriedrich/parsley/pkg/types" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func Test_Resolver_register_list_resolver(t *testing.T) { | ||
|
||
// Arrange | ||
registry := registration.NewServiceRegistry() | ||
registry.Register(newLocalDataService, types.LifetimeTransient) | ||
registry.Register(newRemoteDataService, types.LifetimeTransient) | ||
features.RegisterList[dataService](registry) | ||
|
||
resolver := resolving.NewResolver(registry) | ||
ctx := resolving.NewScopedContext(context.Background()) | ||
|
||
// Act | ||
actual, err := resolving.ResolveRequiredService[[]dataService](resolver, ctx) | ||
|
||
// Assert | ||
assert.NoError(t, err) | ||
assert.Len(t, actual, 2) | ||
} | ||
|
||
func Test_Resolver_resolve_multiple_instances_of_type(t *testing.T) { | ||
|
||
// Arrange | ||
registry := registration.NewServiceRegistry() | ||
registry.Register(newLocalDataService, types.LifetimeTransient) | ||
registry.Register(newRemoteDataService, types.LifetimeTransient) | ||
features.RegisterList[dataService](registry) | ||
registry.Register(newControllerWithServiceList, types.LifetimeTransient) | ||
|
||
resolver := resolving.NewResolver(registry) | ||
ctx := resolving.NewScopedContext(context.Background()) | ||
|
||
// Act | ||
actual, err := resolving.ResolveRequiredService[*controllerWithServiceList](resolver, ctx) | ||
|
||
// Assert | ||
assert.NoError(t, err) | ||
assert.NotNil(t, actual) | ||
|
||
} | ||
|
||
type controllerWithServiceList struct { | ||
dataServices []dataService | ||
} | ||
|
||
func newControllerWithServiceList(dataServices []dataService) *controllerWithServiceList { | ||
return &controllerWithServiceList{ | ||
dataServices: dataServices, | ||
} | ||
} |
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,14 @@ | ||
package features | ||
|
||
import ( | ||
"context" | ||
"github.com/matzefriedrich/parsley/pkg/resolving" | ||
"github.com/matzefriedrich/parsley/pkg/types" | ||
) | ||
|
||
func RegisterList[T any](registry types.ServiceRegistry) error { | ||
return registry.Register(func(resolver types.Resolver) []T { | ||
services, _ := resolving.ResolveRequiredServices[T](resolver, context.Background()) | ||
return services | ||
}, types.LifetimeTransient) | ||
} |
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
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
Oops, something went wrong.