internal/wire: support specifying struct fields to inject (#147)
Added wire.Struct function and deprecate old form. Updates #36
This commit is contained in:
@@ -51,8 +51,8 @@ func main() {
|
||||
|
||||
// appSet is a provider set for creating a real app.
|
||||
var appSet = wire.NewSet(
|
||||
app{},
|
||||
greeter{},
|
||||
wire.Struct(new(app), "*"),
|
||||
wire.Struct(new(greeter), "*"),
|
||||
wire.InterfaceValue(new(timer), realTime{}),
|
||||
)
|
||||
|
||||
@@ -61,17 +61,17 @@ var appSet = wire.NewSet(
|
||||
// arguments to the injector.
|
||||
// It is used for Approach A.
|
||||
var appSetWithoutMocks = wire.NewSet(
|
||||
app{},
|
||||
greeter{},
|
||||
wire.Struct(new(app), "*"),
|
||||
wire.Struct(new(greeter), "*"),
|
||||
)
|
||||
|
||||
// mockAppSet is a provider set for creating a mocked app, including the mocked
|
||||
// dependencies.
|
||||
// It is used for Approach B.
|
||||
var mockAppSet = wire.NewSet(
|
||||
app{},
|
||||
greeter{},
|
||||
appWithMocks{},
|
||||
wire.Struct(new(app), "*"),
|
||||
wire.Struct(new(greeter), "*"),
|
||||
wire.Struct(new(appWithMocks), "*"),
|
||||
// For each mocked dependency, add a provider and use wire.Bind to bind
|
||||
// the concrete type to the relevant interface.
|
||||
newMockTimer,
|
||||
|
||||
@@ -27,7 +27,7 @@ import (
|
||||
|
||||
func newBazService(*baz.Config) *baz.Service {
|
||||
wire.Build(
|
||||
baz.Service{},
|
||||
wire.Struct(new(baz.Service), "*"),
|
||||
wire.FieldsOf(
|
||||
new(*baz.Config),
|
||||
"Foo",
|
||||
|
||||
@@ -27,7 +27,7 @@ import (
|
||||
|
||||
func newBazService() *baz.Service {
|
||||
wire.Build(
|
||||
baz.Service{},
|
||||
wire.Struct(new(baz.Service), "*"),
|
||||
wire.Value(&baz.Config{
|
||||
Foo: &foo.Config{1},
|
||||
Bar: &bar.Config{2},
|
||||
|
||||
@@ -43,7 +43,7 @@ func (m *MainService) String() string {
|
||||
|
||||
func newMainService(MainConfig) *MainService {
|
||||
wire.Build(
|
||||
MainService{},
|
||||
wire.Struct(new(MainService), "Foo", "Bar", "baz"),
|
||||
wire.FieldsOf(
|
||||
new(MainConfig),
|
||||
"Foo",
|
||||
|
||||
9
internal/wire/testdata/Struct/foo/foo.go
vendored
9
internal/wire/testdata/Struct/foo/foo.go
vendored
@@ -22,7 +22,9 @@ import (
|
||||
|
||||
func main() {
|
||||
fb := injectFooBar()
|
||||
pfb := injectPartFooBar()
|
||||
fmt.Println(fb.Foo, fb.Bar)
|
||||
fmt.Println(pfb.Foo, pfb.Bar)
|
||||
}
|
||||
|
||||
type Foo int
|
||||
@@ -42,6 +44,11 @@ func provideBar() Bar {
|
||||
}
|
||||
|
||||
var Set = wire.NewSet(
|
||||
FooBar{},
|
||||
wire.Struct(new(FooBar), "*"),
|
||||
provideFoo,
|
||||
provideBar)
|
||||
|
||||
var PartSet = wire.NewSet(
|
||||
wire.Struct(new(FooBar), "Foo"),
|
||||
provideFoo,
|
||||
)
|
||||
|
||||
5
internal/wire/testdata/Struct/foo/wire.go
vendored
5
internal/wire/testdata/Struct/foo/wire.go
vendored
@@ -24,3 +24,8 @@ func injectFooBar() FooBar {
|
||||
wire.Build(Set)
|
||||
return FooBar{}
|
||||
}
|
||||
|
||||
func injectPartFooBar() FooBar {
|
||||
wire.Build(PartSet)
|
||||
return FooBar{}
|
||||
}
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
41 1
|
||||
41 0
|
||||
|
||||
@@ -16,3 +16,11 @@ func injectFooBar() FooBar {
|
||||
}
|
||||
return fooBar
|
||||
}
|
||||
|
||||
func injectPartFooBar() FooBar {
|
||||
foo := provideFoo()
|
||||
fooBar := FooBar{
|
||||
Foo: foo,
|
||||
}
|
||||
return fooBar
|
||||
}
|
||||
|
||||
@@ -45,6 +45,6 @@ func provideBar() Bar {
|
||||
}
|
||||
|
||||
var Set = wire.NewSet(
|
||||
FooBar{},
|
||||
wire.Struct(new(FooBar), "*"),
|
||||
provideFoo,
|
||||
provideBar)
|
||||
|
||||
@@ -26,6 +26,6 @@ func injectFooBar() *FooBar {
|
||||
}
|
||||
|
||||
func injectEmptyStruct() *Empty {
|
||||
wire.Build(Empty{})
|
||||
wire.Build(wire.Struct(new(Empty)))
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user