@@ -102,7 +102,7 @@ calls providers in dependency order. With goose, you write the injector's
|
|||||||
signature, then goose generates the function's body.
|
signature, then goose generates the function's body.
|
||||||
|
|
||||||
An injector is declared by writing a function declaration whose body is a call
|
An injector is declared by writing a function declaration whose body is a call
|
||||||
to `panic()` with a call to `goose.Use` as its argument. Let's say that the
|
to `panic()` with a call to `goose.Build` as its argument. Let's say that the
|
||||||
above providers were defined in a package called `example.com/foobarbaz`. The
|
above providers were defined in a package called `example.com/foobarbaz`. The
|
||||||
following would declare an injector to obtain a `Baz`:
|
following would declare an injector to obtain a `Baz`:
|
||||||
|
|
||||||
@@ -121,12 +121,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func initializeApp(ctx context.Context) (foobarbaz.Baz, error) {
|
func initializeApp(ctx context.Context) (foobarbaz.Baz, error) {
|
||||||
panic(goose.Use(foobarbaz.MegaSet))
|
panic(goose.Build(foobarbaz.MegaSet))
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Like providers, injectors can be parameterized on inputs (which then get sent to
|
Like providers, injectors can be parameterized on inputs (which then get sent to
|
||||||
providers) and can return errors. Arguments to `goose.Use` are the same as
|
providers) and can return errors. Arguments to `goose.Build` are the same as
|
||||||
`goose.NewSet`: they form a provider set. This is the provider set that gets
|
`goose.NewSet`: they form a provider set. This is the provider set that gets
|
||||||
used during code generation for that injector.
|
used during code generation for that injector.
|
||||||
|
|
||||||
@@ -314,7 +314,7 @@ add a value expression to a provider set.
|
|||||||
type Foo int
|
type Foo int
|
||||||
|
|
||||||
func injectFoo() Foo {
|
func injectFoo() Foo {
|
||||||
panic(goose.Use(goose.Value(Foo(42))))
|
panic(goose.Build(goose.Value(Foo(42))))
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
6
goose.go
6
goose.go
@@ -25,16 +25,16 @@ func NewSet(...interface{}) ProviderSet {
|
|||||||
return ProviderSet{}
|
return ProviderSet{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use is placed in the body of an injector function to declare the
|
// Build is placed in the body of an injector function to declare the
|
||||||
// providers to use. Its arguments are the same as NewSet. Its return
|
// providers to use. Its arguments are the same as NewSet. Its return
|
||||||
// value is an error message that can be sent to panic.
|
// value is an error message that can be sent to panic.
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// func injector(ctx context.Context) (*sql.DB, error) {
|
// func injector(ctx context.Context) (*sql.DB, error) {
|
||||||
// panic(Use(otherpkg.Foo, myProviderFunc, goose.Bind()))
|
// panic(Build(otherpkg.Foo, myProviderFunc, goose.Bind()))
|
||||||
// }
|
// }
|
||||||
func Use(...interface{}) string {
|
func Build(...interface{}) string {
|
||||||
return "implementation not generated, run goose"
|
return "implementation not generated, run goose"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -92,8 +92,8 @@ func generateInjectors(g *gen, pkgInfo *loader.PackageInfo) (injectorFiles []*as
|
|||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
useCall := isInjector(&pkgInfo.Info, fn)
|
buildCall := isInjector(&pkgInfo.Info, fn)
|
||||||
if useCall == nil {
|
if buildCall == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if len(injectorFiles) == 0 || injectorFiles[len(injectorFiles)-1] != f {
|
if len(injectorFiles) == 0 || injectorFiles[len(injectorFiles)-1] != f {
|
||||||
@@ -103,7 +103,7 @@ func generateInjectors(g *gen, pkgInfo *loader.PackageInfo) (injectorFiles []*as
|
|||||||
g.p("// Injectors from %s:\n\n", name)
|
g.p("// Injectors from %s:\n\n", name)
|
||||||
injectorFiles = append(injectorFiles, f)
|
injectorFiles = append(injectorFiles, f)
|
||||||
}
|
}
|
||||||
set, err := oc.processNewSet(pkgInfo, useCall)
|
set, err := oc.processNewSet(pkgInfo, buildCall)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%v: %v", g.prog.Fset.Position(fn.Pos()), err)
|
return nil, fmt.Errorf("%v: %v", g.prog.Fset.Position(fn.Pos()), err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ import (
|
|||||||
// A ProviderSet describes a set of providers. The zero value is an empty
|
// A ProviderSet describes a set of providers. The zero value is an empty
|
||||||
// ProviderSet.
|
// ProviderSet.
|
||||||
type ProviderSet struct {
|
type ProviderSet struct {
|
||||||
// Pos is the position of the call to goose.NewSet or goose.Use that
|
// Pos is the position of the call to goose.NewSet or goose.Build that
|
||||||
// created the set.
|
// created the set.
|
||||||
Pos token.Pos
|
Pos token.Pos
|
||||||
// PkgPath is the import path of the package that declared this set.
|
// PkgPath is the import path of the package that declared this set.
|
||||||
@@ -316,7 +316,7 @@ type structProviderPair struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (oc *objectCache) processNewSet(pkg *loader.PackageInfo, call *ast.CallExpr) (*ProviderSet, error) {
|
func (oc *objectCache) processNewSet(pkg *loader.PackageInfo, call *ast.CallExpr) (*ProviderSet, error) {
|
||||||
// Assumes that call.Fun is goose.NewSet or goose.Use.
|
// Assumes that call.Fun is goose.NewSet or goose.Build.
|
||||||
|
|
||||||
pset := &ProviderSet{
|
pset := &ProviderSet{
|
||||||
Pos: call.Pos(),
|
Pos: call.Pos(),
|
||||||
@@ -558,7 +558,7 @@ func processValue(fset *token.FileSet, info *types.Info, call *ast.CallExpr) (*V
|
|||||||
}
|
}
|
||||||
|
|
||||||
// isInjector checks whether a given function declaration is an
|
// isInjector checks whether a given function declaration is an
|
||||||
// injector template, returning the goose.Use call. It returns nil if
|
// injector template, returning the goose.Build call. It returns nil if
|
||||||
// the function is not an injector template.
|
// the function is not an injector template.
|
||||||
func isInjector(info *types.Info, fn *ast.FuncDecl) *ast.CallExpr {
|
func isInjector(info *types.Info, fn *ast.FuncDecl) *ast.CallExpr {
|
||||||
if fn.Body == nil {
|
if fn.Body == nil {
|
||||||
@@ -595,15 +595,15 @@ func isInjector(info *types.Info, fn *ast.FuncDecl) *ast.CallExpr {
|
|||||||
if len(panicCall.Args) != 1 {
|
if len(panicCall.Args) != 1 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
useCall, ok := panicCall.Args[0].(*ast.CallExpr)
|
buildCall, ok := panicCall.Args[0].(*ast.CallExpr)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
useObj := qualifiedIdentObject(info, useCall.Fun)
|
buildObj := qualifiedIdentObject(info, buildCall.Fun)
|
||||||
if !isGooseImport(useObj.Pkg().Path()) || useObj.Name() != "Use" {
|
if !isGooseImport(buildObj.Pkg().Path()) || buildObj.Name() != "Build" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return useCall
|
return buildCall
|
||||||
}
|
}
|
||||||
|
|
||||||
func isGooseImport(path string) bool {
|
func isGooseImport(path string) bool {
|
||||||
|
|||||||
@@ -21,5 +21,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectFooBar() FooBar {
|
func injectFooBar() FooBar {
|
||||||
panic(goose.Use(Set))
|
panic(goose.Build(Set))
|
||||||
}
|
}
|
||||||
|
|||||||
2
internal/goose/testdata/Cleanup/foo/goose.go
vendored
2
internal/goose/testdata/Cleanup/foo/goose.go
vendored
@@ -21,5 +21,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectBar() (*Bar, func()) {
|
func injectBar() (*Bar, func()) {
|
||||||
panic(goose.Use(provideFoo, provideBar))
|
panic(goose.Build(provideFoo, provideBar))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,5 +35,5 @@ func provideMessage() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func injectedMessage() string {
|
func injectedMessage() string {
|
||||||
panic(goose.Use(provideMessage))
|
panic(goose.Build(provideMessage))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,5 +21,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectedMessage() string {
|
func injectedMessage() string {
|
||||||
panic(goose.Use(myFakeSet))
|
panic(goose.Build(myFakeSet))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,5 +22,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectedMessage() string {
|
func injectedMessage() string {
|
||||||
panic(goose.Use(bar.Value))
|
panic(goose.Build(bar.Value))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,5 +24,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectedFile() *os.File {
|
func injectedFile() *os.File {
|
||||||
panic(goose.Use(bar.Value))
|
panic(goose.Build(bar.Value))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,5 +22,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectFooer() foo.Fooer {
|
func injectFooer() foo.Fooer {
|
||||||
panic(goose.Use(Set))
|
panic(goose.Build(Set))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,5 +21,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectFooBar(foo Foo) FooBar {
|
func injectFooBar(foo Foo) FooBar {
|
||||||
panic(goose.Use(Set))
|
panic(goose.Build(Set))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,5 +21,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectBar(foo Foo) Bar {
|
func injectBar(foo Foo) Bar {
|
||||||
panic(goose.Use(Set))
|
panic(goose.Build(Set))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,5 +21,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectFooer() Fooer {
|
func injectFooer() Fooer {
|
||||||
panic(goose.Use(Set))
|
panic(goose.Build(Set))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectFooBar() FooBar {
|
func injectFooBar() FooBar {
|
||||||
panic(goose.Use(
|
panic(goose.Build(
|
||||||
provideBar,
|
provideBar,
|
||||||
provideFooBar,
|
provideFooBar,
|
||||||
goose.Bind(Fooer(nil), (*Bar)(nil))))
|
goose.Bind(Fooer(nil), (*Bar)(nil))))
|
||||||
|
|||||||
@@ -23,5 +23,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func inject(context stdcontext.Context, err struct{}) (context, error) {
|
func inject(context stdcontext.Context, err struct{}) (context, error) {
|
||||||
panic(goose.Use(provide))
|
panic(goose.Build(provide))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ func Provide(context2 stdcontext.Context) (context, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func inject(context stdcontext.Context, err struct{}) (context, error) {
|
func inject(context stdcontext.Context, err struct{}) (context, error) {
|
||||||
panic(goose.Use(Provide))
|
panic(goose.Build(Provide))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context) Provide() {
|
func (context) Provide() {
|
||||||
|
|||||||
@@ -21,5 +21,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectedMessage() string {
|
func injectedMessage() string {
|
||||||
panic(goose.Use(provideMessage))
|
panic(goose.Build(provideMessage))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,5 +21,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectedMessage() string {
|
func injectedMessage() string {
|
||||||
panic(goose.Use(goose.Value("Hello, World!")))
|
panic(goose.Build(goose.Value("Hello, World!")))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,5 +21,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectFooer() Fooer {
|
func injectFooer() Fooer {
|
||||||
panic(goose.Use(provideBar))
|
panic(goose.Build(provideBar))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,5 +26,5 @@ import (
|
|||||||
// parameter names on the inject stub.
|
// parameter names on the inject stub.
|
||||||
|
|
||||||
func inject(stdcontext.Context, struct{}) (context, error) {
|
func inject(stdcontext.Context, struct{}) (context, error) {
|
||||||
panic(goose.Use(provide))
|
panic(goose.Build(provide))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,5 +21,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectBaz() (Baz, func(), error) {
|
func injectBaz() (Baz, func(), error) {
|
||||||
panic(goose.Use(provideFoo, provideBar, provideBaz))
|
panic(goose.Build(provideFoo, provideBar, provideBaz))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,5 +21,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectFooBar() FooBar {
|
func injectFooBar() FooBar {
|
||||||
panic(goose.Use(Set))
|
panic(goose.Build(Set))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,5 +21,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectFoo() (Foo, error) {
|
func injectFoo() (Foo, error) {
|
||||||
panic(goose.Use(Set))
|
panic(goose.Build(Set))
|
||||||
}
|
}
|
||||||
|
|||||||
2
internal/goose/testdata/Struct/foo/goose.go
vendored
2
internal/goose/testdata/Struct/foo/goose.go
vendored
@@ -21,5 +21,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectFooBar() FooBar {
|
func injectFooBar() FooBar {
|
||||||
panic(goose.Use(Set))
|
panic(goose.Build(Set))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,5 +21,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectFooBar() *FooBar {
|
func injectFooBar() *FooBar {
|
||||||
panic(goose.Use(Set))
|
panic(goose.Build(Set))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,5 +21,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectFooBar() FooBar {
|
func injectFooBar() FooBar {
|
||||||
panic(goose.Use(Set))
|
panic(goose.Build(Set))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,5 +22,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectedMessage() string {
|
func injectedMessage() string {
|
||||||
panic(goose.Use(bar.Value))
|
panic(goose.Build(bar.Value))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,5 +21,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectFooBar() FooBar {
|
func injectFooBar() FooBar {
|
||||||
panic(goose.Use(Set))
|
panic(goose.Build(Set))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,5 +21,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectedMessage() Foo {
|
func injectedMessage() Foo {
|
||||||
panic(goose.Use(goose.Value(Foo("Hello, World!"))))
|
panic(goose.Build(goose.Value(Foo("Hello, World!"))))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,5 +21,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectedMessage() string {
|
func injectedMessage() string {
|
||||||
panic(goose.Use(goose.Value(msg)))
|
panic(goose.Build(goose.Value(msg)))
|
||||||
}
|
}
|
||||||
|
|||||||
2
internal/goose/testdata/Vendor/foo/goose.go
vendored
2
internal/goose/testdata/Vendor/foo/goose.go
vendored
@@ -22,5 +22,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectedMessage() string {
|
func injectedMessage() string {
|
||||||
panic(goose.Use(bar.ProvideMessage))
|
panic(goose.Build(bar.ProvideMessage))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user