Skip to content

Commit

Permalink
cmd/vfsgendev: Simplify lookupSource to lookupNameAndComment.
Browse files Browse the repository at this point in the history
This is cleaner and more readable code, better separated by the
responsibilities.

Rename imported source package to "sourcepkg". This makes the generated
script easier to read, and also safer (avoids any possible package name
conflicts).
  • Loading branch information
dmitshur committed May 20, 2017
1 parent a68da2d commit adf640b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
11 changes: 7 additions & 4 deletions cmd/vfsgendev/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
)

type data struct {
source
BuildTags string
ImportPath string
PackageName string
BuildTags string
VariableName string
VariableComment string
}

var generateTemplate = template.Must(template.New("").Funcs(template.FuncMap{
Expand All @@ -21,11 +24,11 @@ import (
"github.com/shurcooL/vfsgen"
{{.ImportPath | quote}}
sourcepkg {{.ImportPath | quote}}
)
func main() {
err := vfsgen.Generate({{.PackageName}}.{{.VariableName}}, vfsgen.Options{
err := vfsgen.Generate(sourcepkg.{{.VariableName}}, vfsgen.Options{
PackageName: {{.PackageName | quote}},
BuildTags: {{.BuildTags | quote}},
VariableName: {{.VariableName | quote}},
Expand Down
10 changes: 8 additions & 2 deletions cmd/vfsgendev/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,19 @@ func run() error {

bctx := build.Default
bctx.BuildTags = []string{tag}
source, err := lookupSource(bctx, importPath, variableName)
packageName, variableComment, err := lookupNameAndComment(bctx, importPath, variableName)
if err != nil {
return err
}

var buf bytes.Buffer
err = generateTemplate.Execute(&buf, data{source: source, BuildTags: "!" + tag})
err = generateTemplate.Execute(&buf, data{
ImportPath: importPath,
PackageName: packageName,
BuildTags: "!" + tag,
VariableName: variableName,
VariableComment: variableComment,
})
if err != nil {
return err
}
Expand Down
25 changes: 6 additions & 19 deletions cmd/vfsgendev/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,37 +55,24 @@ func stringValue(e ast.Expr) (string, error) {
return strconv.Unquote(lit.Value)
}

type source struct {
ImportPath string
PackageName string
VariableName string
VariableComment string
}

// lookupSource imports package using provided build context, and returns the
// import path, package name, variable name and variable comment.
func lookupSource(bctx build.Context, importPath, variableName string) (source, error) {
// lookupNameAndComment imports package using provided build context, and
// returns the package name and variable comment.
func lookupNameAndComment(bctx build.Context, importPath, variableName string) (packageName, variableComment string, err error) {
bpkg, err := bctx.Import(importPath, "", 0)
if err != nil {
return source{}, fmt.Errorf("can't import package %q: %v", importPath, err)
return "", "", fmt.Errorf("can't import package %q: %v", importPath, err)
}
dpkg, err := docPackage(bpkg)
if err != nil {
return source{}, fmt.Errorf("can't get godoc of package %q: %v", importPath, err)
return "", "", fmt.Errorf("can't get godoc of package %q: %v", importPath, err)
}
var variableComment string
for _, v := range dpkg.Vars {
if len(v.Names) == 1 && v.Names[0] == variableName {
variableComment = strings.TrimSuffix(v.Doc, "\n")
break
}
}
return source{
ImportPath: bpkg.ImportPath,
PackageName: bpkg.Name,
VariableName: variableName,
VariableComment: variableComment,
}, nil
return bpkg.Name, variableComment, nil
}

// TODO: Keep in sync or unify with github.com/shurcooL/cmd/gorepogen/docpackage.go.
Expand Down

0 comments on commit adf640b

Please sign in to comment.