Skip to content

Instantly share code, notes, and snippets.

@XUJiahua
Created October 11, 2018 11:20
Show Gist options
  • Save XUJiahua/e1766a3e1d94d730d9a075ad37d9c27d to your computer and use it in GitHub Desktop.
Save XUJiahua/e1766a3e1d94d730d9a075ad37d9c27d to your computer and use it in GitHub Desktop.
golang wire failure case
package foobarbaz
type Bar struct {
X int
Y int
}
// ProvideBar returns a Bar: a negative Foo.
func ProvideBar(foo Foo, y int) Bar {
return Bar{X: -foo.X, Y: y}
}
package foobarbaz
import (
"context"
"errors"
"github.com/google/go-cloud/wire"
)
// ...
type Baz struct {
X int
}
// ProvideBaz returns a value if Bar is not zero.
func ProvideBaz(ctx context.Context, bar Bar) (Baz, error) {
if bar.X == 0 {
return Baz{}, errors.New("cannot provide baz when bar is zero")
}
return Baz{X: bar.X}, nil
}
var SuperSet = wire.NewSet(ProvideFoo, ProvideBar, ProvideBaz)
package foobarbaz
type Foo struct {
X int
}
// ProvideFoo returns a Foo.
func ProvideFoo(x int) Foo {
return Foo{X: x}
}
// +build wireinject
// The build tag makes sure the stub is not built in the final build.
package inject_
import (
"context"
"github.com/google/go-cloud/wire"
"github.com/XUJiahua/tutorial/foobarbaz"
)
// x是给foo的,y是给bar的,但是wire无法处理
// inject initializeBaz: multiple inputs of the same type int
func initializeBaz(ctx context.Context, x, y int) (foobarbaz.Baz, error) {
wire.Build(foobarbaz.SuperSet)
return foobarbaz.Baz{}, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment