internal/wire: support specifying struct fields to inject (#147)

Added wire.Struct function and deprecate old form.

Updates #36
This commit is contained in:
shantuo
2019-03-28 09:00:20 -07:00
committed by Ross Light
parent aeaafca1a6
commit 65ae46b7ea
12 changed files with 155 additions and 23 deletions

View File

@@ -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,

View File

@@ -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",

View File

@@ -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},

View File

@@ -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",

View File

@@ -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,
)

View File

@@ -24,3 +24,8 @@ func injectFooBar() FooBar {
wire.Build(Set)
return FooBar{}
}
func injectPartFooBar() FooBar {
wire.Build(PartSet)
return FooBar{}
}

View File

@@ -1 +1,2 @@
41 1
41 0

View File

@@ -16,3 +16,11 @@ func injectFooBar() FooBar {
}
return fooBar
}
func injectPartFooBar() FooBar {
foo := provideFoo()
fooBar := FooBar{
Foo: foo,
}
return fooBar
}

View File

@@ -45,6 +45,6 @@ func provideBar() Bar {
}
var Set = wire.NewSet(
FooBar{},
wire.Struct(new(FooBar), "*"),
provideFoo,
provideBar)

View File

@@ -26,6 +26,6 @@ func injectFooBar() *FooBar {
}
func injectEmptyStruct() *Empty {
wire.Build(Empty{})
wire.Build(wire.Struct(new(Empty)))
return nil
}