Created
October 11, 2018 11:20
-
-
Save XUJiahua/e1766a3e1d94d730d9a075ad37d9c27d to your computer and use it in GitHub Desktop.
golang wire failure case
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package foobarbaz | |
type Foo struct { | |
X int | |
} | |
// ProvideFoo returns a Foo. | |
func ProvideFoo(x int) Foo { | |
return Foo{X: x} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// +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