goose: use function name as implicit provider set
Single-element provider sets are frequently useful enough to warrant being a default. Larger groupings within a package are less frequent. Reviewed-by: Herbie Ong <herbie@google.com> Reviewed-by: Tuo Shan <shantuo@google.com>
This commit is contained in:
@@ -256,8 +256,6 @@ type providerSetImport struct {
|
||||
pos token.Pos
|
||||
}
|
||||
|
||||
const implicitModuleName = "Module"
|
||||
|
||||
// findProviderSets processes a package and extracts the provider sets declared in it.
|
||||
func findProviderSets(fset *token.FileSet, pkg *types.Package, typeInfo *types.Info, files []*ast.File) (map[string]*providerSet, error) {
|
||||
sets := make(map[string]*providerSet)
|
||||
@@ -274,14 +272,12 @@ func findProviderSets(fset *token.FileSet, pkg *types.Package, typeInfo *types.I
|
||||
if fileScope == nil {
|
||||
return nil, fmt.Errorf("%s: no scope found for file (likely a bug)", fset.File(f.Pos()).Name())
|
||||
}
|
||||
var name, spec string
|
||||
if strings.HasPrefix(d.line, `"`) {
|
||||
name, spec = implicitModuleName, d.line
|
||||
} else if i := strings.IndexByte(d.line, ' '); i != -1 {
|
||||
name, spec = d.line[:i], d.line[i+1:]
|
||||
} else {
|
||||
name, spec = implicitModuleName, d.line
|
||||
i := strings.IndexByte(d.line, ' ')
|
||||
// TODO(light): allow multiple imports in one line
|
||||
if i == -1 {
|
||||
return nil, fmt.Errorf("%s: invalid import: expected TARGET SETREF", fset.Position(d.pos))
|
||||
}
|
||||
name, spec := d.line[:i], d.line[i+1:]
|
||||
ref, err := parseProviderSetRef(spec, fileScope, pkg.Path(), d.pos)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v: %v", fset.Position(d.pos), err)
|
||||
@@ -337,9 +333,8 @@ func findProviderSets(fset *token.FileSet, pkg *types.Package, typeInfo *types.I
|
||||
if !isFunction {
|
||||
return nil, fmt.Errorf("%v: only functions can be marked as providers", fset.Position(d.pos))
|
||||
}
|
||||
if d.line == "" {
|
||||
providerSetName = implicitModuleName
|
||||
} else {
|
||||
providerSetName = fn.Name.Name
|
||||
if d.line != "" {
|
||||
// TODO(light): validate identifier
|
||||
providerSetName = d.line
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ func provideMessage() string { return "Hello, World!"; }
|
||||
|
||||
package main
|
||||
|
||||
//goose:use Module
|
||||
//goose:use provideMessage
|
||||
|
||||
func injectedMessage() string
|
||||
`,
|
||||
@@ -59,7 +59,7 @@ func injectedMessage() string
|
||||
import "fmt"
|
||||
func main() { fmt.Println(injectedMessage()); }
|
||||
|
||||
//goose:provide
|
||||
//goose:provide Set
|
||||
|
||||
// provideMessage provides a friendly user greeting.
|
||||
func provideMessage() string { return "Hello, World!"; }
|
||||
@@ -86,17 +86,17 @@ func main() {
|
||||
type Foo int
|
||||
type FooBar int
|
||||
|
||||
//goose:provide
|
||||
//goose:provide Set
|
||||
func provideFoo() Foo { return 41 }
|
||||
|
||||
//goose:provide
|
||||
//goose:provide Set
|
||||
func provideFooBar(foo Foo) FooBar { return FooBar(foo) + 1 }
|
||||
`,
|
||||
"foo/foo_goose.go": `//+build gooseinject
|
||||
|
||||
package main
|
||||
|
||||
//goose:use Module
|
||||
//goose:use Set
|
||||
|
||||
func injectFooBar() FooBar
|
||||
`,
|
||||
@@ -117,20 +117,20 @@ type Foo int
|
||||
type Bar int
|
||||
type FooBar int
|
||||
|
||||
//goose:provide
|
||||
//goose:provide Set
|
||||
func provideFoo() Foo { return 40 }
|
||||
|
||||
//goose:provide
|
||||
//goose:provide Set
|
||||
func provideBar() Bar { return 2 }
|
||||
|
||||
//goose:provide
|
||||
//goose:provide Set
|
||||
func provideFooBar(foo Foo, bar Bar) FooBar { return FooBar(foo) + FooBar(bar) }
|
||||
`,
|
||||
"foo/foo_goose.go": `//+build gooseinject
|
||||
|
||||
package main
|
||||
|
||||
//goose:use Module
|
||||
//goose:use Set
|
||||
|
||||
func injectFooBar() FooBar
|
||||
`,
|
||||
@@ -151,17 +151,17 @@ type Foo int
|
||||
type Bar int
|
||||
type FooBar int
|
||||
|
||||
//goose:provide
|
||||
//goose:provide Set
|
||||
func provideBar() Bar { return 2 }
|
||||
|
||||
//goose:provide
|
||||
//goose:provide Set
|
||||
func provideFooBar(foo Foo, bar Bar) FooBar { return FooBar(foo) + FooBar(bar) }
|
||||
`,
|
||||
"foo/foo_goose.go": `//+build gooseinject
|
||||
|
||||
package main
|
||||
|
||||
//goose:use Module
|
||||
//goose:use Set
|
||||
|
||||
func injectFooBar(foo Foo) FooBar
|
||||
`,
|
||||
@@ -181,17 +181,17 @@ func main() {
|
||||
type Foo int
|
||||
type Bar int
|
||||
|
||||
//goose:provide
|
||||
//goose:provide Set
|
||||
func provideFoo() Foo { return -888 }
|
||||
|
||||
//goose:provide
|
||||
//goose:provide Set
|
||||
func provideBar(foo Foo) Bar { return 2 }
|
||||
`,
|
||||
"foo/foo_goose.go": `//+build gooseinject
|
||||
|
||||
package main
|
||||
|
||||
//goose:use Module
|
||||
//goose:use Set
|
||||
|
||||
func injectBar(foo Foo) Bar
|
||||
`,
|
||||
@@ -218,14 +218,14 @@ func main() {
|
||||
|
||||
type Foo int
|
||||
|
||||
//goose:provide
|
||||
//goose:provide Set
|
||||
func provideFoo() (Foo, error) { return 42, errors.New("there is no Foo") }
|
||||
`,
|
||||
"foo/foo_goose.go": `//+build gooseinject
|
||||
|
||||
package main
|
||||
|
||||
//goose:use Module
|
||||
//goose:use Set
|
||||
|
||||
func injectFoo() (Foo, error)
|
||||
`,
|
||||
|
||||
Reference in New Issue
Block a user