2018-06-19 08:04:59 -07:00
|
|
|
# Wire: Automated Initialization in Go
|
2018-03-26 07:39:00 -07:00
|
|
|
|
2018-06-19 08:04:59 -07:00
|
|
|
Wire is a code generation tool that automates connecting components using
|
|
|
|
|
[dependency injection][]. Dependencies between components are represented in
|
|
|
|
|
Wire as function parameters, encouraging explicit initialization instead of
|
|
|
|
|
global variables. Because Wire operates without runtime state or reflection,
|
|
|
|
|
code written to be used with Wire is useful even for hand-written
|
|
|
|
|
initialization.
|
2018-03-26 07:39:00 -07:00
|
|
|
|
|
|
|
|
[dependency injection]: https://en.wikipedia.org/wiki/Dependency_injection
|
|
|
|
|
|
2018-06-27 08:37:13 -07:00
|
|
|
## Installing
|
|
|
|
|
|
2018-07-23 11:07:27 -07:00
|
|
|
Install Wire by running:
|
2018-06-27 08:37:13 -07:00
|
|
|
|
|
|
|
|
```shell
|
2018-07-23 11:07:27 -07:00
|
|
|
go get github.com/google/go-cloud/wire/cmd/wire
|
2018-06-27 08:37:13 -07:00
|
|
|
```
|
|
|
|
|
|
2018-06-19 08:04:59 -07:00
|
|
|
## Basics
|
|
|
|
|
|
|
|
|
|
Wire has two core concepts: providers and injectors.
|
2018-03-26 07:39:00 -07:00
|
|
|
|
|
|
|
|
### Defining Providers
|
|
|
|
|
|
2018-09-28 10:33:08 -07:00
|
|
|
The primary mechanism in Wire is the **provider**: a function that can produce a
|
|
|
|
|
value. These functions are ordinary Go code.
|
2018-03-26 07:39:00 -07:00
|
|
|
|
|
|
|
|
```go
|
2018-03-28 17:31:32 -07:00
|
|
|
package foobarbaz
|
2018-03-26 07:39:00 -07:00
|
|
|
|
2018-06-15 15:47:43 -07:00
|
|
|
type Foo struct {
|
2018-09-28 10:33:08 -07:00
|
|
|
X int
|
2018-06-15 15:47:43 -07:00
|
|
|
}
|
2018-03-26 07:39:00 -07:00
|
|
|
|
|
|
|
|
// ProvideFoo returns a Foo.
|
|
|
|
|
func ProvideFoo() Foo {
|
2018-09-28 10:33:08 -07:00
|
|
|
return Foo{X: 42}
|
2018-03-26 07:39:00 -07:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-06-19 08:04:59 -07:00
|
|
|
Provider functions must be exported in order to be used from other packages,
|
|
|
|
|
just like ordinary functions.
|
|
|
|
|
|
2018-03-26 07:39:00 -07:00
|
|
|
Providers can specify dependencies with parameters:
|
|
|
|
|
|
|
|
|
|
```go
|
2018-03-28 17:31:32 -07:00
|
|
|
package foobarbaz
|
2018-03-26 07:39:00 -07:00
|
|
|
|
2018-04-27 13:44:54 -04:00
|
|
|
// ...
|
2018-03-26 07:39:00 -07:00
|
|
|
|
2018-06-15 15:47:43 -07:00
|
|
|
type Bar struct {
|
2018-09-28 10:33:08 -07:00
|
|
|
X int
|
2018-06-15 15:47:43 -07:00
|
|
|
}
|
2018-03-28 17:31:32 -07:00
|
|
|
|
2018-03-26 07:39:00 -07:00
|
|
|
// ProvideBar returns a Bar: a negative Foo.
|
|
|
|
|
func ProvideBar(foo Foo) Bar {
|
2018-09-28 10:33:08 -07:00
|
|
|
return Bar{X: -foo.X}
|
2018-03-26 07:39:00 -07:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Providers can also return errors:
|
|
|
|
|
|
|
|
|
|
```go
|
2018-03-28 17:31:32 -07:00
|
|
|
package foobarbaz
|
2018-03-26 07:39:00 -07:00
|
|
|
|
|
|
|
|
import (
|
2018-09-28 10:33:08 -07:00
|
|
|
"context"
|
|
|
|
|
"errors"
|
2018-03-26 07:39:00 -07:00
|
|
|
)
|
|
|
|
|
|
2018-04-27 13:44:54 -04:00
|
|
|
// ...
|
2018-03-26 07:39:00 -07:00
|
|
|
|
2018-06-15 15:47:43 -07:00
|
|
|
type Baz struct {
|
2018-09-28 10:33:08 -07:00
|
|
|
X int
|
2018-06-15 15:47:43 -07:00
|
|
|
}
|
2018-03-26 07:39:00 -07:00
|
|
|
|
|
|
|
|
// ProvideBaz returns a value if Bar is not zero.
|
|
|
|
|
func ProvideBaz(ctx context.Context, bar Bar) (Baz, error) {
|
2018-09-28 10:33:08 -07:00
|
|
|
if bar.X == 0 {
|
|
|
|
|
return Baz{}, errors.New("cannot provide baz when bar is zero")
|
|
|
|
|
}
|
|
|
|
|
return Baz{X: bar.X}, nil
|
2018-03-26 07:39:00 -07:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-06-19 08:04:59 -07:00
|
|
|
Providers can be grouped into **provider sets**. This is useful if several
|
|
|
|
|
providers will frequently be used together. To add these providers to a new set
|
|
|
|
|
called `SuperSet`, use the `wire.NewSet` function:
|
2018-03-28 17:31:32 -07:00
|
|
|
|
|
|
|
|
```go
|
2018-04-27 13:44:54 -04:00
|
|
|
package foobarbaz
|
|
|
|
|
|
|
|
|
|
import (
|
2018-09-28 10:33:08 -07:00
|
|
|
// ...
|
|
|
|
|
"github.com/google/go-cloud/wire"
|
2018-04-27 13:44:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ...
|
|
|
|
|
|
2018-05-31 15:34:15 -07:00
|
|
|
var SuperSet = wire.NewSet(ProvideFoo, ProvideBar, ProvideBaz)
|
2018-03-28 17:31:32 -07:00
|
|
|
```
|
|
|
|
|
|
2018-04-27 13:44:54 -04:00
|
|
|
You can also add other provider sets into a provider set.
|
2018-03-26 07:39:00 -07:00
|
|
|
|
|
|
|
|
```go
|
2018-04-27 13:44:54 -04:00
|
|
|
package foobarbaz
|
2018-03-26 07:39:00 -07:00
|
|
|
|
2018-04-27 13:44:54 -04:00
|
|
|
import (
|
2018-09-28 10:33:08 -07:00
|
|
|
// ...
|
|
|
|
|
"example.com/some/other/pkg"
|
2018-04-27 13:44:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ...
|
|
|
|
|
|
2018-05-31 15:34:15 -07:00
|
|
|
var MegaSet = wire.NewSet(SuperSet, pkg.OtherSet)
|
2018-04-27 13:44:54 -04:00
|
|
|
```
|
2018-03-28 17:31:32 -07:00
|
|
|
|
2018-03-26 07:39:00 -07:00
|
|
|
### Injectors
|
|
|
|
|
|
2018-03-28 17:31:32 -07:00
|
|
|
An application wires up these providers with an **injector**: a function that
|
2018-05-31 15:34:15 -07:00
|
|
|
calls providers in dependency order. With Wire, you write the injector's
|
|
|
|
|
signature, then Wire generates the function's body.
|
2018-03-26 07:39:00 -07:00
|
|
|
|
2018-04-27 13:44:54 -04:00
|
|
|
An injector is declared by writing a function declaration whose body is a call
|
2018-06-15 15:47:43 -07:00
|
|
|
to `wire.Build`. The return values don't matter as long as they are of the
|
|
|
|
|
correct type. The values themselves will be ignored in the generated code. Let's
|
|
|
|
|
say that the above providers were defined in a package called
|
|
|
|
|
`example.com/foobarbaz`. The following would declare an injector to obtain a
|
|
|
|
|
`Baz`:
|
2018-03-26 07:39:00 -07:00
|
|
|
|
|
|
|
|
```go
|
2018-05-31 15:34:15 -07:00
|
|
|
// +build wireinject
|
2018-06-19 08:04:59 -07:00
|
|
|
// The build tag makes sure the stub is not built in the final build.
|
2018-03-26 07:39:00 -07:00
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2018-09-28 10:33:08 -07:00
|
|
|
"context"
|
2018-03-26 07:39:00 -07:00
|
|
|
|
2018-09-28 10:33:08 -07:00
|
|
|
"github.com/google/go-cloud/wire"
|
|
|
|
|
"example.com/foobarbaz"
|
2018-03-26 07:39:00 -07:00
|
|
|
)
|
|
|
|
|
|
2018-07-25 14:59:52 -07:00
|
|
|
func initializeBaz(ctx context.Context) (foobarbaz.Baz, error) {
|
2018-09-28 10:33:08 -07:00
|
|
|
wire.Build(foobarbaz.MegaSet)
|
|
|
|
|
return foobarbaz.Baz{}, nil
|
2018-04-27 13:44:54 -04:00
|
|
|
}
|
2018-03-26 07:39:00 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Like providers, injectors can be parameterized on inputs (which then get sent to
|
2018-05-31 15:34:15 -07:00
|
|
|
providers) and can return errors. Arguments to `wire.Build` are the same as
|
2018-09-28 10:33:08 -07:00
|
|
|
`wire.NewSet`: they form a provider set. This is the provider set that gets used
|
|
|
|
|
during code generation for that injector.
|
2018-03-26 07:39:00 -07:00
|
|
|
|
2018-05-01 14:46:39 -04:00
|
|
|
Any non-injector declarations found in a file with injectors will be copied into
|
|
|
|
|
the generated file.
|
|
|
|
|
|
2018-07-23 11:07:27 -07:00
|
|
|
You can generate the injector by invoking Wire in the package directory:
|
2018-03-26 07:39:00 -07:00
|
|
|
|
2018-06-19 08:04:59 -07:00
|
|
|
```shell
|
2018-07-23 11:07:27 -07:00
|
|
|
wire
|
2018-03-26 07:39:00 -07:00
|
|
|
```
|
|
|
|
|
|
2018-05-31 15:34:15 -07:00
|
|
|
Wire will produce an implementation of the injector in a file called
|
|
|
|
|
`wire_gen.go` that looks something like this:
|
2018-03-26 07:39:00 -07:00
|
|
|
|
|
|
|
|
```go
|
2018-07-23 11:07:27 -07:00
|
|
|
// Code generated by Wire. DO NOT EDIT.
|
2018-03-26 07:39:00 -07:00
|
|
|
|
2018-07-23 11:07:27 -07:00
|
|
|
//go:generate wire
|
2018-05-31 15:34:15 -07:00
|
|
|
//+build !wireinject
|
2018-03-26 07:39:00 -07:00
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2018-09-28 10:33:08 -07:00
|
|
|
"example.com/foobarbaz"
|
2018-03-26 07:39:00 -07:00
|
|
|
)
|
|
|
|
|
|
2018-07-25 14:59:52 -07:00
|
|
|
func initializeBaz(ctx context.Context) (foobarbaz.Baz, error) {
|
2018-09-28 10:33:08 -07:00
|
|
|
foo := foobarbaz.ProvideFoo()
|
|
|
|
|
bar := foobarbaz.ProvideBar(foo)
|
|
|
|
|
baz, err := foobarbaz.ProvideBaz(ctx, bar)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
return baz, nil
|
2018-03-26 07:39:00 -07:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
As you can see, the output is very close to what a developer would write
|
2018-05-31 15:34:15 -07:00
|
|
|
themselves. Further, there is little dependency on Wire at runtime: all of the
|
|
|
|
|
written code is just normal Go code, and can be used without Wire.
|
2018-03-26 07:39:00 -07:00
|
|
|
|
2018-06-19 08:04:59 -07:00
|
|
|
Once `wire_gen.go` is created, you can regenerate it by running [`go generate`].
|
2018-03-26 07:39:00 -07:00
|
|
|
|
2018-06-19 08:04:59 -07:00
|
|
|
[`go generate`]: https://blog.golang.org/generate
|
2018-03-26 07:39:00 -07:00
|
|
|
|
2018-03-30 21:34:08 -07:00
|
|
|
## Advanced Features
|
|
|
|
|
|
2018-06-19 08:04:59 -07:00
|
|
|
The following features all build on top of the concepts of providers and
|
|
|
|
|
injectors.
|
|
|
|
|
|
2018-04-02 14:08:17 -07:00
|
|
|
### Binding Interfaces
|
|
|
|
|
|
2018-06-19 08:04:59 -07:00
|
|
|
Frequently, dependency injection is used to bind a concrete implementation for
|
|
|
|
|
an interface. Wire matches inputs to outputs via [type identity][], so the
|
2018-04-02 14:08:17 -07:00
|
|
|
inclination might be to create a provider that returns an interface type.
|
2018-09-28 10:33:08 -07:00
|
|
|
However, this would not be idiomatic, since the Go best practice is to
|
|
|
|
|
[return concrete types][]. Instead, you can declare an interface binding in a
|
|
|
|
|
provider set:
|
2018-04-02 14:08:17 -07:00
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
type Fooer interface {
|
2018-09-28 10:33:08 -07:00
|
|
|
Foo() string
|
2018-04-02 14:08:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Bar string
|
|
|
|
|
|
|
|
|
|
func (b *Bar) Foo() string {
|
2018-09-28 10:33:08 -07:00
|
|
|
return string(*b)
|
2018-04-02 14:08:17 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-27 13:44:54 -04:00
|
|
|
func ProvideBar() *Bar {
|
2018-09-28 10:33:08 -07:00
|
|
|
b := new(Bar)
|
|
|
|
|
*b = "Hello, World!"
|
|
|
|
|
return b
|
2018-04-02 14:08:17 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 15:34:15 -07:00
|
|
|
var BarFooer = wire.NewSet(
|
2018-09-28 10:33:08 -07:00
|
|
|
ProvideBar,
|
|
|
|
|
wire.Bind(new(Fooer), new(Bar)))
|
2018-04-02 14:08:17 -07:00
|
|
|
```
|
|
|
|
|
|
2018-05-31 15:34:15 -07:00
|
|
|
The first argument to `wire.Bind` is a pointer to a value of the desired
|
2018-09-28 10:33:08 -07:00
|
|
|
interface type and the second argument is a zero value of the concrete type. Any
|
|
|
|
|
set that includes an interface binding must also have a provider in the same set
|
|
|
|
|
that provides the concrete type.
|
2018-04-02 14:08:17 -07:00
|
|
|
|
|
|
|
|
[type identity]: https://golang.org/ref/spec#Type_identity
|
|
|
|
|
[return concrete types]: https://github.com/golang/go/wiki/CodeReviewComments#interfaces
|
|
|
|
|
|
2018-04-03 21:11:53 -07:00
|
|
|
### Struct Providers
|
|
|
|
|
|
|
|
|
|
Structs can also be marked as providers. Instead of calling a function, an
|
|
|
|
|
injector will fill in each field using the corresponding provider. For a given
|
|
|
|
|
struct type `S`, this would provide both `S` and `*S`. For example, given the
|
|
|
|
|
following providers:
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
type Foo int
|
|
|
|
|
type Bar int
|
|
|
|
|
|
2018-04-27 13:44:54 -04:00
|
|
|
func ProvideFoo() Foo {
|
2018-09-28 10:33:08 -07:00
|
|
|
// ...
|
2018-04-03 21:11:53 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-27 13:44:54 -04:00
|
|
|
func ProvideBar() Bar {
|
2018-09-28 10:33:08 -07:00
|
|
|
// ...
|
2018-04-03 21:11:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type FooBar struct {
|
2018-09-28 10:33:08 -07:00
|
|
|
Foo Foo
|
|
|
|
|
Bar Bar
|
2018-04-03 21:11:53 -07:00
|
|
|
}
|
2018-04-27 13:44:54 -04:00
|
|
|
|
2018-05-31 15:34:15 -07:00
|
|
|
var Set = wire.NewSet(
|
2018-09-28 10:33:08 -07:00
|
|
|
ProvideFoo,
|
|
|
|
|
ProvideBar,
|
|
|
|
|
FooBar{})
|
2018-04-03 21:11:53 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
A generated injector for `FooBar` would look like this:
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
func injectFooBar() FooBar {
|
2018-09-28 10:33:08 -07:00
|
|
|
foo := ProvideFoo()
|
|
|
|
|
bar := ProvideBar()
|
|
|
|
|
fooBar := FooBar{
|
|
|
|
|
Foo: foo,
|
|
|
|
|
Bar: bar,
|
|
|
|
|
}
|
|
|
|
|
return fooBar
|
2018-04-03 21:11:53 -07:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
And similarly if the injector needed a `*FooBar`.
|
|
|
|
|
|
2018-05-04 12:44:53 -04:00
|
|
|
### Binding Values
|
|
|
|
|
|
|
|
|
|
Occasionally, it is useful to bind a basic value (usually `nil`) to a type.
|
2018-09-28 10:33:08 -07:00
|
|
|
Instead of having injectors depend on a throwaway provider function, you can add
|
|
|
|
|
a value expression to a provider set.
|
2018-05-04 12:44:53 -04:00
|
|
|
|
|
|
|
|
```go
|
2018-06-15 15:47:43 -07:00
|
|
|
type Foo struct {
|
2018-09-28 10:33:08 -07:00
|
|
|
X int
|
2018-06-15 15:47:43 -07:00
|
|
|
}
|
2018-05-04 12:44:53 -04:00
|
|
|
|
|
|
|
|
func injectFoo() Foo {
|
2018-09-28 10:33:08 -07:00
|
|
|
wire.Build(wire.Value(Foo{X: 42}))
|
|
|
|
|
return Foo{}
|
2018-05-04 12:44:53 -04:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The generated injector would look like this:
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
func injectFoo() Foo {
|
2018-09-28 10:33:08 -07:00
|
|
|
foo := Foo{X: 42}
|
|
|
|
|
return foo
|
2018-05-04 12:44:53 -04:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-06-27 08:17:45 -07:00
|
|
|
It's important to note that the expression will be copied to the injector's
|
2018-09-28 10:33:08 -07:00
|
|
|
package; references to variables will be evaluated during the injector package's
|
|
|
|
|
initialization. Wire will emit an error if the expression calls any functions or
|
|
|
|
|
receives from any channels.
|
2018-05-04 12:44:53 -04:00
|
|
|
|
2018-08-14 14:55:28 -07:00
|
|
|
For interface values, use `InterfaceValue`:
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
func injectReader() io.Reader {
|
2018-09-28 10:33:08 -07:00
|
|
|
wire.Build(wire.InterfaceValue(new(io.Reader), os.Stdin))
|
|
|
|
|
return Foo{}
|
2018-08-14 14:55:28 -07:00
|
|
|
}
|
|
|
|
|
```
|
2018-09-28 10:33:08 -07:00
|
|
|
|
2018-04-03 13:13:15 -07:00
|
|
|
### Cleanup functions
|
|
|
|
|
|
|
|
|
|
If a provider creates a value that needs to be cleaned up (e.g. closing a file),
|
|
|
|
|
then it can return a closure to clean up the resource. The injector will use
|
|
|
|
|
this to either return an aggregated cleanup function to the caller or to clean
|
2018-06-19 08:04:59 -07:00
|
|
|
up the resource if a provider called later in the injector's implementation
|
|
|
|
|
returns an error.
|
2018-04-03 13:13:15 -07:00
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
func provideFile(log Logger, path Path) (*os.File, func(), error) {
|
2018-09-28 10:33:08 -07:00
|
|
|
f, err := os.Open(string(path))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
cleanup := func() {
|
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
|
log.Log(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return f, cleanup, nil
|
2018-04-03 13:13:15 -07:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
A cleanup function is guaranteed to be called before the cleanup function of any
|
|
|
|
|
of the provider's inputs and must have the signature `func()`.
|
2018-06-15 15:47:43 -07:00
|
|
|
|
|
|
|
|
### Alternate Injector Syntax
|
|
|
|
|
|
|
|
|
|
If you grow weary of writing `return foobarbaz.Foo{}, nil` at the end of your
|
|
|
|
|
injector function declaration, you can instead write it more concisely with a
|
|
|
|
|
`panic`:
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
func injectFoo() Foo {
|
2018-09-28 10:33:08 -07:00
|
|
|
panic(wire.Build(/* ... */))
|
2018-06-15 15:47:43 -07:00
|
|
|
}
|
|
|
|
|
```
|
2018-06-19 08:04:59 -07:00
|
|
|
|
|
|
|
|
## Best Practices
|
|
|
|
|
|
|
|
|
|
The following are practices we recommend for using Wire. This list will grow
|
|
|
|
|
over time.
|
|
|
|
|
|
|
|
|
|
### Distinguishing Types
|
|
|
|
|
|
2018-09-28 10:33:08 -07:00
|
|
|
If you need to inject a common type like `string`, create a new string type to
|
|
|
|
|
avoid conflicts with other providers. For example:
|
2018-06-19 08:04:59 -07:00
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
type MySQLConnectionString string
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Options Structs
|
|
|
|
|
|
|
|
|
|
A provider function that includes many dependencies can pair the function with
|
|
|
|
|
an options struct.
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
type Options struct {
|
2018-09-28 10:33:08 -07:00
|
|
|
// Messages is the set of recommended greetings.
|
|
|
|
|
Messages []Message
|
|
|
|
|
// Writer is the location to send greetings. nil goes to stdout.
|
|
|
|
|
Writer io.Writer
|
2018-06-19 08:04:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewGreeter(ctx context.Context, opts *Options) (*Greeter, error) {
|
2018-09-28 10:33:08 -07:00
|
|
|
// ...
|
2018-06-19 08:04:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var GreeterSet = wire.NewSet(Options{}, NewGreeter)
|
|
|
|
|
```
|
2018-07-12 09:04:05 -07:00
|
|
|
|
|
|
|
|
### Evolving Provider Sets
|
|
|
|
|
|
|
|
|
|
When creating a provider set for use in a library, the only changes you can make
|
|
|
|
|
without breaking compatibility are:
|
|
|
|
|
|
2018-09-28 10:33:08 -07:00
|
|
|
- Change which provider a provider set uses to provide a specific output, as
|
|
|
|
|
long as it does not introduce a new input to the provider set. It may remove
|
|
|
|
|
inputs. However, note that existing injectors will use the old provider
|
|
|
|
|
until they are regenerated.
|
|
|
|
|
- Introduce a new output type into the provider set, but only if the type
|
|
|
|
|
itself is newly added. If the type is not new, it is possible that some
|
|
|
|
|
injector already has the output type included, which would cause a conflict.
|
2018-07-12 09:04:05 -07:00
|
|
|
|
|
|
|
|
All other changes are not safe. This includes:
|
|
|
|
|
|
2018-09-28 10:33:08 -07:00
|
|
|
- Requiring a new input in the provider set.
|
|
|
|
|
- Removing an output type from a provider set.
|
|
|
|
|
- Adding an existing output type into the provider set.
|
2018-07-12 09:04:05 -07:00
|
|
|
|
|
|
|
|
Instead of making one of these breaking changes, consider adding a new provider
|
|
|
|
|
set.
|
|
|
|
|
|
|
|
|
|
As an example, if you have a provider set like this:
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
var GreeterSet = wire.NewSet(NewStdoutGreeter)
|
|
|
|
|
|
|
|
|
|
func DefaultGreeter(ctx context.Context) *Greeter {
|
2018-09-28 10:33:08 -07:00
|
|
|
// ...
|
2018-07-12 09:04:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewStdoutGreeter(ctx context.Context, msgs []Message) *Greeter {
|
2018-09-28 10:33:08 -07:00
|
|
|
// ...
|
2018-07-12 09:04:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewGreeter(ctx context.Context, w io.Writer, msgs []Message) (*Greeter, error) {
|
2018-09-28 10:33:08 -07:00
|
|
|
// ...
|
2018-07-12 09:04:05 -07:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
You may:
|
|
|
|
|
|
2018-09-28 10:33:08 -07:00
|
|
|
- Use `DefaultGreeter` instead of `NewStdoutGreeter` in `GreeterSet`.
|
|
|
|
|
- Create a new type `T` and add a provider for `T` to `GreeterSet`, as long as
|
|
|
|
|
`T` is introduced in the same commit/release as the provider is added.
|
2018-07-12 09:04:05 -07:00
|
|
|
|
|
|
|
|
You may not:
|
|
|
|
|
|
2018-09-28 10:33:08 -07:00
|
|
|
- Use `NewGreeter` instead of `NewStdoutGreeter` in `GreeterSet`. This both
|
|
|
|
|
adds an input type (`io.Writer`) and requires injectors to return an `error`
|
|
|
|
|
where the provider of `*Greeter` did not require this before.
|
|
|
|
|
- Remove `NewStdoutGreeter` from `GreeterSet`. Injectors depending on
|
|
|
|
|
`*Greeter` will be broken.
|
|
|
|
|
- Add a provider for `io.Writer` to `GreeterSet`. Injectors might already have
|
|
|
|
|
a provider for `io.Writer` which might conflict with this one.
|
2018-09-28 11:08:06 -07:00
|
|
|
|
|
|
|
|
### Mocking
|
|
|
|
|
|
|
|
|
|
There are two approaches for creating an injected app with mocked dependencies.
|
|
|
|
|
Examples of both approaches are shown
|
|
|
|
|
[here](https://github.com/google/go-cloud/tree/master/wire/internal/wire/testdata/ExampleWithMocks/foo).
|
|
|
|
|
|
|
|
|
|
#### Approach A: Pass mocks to the injector
|
|
|
|
|
|
|
|
|
|
Create a test-only injector that takes all of the mocks as arguments; the
|
|
|
|
|
argument types must be the interface types the mocks are mocking. `wire.Build`
|
|
|
|
|
can't include providers for the mocked dependencies without creating conflicts,
|
|
|
|
|
so if you're using provider set(s) you will need to define one that doesn't
|
|
|
|
|
include the mocked types.
|
|
|
|
|
|
|
|
|
|
#### Approach B: Return the mocks from the injector
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
for the concrete mock types, and use `wire.Bind` to tell Wire that the
|
|
|
|
|
concrete mock types should be used to fulfill the appropriate interface.
|