goose: use same directive parsing code during inject

Reviewed-by: Tuo Shan <shantuo@google.com>
This commit is contained in:
Ross Light
2018-04-09 08:23:10 -07:00
parent 479a501c08
commit 89371e16bd

View File

@@ -46,25 +46,28 @@ func Generate(bctx *build.Context, wd string, pkg string) ([]byte, error) {
g := newGen(prog, pkgInfo.Pkg.Path()) g := newGen(prog, pkgInfo.Pkg.Path())
r := newImportResolver(conf, prog.Fset) r := newImportResolver(conf, prog.Fset)
mc := newProviderSetCache(prog, r) mc := newProviderSetCache(prog, r)
var directives []directive
for _, f := range pkgInfo.Files { for _, f := range pkgInfo.Files {
if !isInjectFile(f) { if !isInjectFile(f) {
continue continue
} }
// TODO(light): use same directive extraction logic as provider set finding.
fileScope := pkgInfo.Scopes[f] fileScope := pkgInfo.Scopes[f]
cmap := ast.NewCommentMap(prog.Fset, f, f.Comments) groups := parseFile(prog.Fset, f)
for _, decl := range f.Decls { for _, decl := range f.Decls {
fn, ok := decl.(*ast.FuncDecl) fn, ok := decl.(*ast.FuncDecl)
if !ok { if !ok {
continue continue
} }
directives = directives[:0] var dg directiveGroup
for _, c := range cmap[fn] { for _, dg = range groups {
directives = extractDirectives(directives, c) if dg.decl == decl {
break
} }
sets := make([]providerSetRef, 0, len(directives)) }
for _, d := range directives { if dg.decl != decl {
dg = directiveGroup{}
}
var sets []providerSetRef
for _, d := range dg.dirs {
if d.kind != "use" { if d.kind != "use" {
return nil, fmt.Errorf("%v: cannot use %s directive on inject function", prog.Fset.Position(d.pos), d.kind) return nil, fmt.Errorf("%v: cannot use %s directive on inject function", prog.Fset.Position(d.pos), d.kind)
} }