Skip to content

Commit

Permalink
fix: prefer drivers in DI
Browse files Browse the repository at this point in the history
  • Loading branch information
Reasno committed Sep 13, 2021
1 parent 3f7fc7e commit ac72975
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
19 changes: 19 additions & 0 deletions dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ func newDefaultDriver(args DriverArgs) (Driver, error) {
if err := args.Populator.Populate(&injected); err != nil {
return nil, fmt.Errorf("missing dependency for the default queue driver: %w", err)
}
driver, err := driverFromDI(args.Populator)
if err != nil {
return nil, fmt.Errorf("error fetching default driver from DI: %w", err)
}
if driver != nil {
return driver, nil
}
var redisName string
if err := injected.ConfigUnmarshaler.Unmarshal(fmt.Sprintf("queue.%s.redisName", injected.AppName), &redisName); err != nil {
return nil, fmt.Errorf("bad configuration: %w", err)
Expand Down Expand Up @@ -257,3 +264,15 @@ func provideConfig() configOut {
}}
return configOut{Config: configs}
}

func driverFromDI(populator contract.DIPopulator) (Driver, error) {
var injected struct {
di.In
Driver `optional:"true"`
}
err := populator.Populate(&injected)
if err != nil {
return nil, err
}
return injected.Driver, nil
}
20 changes: 20 additions & 0 deletions dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,23 @@ func TestProvideConfigs(t *testing.T) {
c := provideConfig()
assert.NotEmpty(t, c.Config)
}

type driverPopulator struct{}

func (d driverPopulator) Populate(target interface{}) error {
graph := di.NewGraph()
graph.Provide(func() Driver {
return mockDriver{}
})
di.IntoPopulator(graph).Populate(target)
return nil
}

func TestDriverFromDI(t *testing.T) {
driver, err := newDefaultDriver(DriverArgs{
Name: "",
Populator: driverPopulator{},
})
assert.NoError(t, err)
assert.IsType(t, mockDriver{}, driver)
}

0 comments on commit ac72975

Please sign in to comment.