Skip to content

Commit

Permalink
Fix code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pytimer committed Mar 9, 2022
1 parent 8a5cb7b commit 7c5fafd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion gen/gen.go
Expand Up @@ -149,12 +149,12 @@ func (g *Gen) Build(config *Config) error {
swag.SetCodeExamplesDirectory(config.CodeExampleFilesDir),
swag.SetStrict(config.Strict),
swag.SetOverrides(overrides),
swag.ParseUsingGoList(config.ParseGoList),
)
p.PropNamingStrategy = config.PropNamingStrategy
p.ParseVendor = config.ParseVendor
p.ParseDependency = config.ParseDependency
p.ParseInternal = config.ParseInternal
p.ParseGoList = config.ParseGoList

if err := p.ParseAPIMultiSearchDir(searchDirs, config.MainAPIFile, config.ParseDepth); err != nil {
return err
Expand Down
21 changes: 12 additions & 9 deletions golist.go
Expand Up @@ -11,8 +11,7 @@ import (
)

func listPackages(ctx context.Context, dir string, env []string, args ...string) (pkgs []*build.Package, finalErr error) {
goArgs := append([]string{"list", "-json", "-e"}, args...)
cmd := exec.CommandContext(ctx, "go", goArgs...)
cmd := exec.CommandContext(ctx, "go", append([]string{"list", "-json", "-e"}, args...)...)
cmd.Env = env
cmd.Dir = dir

Expand All @@ -28,18 +27,21 @@ func listPackages(ctx context.Context, dir string, env []string, args ...string)
}
}()

if err := cmd.Start(); err != nil {
err = cmd.Start()
if err != nil {
return nil, err
}
dec := json.NewDecoder(stdout)
for dec.More() {
var pkg build.Package
if err := dec.Decode(&pkg); err != nil {
err = dec.Decode(&pkg)
if err != nil {
return nil, err
}
pkgs = append(pkgs, &pkg)
}
if err := cmd.Wait(); err != nil {
err = cmd.Wait()
if err != nil {
return nil, err
}
return pkgs, nil
Expand All @@ -57,16 +59,17 @@ func (parser *Parser) getAllGoFileInfoFromDepsByList(pkg *build.Package) error {
}

srcDir := pkg.Dir
var err error
for i := range pkg.GoFiles {
path := filepath.Join(srcDir, pkg.GoFiles[i])
if err := parser.parseFile(pkg.ImportPath, path, nil); err != nil {
err = parser.parseFile(pkg.ImportPath, filepath.Join(srcDir, pkg.GoFiles[i]), nil)
if err != nil {
return err
}
}

for i := range pkg.CFiles {
path := filepath.Join(srcDir, pkg.CFiles[i])
if err := parser.parseFile(pkg.ImportPath, path, nil); err != nil {
err = parser.parseFile(pkg.ImportPath, filepath.Join(srcDir, pkg.CFiles[i]), nil)
if err != nil {
return err
}
}
Expand Down
13 changes: 10 additions & 3 deletions parser.go
Expand Up @@ -142,8 +142,8 @@ type Parser struct {
// Overrides allows global replacements of types. A blank replacement will be skipped.
Overrides map[string]string

// ParseGoList whether swag use go list to parse dependency
ParseGoList bool
// parseGoList whether swag use go list to parse dependency
parseGoList bool
}

// FieldParserFactory create FieldParser
Expand Down Expand Up @@ -261,6 +261,13 @@ func SetOverrides(overrides map[string]string) func(parser *Parser) {
}
}

// ParseUsingGoList sets whether swag use go list to parse dependency
func ParseUsingGoList(enabled bool) func(parser *Parser) {
return func(p *Parser) {
p.parseGoList = enabled
}
}

// ParseAPI parses general api info for given searchDir and mainAPIFile.
func (parser *Parser) ParseAPI(searchDir string, mainAPIFile string, parseDepth int) error {
return parser.ParseAPIMultiSearchDir([]string{searchDir}, mainAPIFile, parseDepth)
Expand Down Expand Up @@ -289,7 +296,7 @@ func (parser *Parser) ParseAPIMultiSearchDir(searchDirs []string, mainAPIFile st

// Use 'go list' command instead of depth.Resolve()
if parser.ParseDependency {
if parser.ParseGoList {
if parser.parseGoList {
pkgs, err := listPackages(context.Background(), filepath.Dir(absMainAPIFilePath), nil, "-deps")
if err != nil {
return fmt.Errorf("pkg %s cannot find all dependencies, %s", filepath.Dir(absMainAPIFilePath), err)
Expand Down
2 changes: 1 addition & 1 deletion parser_test.go
Expand Up @@ -2132,7 +2132,7 @@ func TestParseGoList(t *testing.T) {
mainAPIFile := "main.go"
p := New()
p.ParseDependency = true
p.ParseGoList = true
p.parseGoList = true
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.NoError(t, err)
}
Expand Down

0 comments on commit 7c5fafd

Please sign in to comment.