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

Commit

Permalink
refactor: back to original ServiceArg
Browse files Browse the repository at this point in the history
  • Loading branch information
dtomasi committed Apr 28, 2022
1 parent 26474bf commit e473711
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func BuildContainer() (*di.Container, error) {
// Injects the whole DI Container
di.ContainerArg(),
// Injects another service
di.ServiceRefArg(di.StringRef("OtherService")),
di.ServiceArg(di.StringRef("OtherService")),
// Inject multiple services by tag
di.ServicesByTagsArg([]fmt.Stringer{di.StringRef("foo")}),
// Injects a value using interface{}
Expand Down
22 changes: 3 additions & 19 deletions arg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package di

import "fmt"


type ServiceDefArg interface {
evaluate(*Container) (interface{}, error)
}
Expand All @@ -13,7 +12,6 @@ type interfaceArg struct {
inValue interface{}
}


func (a *interfaceArg) evaluate(_ *Container) (interface{}, error) {
return a.inValue, nil
}
Expand All @@ -29,32 +27,25 @@ type serviceRefArg struct {
ref fmt.Stringer
}


func (a *serviceRefArg) evaluate(c *Container) (interface{}, error) {
return c.Get(a.ref)
}


func ServiceRefArg(ref fmt.Stringer) ServiceDefArg {
return &serviceRefArg{ref: ref}
}

// Deprecated: use ServiceRefArg.
func ServiceArg(ref fmt.Stringer) ServiceDefArg {
return ServiceRefArg(ref)
return &serviceRefArg{ref: ref}
}

// Services By Tag Arg allows to get one or more services by tags and inject them.
type servicesByTagArg struct {
tags []fmt.Stringer
}


func (a *servicesByTagArg) evaluate(c *Container) (interface{}, error) {
return c.FindByTags(a.tags)
}

// ServicesByTagsArg is a shortcut for a service argument.
//goland:noinspection GoUnusedExportedFunction
func ServicesByTagsArg(tags []fmt.Stringer) ServiceDefArg {
return &servicesByTagArg{tags: tags}
}
Expand All @@ -64,51 +55,44 @@ type paramArg struct {
paramPath string
}


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) {
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) {
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) {
return c.eventBus, nil
}


//goland:noinspection GoUnusedExportedFunction
func EventBusArg() ServiceDefArg {
return &eventBusArg{}
}
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.ServiceRefArg(di.StringRef("TestService1")),
di.ServiceArg(di.StringRef("TestService1")),
di.InterfaceArg(true),
di.ParamArg("foo.bar.baz"),
).
Expand Down
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.ServiceRefArg(di.LoggerService),
di.ServiceArg(di.LoggerService),
di.ParamArg("morning"),
),
di.NewServiceDef(greeter.ServiceGreeterAfternoon).
Provider(greeter.NewGreeter).
Args(
di.ContextArg(),
di.ServiceRefArg(di.LoggerService),
di.ServiceArg(di.LoggerService),
di.ParamArg("afternoon"),
),
di.NewServiceDef(greeter.ServiceGreeterEvening).
Expand All @@ -118,7 +118,7 @@ func main() {
Provider(greeter.NewGreeter).
Args(
di.ContextArg(),
di.ServiceRefArg(di.LoggerService),
di.ServiceArg(di.LoggerService),
di.ParamArg("evening"),
),
)
Expand Down
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.ServiceRefArg(di.LoggerService),
di.ServiceArg(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.ServiceRefArg(di.StringRef("bar")),
di.ServiceArg(di.StringRef("bar")),
di.ParamArg(""),
).
Tags(di.StringRef("foo"))
Expand Down

0 comments on commit e473711

Please # to comment.