I am using Google wire to wire up the dependencies in one of my Go projects. So far all the dependencies were wired up successfully.
I have following container:
type Container struct {
Logger logger.StructuredLogger
IDGenerator idgenerator.IDGenerator
Arango arangodb.Arango
}
Here StructuredLogger
, IDGenerator
and Arango
are interface.
The container set is defined as below:
var mockContainerSet = wire.NewSet(
Container{},
ResolveStructuredLogger,
ResolveIDGenerator,
ResolveArangoDB,
)
Functions:
func ResolveStructuredLogger() logger.StructuredLogger {
panic(wire.Build(wire.InterfaceValue(new(logger.StructuredLogger), logrusprovider.NewLogrusLogger(nil))))
}
func ResolveIDGenerator() idgenerator.IDGenerator {
panic(wire.Build(wire.InterfaceValue(new(idgenerator.IDGenerator), examples.NewKSUIDGenerator())))
}
Now the issue is when I want to create a provider for Arango
. The factory method for Arango
provider is taking IDGenerator
as an argument.
func NewMockedArango(generator idgenerator.IDGenerator) (*MockedArango, error){
if generator == nil {
return nil , errors.New("mock arangodb expects a valid IDGenerator")
}
return &MockedArango{generator:generator}, nil
}
All the functions from arangodb.Arango
is implemented. And I tried the following to create a provider:
func ResolveMockArangoDB(idGenerator idgenerator.IDGenerator) (arangodb.Arango, error) {
panic(wire.Build(wire.InterfaceValue(new(arangodb.Arango), mockdb.NewMockedArango(idGenerator))))
}
But the wire is throwing errors, I tried a lot of ways and no success to set this up. Any help?
Error:
2-valued mockdb.NewMockedArango(idGenerator) (value of type (*github.com/tejashwi/catalog-manager-svc/pkg/webservice/arangodb/mockdb.MockedArango, error)) where single value is expected
Signature of wire.Interface is InterfaceValue(typ interface{}, x interface{})
Here you need to pass two single-valued arguments. In the second argument, you are passing 2-valued argument since function mockdb.NewMockedArango
is returning 2 values.
You can change function ResolveMockArangoDB to:
func ResolveMockArangoDB(idGenerator idgenerator.IDGenerator) (arangodb.Arango, error) {
mockedArango, err := mockdb.NewMockedArango(idGenerator)
<HANDLE err>
panic(wire.Build(wire.InterfaceValue(new(arangodb.Arango), mockedArango)))
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.