docs: update docs with the bind and struct changes (#155)

This commit is contained in:
shantuo
2019-04-23 10:00:57 -07:00
committed by GitHub
parent 63cf8b9f27
commit b1c66a196d
2 changed files with 45 additions and 39 deletions

View File

@@ -29,7 +29,7 @@ func NewGreeter(ctx context.Context, opts *Options) (*Greeter, error) {
// ... // ...
} }
var GreeterSet = wire.NewSet(Options{}, NewGreeter) var GreeterSet = wire.NewSet(wire.Struct(new(Options), "*"), NewGreeter)
``` ```
## Provider Sets in Libraries ## Provider Sets in Libraries
@@ -117,5 +117,5 @@ include the mocked types.
Create a new struct that includes the app plus all of the dependencies you want Create a new struct that includes the app plus all of the dependencies you want
to mock. Create a test-only injector that returns this struct, give it providers to mock. Create a test-only injector that returns this struct, give it providers
for the concrete mock types, and use `wire.Bind` to tell Wire that the for the concrete mock types, and use `wire.Bind` to tell Wire that the concrete
concrete mock types should be used to fulfill the appropriate interface. mock types should be used to fulfill the appropriate interface.

View File

@@ -219,63 +219,43 @@ func provideBar(f Fooer) string {
var Set = wire.NewSet( var Set = wire.NewSet(
provideMyFooer, provideMyFooer,
wire.Bind((*Fooer)(nil), (*MyFooer)(nil)), wire.Bind(new(Fooer), new(*MyFooer)),
provideBar) provideBar)
``` ```
The first argument to `wire.Bind` is a pointer to a value of the desired The first argument to `wire.Bind` is a pointer to a value of the desired
interface type and the second argument is a zero value of the concrete type. Any interface type and the second argument is a pointer to a value of the type that
set that includes an interface binding must also have a provider in the same set implements the interface. Any set that includes an interface binding must also
that provides the concrete type. have a provider in the same set that provides the concrete type.
If necessary, you can also bind one interface to another:
```go
type FooerPlus interface {
Fooer
Bar() String
}
func ProvideFooerPlus() FooerPlus {
...
}
var FooerPlusAsFooer = wire.NewSet(
ProvideFooerPlus,
wire.Bind(new(Fooer), *new(FooerPlus)))
```
[type identity]: https://golang.org/ref/spec#Type_identity [type identity]: https://golang.org/ref/spec#Type_identity
[return concrete types]: https://github.com/golang/go/wiki/CodeReviewComments#interfaces [return concrete types]: https://github.com/golang/go/wiki/CodeReviewComments#interfaces
### Struct Providers ### Struct Providers
Structs can also be marked as providers. Instead of calling a function, an Structs can also be marked as providers. Use the `wire.Struct` function to
injector will fill in each field using the corresponding provider. For a given inject a struct type and tell the injector which field(s) should be injected.
struct type `S`, this would provide both `S` and `*S`. For example, given the The injector will fill in each field using the provider for the field's type.
following providers: For a given struct type `S`, this would provide both `S` and `*S`. For example,
given the following providers:
```go ```go
type Foo int type Foo int
type Bar int type Bar int
func ProvideFoo() Foo { func ProvideFoo() Foo {/* ... */}
// ...
}
func ProvideBar() Bar { func ProvideBar() Bar {/* ... */}
// ...
}
type FooBar struct { type FooBar struct {
Foo Foo MyFoo Foo
Bar Bar MyBar Bar
} }
var Set = wire.NewSet( var Set = wire.NewSet(
ProvideFoo, ProvideFoo,
ProvideBar, ProvideBar,
FooBar{}) wire.Struct(new(FooBar), "MyFoo", "MyBar"))
``` ```
A generated injector for `FooBar` would look like this: A generated injector for `FooBar` would look like this:
@@ -285,8 +265,34 @@ func injectFooBar() FooBar {
foo := ProvideFoo() foo := ProvideFoo()
bar := ProvideBar() bar := ProvideBar()
fooBar := FooBar{ fooBar := FooBar{
Foo: foo, MyFoo: foo,
Bar: bar, MyBar: bar,
}
return fooBar
}
```
The first argument to `wire.Struct` is a pointer to the desired struct type and
the subsequent arguments are the names of fields to be injected. A special
string `"*"` can be used as a shortcut to tell the injector to inject all
fields. So `wire.Struct(new(FooBar), "*")` produces the same result as above.
For the above example, you can specify only injecting `"MyFoo"` by changing the
`Set` to:
```go
var Set = wire.NewSet(
ProvideFoo,
wire.Struct(new(FooBar), "MyFoo"))
```
Then the generated injector for `FooBar` would look like this:
```go
func injectFooBar() FooBar {
foo := ProvideFoo()
fooBar := FooBar{
MyFoo: foo,
} }
return fooBar return fooBar
} }