Skip to content
This repository has been archived by the owner on Oct 21, 2023. It is now read-only.

Commit

Permalink
refactor: some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dtomasi committed Apr 28, 2022
1 parent 7f63245 commit 26474bf
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 26 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ See also /examples directory
package main

import (
"context"
"fmt"
"github.com/dtomasi/di"
"log"
Expand Down Expand Up @@ -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(),
),
)

Expand Down
29 changes: 21 additions & 8 deletions arg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package di

import "fmt"


type ServiceDefArg interface {
Evaluate(*Container) (interface{}, error)
evaluate(*Container) (interface{}, error)
}

// Interface Arg
Expand All @@ -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
}

Expand All @@ -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}
}
Expand All @@ -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)
}

Expand All @@ -59,43 +64,51 @@ 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 &paramArg{paramPath: paramPath}
}

// 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{}
}

// 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{}
}

// 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{}
}
2 changes: 1 addition & 1 deletion container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
).
Expand Down
2 changes: 0 additions & 2 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@ const (
ProviderToManyReturnValuesError
ProviderArgCountMismatchError
ProviderArgTypeMismatchError
ArgParsingEventError
ParamProviderGetError
ParamProviderNotDefinedError
)
8 changes: 3 additions & 5 deletions errortype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -118,7 +118,7 @@ func main() {
Provider(greeter.NewGreeter).
Args(
di.ContextArg(),
di.ServiceArg(di.LoggerService),
di.ServiceRefArg(di.LoggerService),
di.ParamArg("evening"),
),
)
Expand Down
4 changes: 2 additions & 2 deletions no_parameter_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
2 changes: 1 addition & 1 deletion service_def_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "),
Expand Down
2 changes: 1 addition & 1 deletion service_def_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit 26474bf

Please # to comment.