wire
wire copied to clipboard
"multiple binding" error throws, When generating wire_gen.go.
Describe the bug
scene one, there are three rules given:
- A depends on B and C
- B depends on D
- C depends on D
in scene one, we can know that there are only one D rather than two. but i got an "multiple bindings" error
scene two, there are also two rules given:
- A depends on B and C
- B and C depends on D
actually scene two can be resolved to the same dependencies. but only the rules in scene two work when generating wire_gen.go
To Reproduce
- provider.go
type GrandParent struct {
pta *ParentA
ptb *ParentB
}
type ParentA struct {
son *Son
}
type ParentB struct {
son *Son
}
type Son struct {
name string
}
var (
son = Son{"singleton"}
pta = ParentA{}
ptb = ParentB{}
ProvideParentASet = wire.NewSet(pta, son)
ProvideParentBSet = wire.NewSet(ptb, son)
ProvideGrandParent = wire.NewSet(ProvideParentASet, ProvideParentBSet)
)
- wire.go
func InjectGrandParent() *GrandParent {
panic(wire.Build(ProvideGrandParent))
}
when run wire, error throws
wire: /Users/duncan/gitRepo/yet-another/provider.go:62:23: ProvideGrandParent has multiple bindings for e.coding.net/codingcorp/coding-ci/yet-another.Son
current:
<- struct provider "Son" (/Users/duncan/gitRepo/yet-another/provider.go:52:6)
<- provider set "ProvideParentBSet" (/Users/duncan/gitRepo/yet-another/provider.go:61:23)
previous:
<- struct provider "Son" (/Users/duncan/gitRepo/yet-another/provider.go:52:6)
<- provider set "ProvideParentASet" (/Users/duncan/gitRepo/yet-another/provider.go:60:23)
wire: /Users/duncan/gitRepo/yet-another/provider.go:62:23: ProvideGrandParent has multiple bindings for *e.coding.net/codingcorp/coding-ci/yet-another.Son
current:
<- struct provider "Son" (/Users/duncan/gitRepo/yet-another/provider.go:52:6)
<- provider set "ProvideParentBSet" (/Users/duncan/gitRepo/yet-another/provider.go:61:23)
previous:
<- struct provider "Son" (/Users/duncan/gitRepo/yet-another/provider.go:52:6)
<- provider set "ProvideParentASet" (/Users/duncan/gitRepo/yet-another/provider.go:60:23)
Expected behavior
I think rules in scene one are supposed to be work.
Version
Which version of Wire are you seeing the bug with?
v0.5.0
Additional context
Add any other context about the problem here.
if you should write provider.go file
package wire_test
import "github.com/google/wire"
type GrandParent struct {
pta *ParentA
ptb *ParentB
}
type ParentA struct {
son *Son
}
type ParentB struct {
son *Son
}
type Son struct {
name string
}
var (
son = Son{"singleton"}
//pta = ParentA{}
//ptb = ParentB{}
ProvideParentASet = wire.Struct(new(ParentA), "*")
ProvideParentBSet = wire.Struct(new(ParentB), "*")
ProvideGrandParent = wire.NewSet(wire.Struct(new(GrandParent), "*"),
ProvideParentASet, ProvideParentBSet, wire.Struct(new(Son), "*"))
)
wire.go
//go:build wireinject
// +build wireinject
package wire_test
import "github.com/google/wire"
func InjectGrandParent(key string) *GrandParent {
panic(wire.Build(ProvideGrandParent))
}
you can generating wire_gen.go
your provider.go , not use wire.Struct instead,
son = Son{"singleton"}
pta = ParentA{}
ptb = ParentB{}
ProvideParentASet = wire.NewSet(pta, **son**)
ProvideParentBSet = wire.NewSet(ptb, **son**)
ProvideGrandParent = wire.NewSet(ProvideParentASet, ProvideParentBSet)
- "multiple binding" error throws, because you write son, you can wire son example:
ProvideParentASet = wire.NewSet(pta)
ProvideParentBSet = wire.NewSet(ptb)
ProvideGrandParent = wire.NewSet(ProvideParentASet, ProvideParentBSet, son)
- you don't use struct in NewSet(), you should use func or wire.Struct().
https://github.com/google/wire/blob/main/docs/guide.md