From 26474bfd61c0e43c47ba3beaa42b85d11e588edb Mon Sep 17 00:00:00 2001 From: Dominik Date: Thu, 28 Apr 2022 17:53:49 +0200 Subject: [PATCH] refactor: some fixes --- README.md | 7 +++++-- arg.go | 29 +++++++++++++++++++++-------- container.go | 2 +- container_test.go | 2 +- error.go | 2 -- errortype_string.go | 8 +++----- examples/simple/main.go | 6 +++--- no_parameter_provider.go | 4 ++-- service_def_example_test.go | 2 +- service_def_test.go | 2 +- 10 files changed, 38 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 17ad5e0..114fb51 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,6 @@ See also /examples directory package main import ( - "context" "fmt" "github.com/dtomasi/di" "log" @@ -51,12 +50,16 @@ func BuildContainer() (*di.Container, error) { // Injects the whole DI Container di.ContainerArg(), // Injects another service - di.ServiceArg(di.StringRef("OtherService")), + di.ServiceRefArg(di.StringRef("OtherService")), + // Inject multiple services by tag + di.ServicesByTagsArg([]fmt.Stringer{di.StringRef("foo")}), // Injects a value using interface{} di.InterfaceArg(true), // Injects a parameter from registered provider // via Get(key string) (interface{}, error) di.ParamArg("foo.bar.baz"), + // Inject Event Bus + di.EventBusArg(), ), ) diff --git a/arg.go b/arg.go index 3f218a8..448f14f 100644 --- a/arg.go +++ b/arg.go @@ -2,8 +2,9 @@ package di import "fmt" + type ServiceDefArg interface { - Evaluate(*Container) (interface{}, error) + evaluate(*Container) (interface{}, error) } // Interface Arg @@ -12,7 +13,8 @@ type interfaceArg struct { inValue interface{} } -func (a *interfaceArg) Evaluate(_ *Container) (interface{}, error) { + +func (a *interfaceArg) evaluate(_ *Container) (interface{}, error) { return a.inValue, nil } @@ -27,10 +29,12 @@ type serviceRefArg struct { ref fmt.Stringer } -func (a *serviceRefArg) Evaluate(c *Container) (interface{}, error) { + +func (a *serviceRefArg) evaluate(c *Container) (interface{}, error) { return c.Get(a.ref) } + func ServiceRefArg(ref fmt.Stringer) ServiceDefArg { return &serviceRefArg{ref: ref} } @@ -45,7 +49,8 @@ type servicesByTagArg struct { tags []fmt.Stringer } -func (a *servicesByTagArg) Evaluate(c *Container) (interface{}, error) { + +func (a *servicesByTagArg) evaluate(c *Container) (interface{}, error) { return c.FindByTags(a.tags) } @@ -59,10 +64,12 @@ type paramArg struct { paramPath string } -func (a *paramArg) Evaluate(c *Container) (interface{}, error) { + +func (a *paramArg) evaluate(c *Container) (interface{}, error) { return c.paramProvider.Get(a.paramPath) } + func ParamArg(paramPath string) ServiceDefArg { return ¶mArg{paramPath: paramPath} } @@ -70,10 +77,12 @@ func ParamArg(paramPath string) ServiceDefArg { // Context Argument injects the context from di.Container. type contextArg struct{} -func (a *contextArg) Evaluate(c *Container) (interface{}, error) { + +func (a *contextArg) evaluate(c *Container) (interface{}, error) { return c.ctx, nil } + func ContextArg() ServiceDefArg { return &contextArg{} } @@ -81,10 +90,12 @@ func ContextArg() ServiceDefArg { // Container Argument injects the container from di.Container. type containerArg struct{} -func (a *containerArg) Evaluate(c *Container) (interface{}, error) { + +func (a *containerArg) evaluate(c *Container) (interface{}, error) { return c, nil } + func ContainerArg() ServiceDefArg { return &containerArg{} } @@ -92,10 +103,12 @@ func ContainerArg() ServiceDefArg { // EventBus Argument injects the eventBus from di.EventBus. type eventBusArg struct{} -func (a *eventBusArg) Evaluate(c *Container) (interface{}, error) { + +func (a *eventBusArg) evaluate(c *Container) (interface{}, error) { return c.eventBus, nil } + func EventBusArg() ServiceDefArg { return &eventBusArg{} } diff --git a/container.go b/container.go index 4380f7d..b4d7f65 100644 --- a/container.go +++ b/container.go @@ -304,7 +304,7 @@ func (c *Container) parseArgs(def *ServiceDef) (args []interface{}, err error) { for _, v := range def.args { var val interface{} - val, err = v.Evaluate(c) + val, err = v.evaluate(c) if err != nil { return } diff --git a/container_test.go b/container_test.go index 19337cd..40d8c60 100644 --- a/container_test.go +++ b/container_test.go @@ -111,7 +111,7 @@ func BuildContainer() (*di.Container, error) { di.NewServiceDef(di.StringRef("TestService2")). Provider(NewTestService2). Args( - di.ServiceArg(di.StringRef("TestService1")), + di.ServiceRefArg(di.StringRef("TestService1")), di.InterfaceArg(true), di.ParamArg("foo.bar.baz"), ). diff --git a/error.go b/error.go index 3e148a3..da30172 100644 --- a/error.go +++ b/error.go @@ -14,7 +14,5 @@ const ( ProviderToManyReturnValuesError ProviderArgCountMismatchError ProviderArgTypeMismatchError - ArgParsingEventError - ParamProviderGetError ParamProviderNotDefinedError ) diff --git a/errortype_string.go b/errortype_string.go index dbc43b9..f413874 100644 --- a/errortype_string.go +++ b/errortype_string.go @@ -16,14 +16,12 @@ func _() { _ = x[ProviderToManyReturnValuesError-5] _ = x[ProviderArgCountMismatchError-6] _ = x[ProviderArgTypeMismatchError-7] - _ = x[ArgParsingEventError-8] - _ = x[ParamProviderGetError-9] - _ = x[ParamProviderNotDefinedError-10] + _ = x[ParamProviderNotDefinedError-8] } -const _ErrorType_name = "ContainerBuildErrorServiceNotFoundErrorServiceBuildErrorProviderMissingErrorProviderNotAFuncErrorProviderToManyReturnValuesErrorProviderArgCountMismatchErrorProviderArgTypeMismatchErrorArgParsingEventErrorParamProviderGetErrorParamProviderNotDefinedError" +const _ErrorType_name = "ContainerBuildErrorServiceNotFoundErrorServiceBuildErrorProviderMissingErrorProviderNotAFuncErrorProviderToManyReturnValuesErrorProviderArgCountMismatchErrorProviderArgTypeMismatchErrorParamProviderNotDefinedError" -var _ErrorType_index = [...]uint8{0, 19, 39, 56, 76, 97, 128, 157, 185, 205, 226, 254} +var _ErrorType_index = [...]uint8{0, 19, 39, 56, 76, 97, 128, 157, 185, 213} func (i ErrorType) String() string { if i < 0 || i >= ErrorType(len(_ErrorType_index)-1) { diff --git a/examples/simple/main.go b/examples/simple/main.go index 9929d09..745e821 100644 --- a/examples/simple/main.go +++ b/examples/simple/main.go @@ -100,14 +100,14 @@ func main() { Provider(greeter.NewGreeter). Args( di.ContextArg(), - di.ServiceArg(di.LoggerService), + di.ServiceRefArg(di.LoggerService), di.ParamArg("morning"), ), di.NewServiceDef(greeter.ServiceGreeterAfternoon). Provider(greeter.NewGreeter). Args( di.ContextArg(), - di.ServiceArg(di.LoggerService), + di.ServiceRefArg(di.LoggerService), di.ParamArg("afternoon"), ), di.NewServiceDef(greeter.ServiceGreeterEvening). @@ -118,7 +118,7 @@ func main() { Provider(greeter.NewGreeter). Args( di.ContextArg(), - di.ServiceArg(di.LoggerService), + di.ServiceRefArg(di.LoggerService), di.ParamArg("evening"), ), ) diff --git a/no_parameter_provider.go b/no_parameter_provider.go index b05bacd..8f0b221 100644 --- a/no_parameter_provider.go +++ b/no_parameter_provider.go @@ -5,11 +5,11 @@ import z "github.com/dtomasi/zerrors" // NoParameterProvider is a provider that is set by default. type NoParameterProvider struct{} -func (p *NoParameterProvider) Get(key string) (interface{}, error) { +func (p *NoParameterProvider) Get(_ string) (interface{}, error) { // Just return nil to not break call to Get if no parameter provider is set. return nil, z.NewWithOpts("provider not defined", z.WithType(ParamProviderNotDefinedError)) } -func (p *NoParameterProvider) Set(key string, value interface{}) error { +func (p *NoParameterProvider) Set(_ string, _ interface{}) error { // Same as above for the Setter here return z.NewWithOpts("provider not defined", z.WithType(ParamProviderNotDefinedError)) } diff --git a/service_def_example_test.go b/service_def_example_test.go index 77442b1..fce4c10 100644 --- a/service_def_example_test.go +++ b/service_def_example_test.go @@ -21,7 +21,7 @@ func ExampleNewServiceDef() { // This magic Arg injects the context provided with di container di.ContextArg(), // This magic Arg injects the logger provided with di container - di.ServiceArg(di.LoggerService), + di.ServiceRefArg(di.LoggerService), // Interface Arg can be used for any value type. The passed value must match the target value type // in the provider function di.InterfaceArg("Hello, "), diff --git a/service_def_test.go b/service_def_test.go index d7a4d25..c1c720a 100644 --- a/service_def_test.go +++ b/service_def_test.go @@ -16,7 +16,7 @@ func TestNewServiceDef(t *testing.T) { di.ContextArg(), di.ContainerArg(), di.InterfaceArg(""), - di.ServiceArg(di.StringRef("bar")), + di.ServiceRefArg(di.StringRef("bar")), di.ParamArg(""), ). Tags(di.StringRef("foo"))