Skip to content

Commit

Permalink
Do not parse operations in dependency (#1432)
Browse files Browse the repository at this point in the history
* Do not parse operations in dependency
  • Loading branch information
sdghchj committed Dec 29, 2022
1 parent c8fad2c commit f916213
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 80 deletions.
4 changes: 2 additions & 2 deletions golist.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ func (parser *Parser) getAllGoFileInfoFromDepsByList(pkg *build.Package) error {
srcDir := pkg.Dir
var err error
for i := range pkg.GoFiles {
err = parser.parseFile(pkg.ImportPath, filepath.Join(srcDir, pkg.GoFiles[i]), nil)
err = parser.parseFile(pkg.ImportPath, filepath.Join(srcDir, pkg.GoFiles[i]), nil, ParseModels)
if err != nil {
return err
}
}

// parse .go source files that import "C"
for i := range pkg.CgoFiles {
err = parser.parseFile(pkg.ImportPath, filepath.Join(srcDir, pkg.CgoFiles[i]), nil)
err = parser.parseFile(pkg.ImportPath, filepath.Join(srcDir, pkg.CgoFiles[i]), nil, ParseModels)
if err != nil {
return err
}
Expand Down
11 changes: 6 additions & 5 deletions packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ func NewPackagesDefinitions() *PackagesDefinitions {
}

// ParseFile parse a source file.
func (pkgDefs *PackagesDefinitions) ParseFile(packageDir, path string, src interface{}) error {
func (pkgDefs *PackagesDefinitions) ParseFile(packageDir, path string, src interface{}, flag ParseFlag) error {
// positions are relative to FileSet
fileSet := token.NewFileSet()
astFile, err := goparser.ParseFile(fileSet, path, src, goparser.ParseComments)
if err != nil {
return fmt.Errorf("failed to parse file %s, error:%+v", path, err)
}
return pkgDefs.collectAstFile(fileSet, packageDir, path, astFile)
return pkgDefs.collectAstFile(fileSet, packageDir, path, astFile, flag)
}

// collectAstFile collect ast.file.
func (pkgDefs *PackagesDefinitions) collectAstFile(fileSet *token.FileSet, packageDir, path string, astFile *ast.File) error {
func (pkgDefs *PackagesDefinitions) collectAstFile(fileSet *token.FileSet, packageDir, path string, astFile *ast.File, flag ParseFlag) error {
if pkgDefs.files == nil {
pkgDefs.files = make(map[*ast.File]*AstFileInfo)
}
Expand Down Expand Up @@ -81,13 +81,14 @@ func (pkgDefs *PackagesDefinitions) collectAstFile(fileSet *token.FileSet, packa
File: astFile,
Path: path,
PackagePath: packageDir,
ParseFlag: flag,
}

return nil
}

// RangeFiles for range the collection of ast.File in alphabetic order.
func (pkgDefs *PackagesDefinitions) RangeFiles(handle func(filename string, file *ast.File) error) error {
func (pkgDefs *PackagesDefinitions) RangeFiles(handle func(info *AstFileInfo) error) error {
sortedFiles := make([]*AstFileInfo, 0, len(pkgDefs.files))
for _, info := range pkgDefs.files {
// ignore package path prefix with 'vendor' or $GOROOT,
Expand All @@ -103,7 +104,7 @@ func (pkgDefs *PackagesDefinitions) RangeFiles(handle func(filename string, file
})

for _, info := range sortedFiles {
err := handle(info.Path, info.File)
err := handle(info)
if err != nil {
return err
}
Expand Down
21 changes: 11 additions & 10 deletions packages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ import (
func TestPackagesDefinitions_ParseFile(t *testing.T) {
pd := PackagesDefinitions{}
packageDir := "github.com/swaggo/swag/testdata/simple"
assert.NoError(t, pd.ParseFile(packageDir, "testdata/simple/main.go", nil))
assert.NoError(t, pd.ParseFile(packageDir, "testdata/simple/main.go", nil, ParseAll))
assert.Equal(t, 1, len(pd.packages))
assert.Equal(t, 1, len(pd.files))
}

func TestPackagesDefinitions_collectAstFile(t *testing.T) {
pd := PackagesDefinitions{}
fileSet := token.NewFileSet()
assert.NoError(t, pd.collectAstFile(fileSet, "", "", nil))
assert.NoError(t, pd.collectAstFile(fileSet, "", "", nil, ParseAll))

firstFile := &ast.File{
Name: &ast.Ident{Name: "main.go"},
}

packageDir := "github.com/swaggo/swag/testdata/simple"
assert.NoError(t, pd.collectAstFile(fileSet, packageDir, "testdata/simple/"+firstFile.Name.String(), firstFile))
assert.NoError(t, pd.collectAstFile(fileSet, packageDir, "testdata/simple/"+firstFile.Name.String(), firstFile, ParseAll))
assert.NotEmpty(t, pd.packages[packageDir])

absPath, _ := filepath.Abs("testdata/simple/" + firstFile.Name.String())
Expand All @@ -37,18 +37,19 @@ func TestPackagesDefinitions_collectAstFile(t *testing.T) {
File: firstFile,
Path: absPath,
PackagePath: packageDir,
ParseFlag: ParseAll,
}
assert.Equal(t, pd.files[firstFile], astFileInfo)

// Override
assert.NoError(t, pd.collectAstFile(fileSet, packageDir, "testdata/simple/"+firstFile.Name.String(), firstFile))
assert.NoError(t, pd.collectAstFile(fileSet, packageDir, "testdata/simple/"+firstFile.Name.String(), firstFile, ParseAll))
assert.Equal(t, pd.files[firstFile], astFileInfo)

// Another file
secondFile := &ast.File{
Name: &ast.Ident{Name: "api.go"},
}
assert.NoError(t, pd.collectAstFile(fileSet, packageDir, "testdata/simple/"+secondFile.Name.String(), secondFile))
assert.NoError(t, pd.collectAstFile(fileSet, packageDir, "testdata/simple/"+secondFile.Name.String(), secondFile, ParseAll))
}

func TestPackagesDefinitions_rangeFiles(t *testing.T) {
Expand All @@ -72,8 +73,8 @@ func TestPackagesDefinitions_rangeFiles(t *testing.T) {
}

i, expect := 0, []string{"testdata/simple/api/api.go", "testdata/simple/main.go"}
_ = pd.RangeFiles(func(filename string, file *ast.File) error {
assert.Equal(t, expect[i], filename)
_ = pd.RangeFiles(func(fileInfo *AstFileInfo) error {
assert.Equal(t, expect[i], fileInfo.Path)
i++
return nil
})
Expand Down Expand Up @@ -225,14 +226,14 @@ func TestPackage_rangeFiles(t *testing.T) {
}

var sorted []string
processor := func(filename string, file *ast.File) error {
sorted = append(sorted, filename)
processor := func(fileInfo *AstFileInfo) error {
sorted = append(sorted, fileInfo.Path)
return nil
}
assert.NoError(t, pd.RangeFiles(processor))
assert.Equal(t, []string{"testdata/simple/api/api.go", "testdata/simple/main.go"}, sorted)

assert.Error(t, pd.RangeFiles(func(filename string, file *ast.File) error {
assert.Error(t, pd.RangeFiles(func(fileInfo *AstFileInfo) error {
return ErrFuncTypeField
}))

Expand Down
33 changes: 25 additions & 8 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ const (
scopeAttrPrefix = "@scope."
)

// ParseFlag determine what to parse
type ParseFlag int

const (
// ParseNone parse nothing
ParseNone ParseFlag = 0x00
// ParseOperations parse operations
ParseOperations = 0x01
// ParseModels parse models
ParseModels = 0x02
// ParseAll parse operations and models
ParseAll = ParseOperations | ParseModels
)

var (
// ErrRecursiveParseStruct recursively parsing struct.
ErrRecursiveParseStruct = errors.New("recursively parsing struct")
Expand Down Expand Up @@ -866,18 +880,21 @@ func matchExtension(extensionToMatch string, comments []*ast.Comment) (match boo
}

// ParseRouterAPIInfo parses router api info for given astFile.
func (parser *Parser) ParseRouterAPIInfo(fileName string, astFile *ast.File) error {
for _, astDescription := range astFile.Decls {
func (parser *Parser) ParseRouterAPIInfo(fileInfo *AstFileInfo) error {
for _, astDescription := range fileInfo.File.Decls {
if (fileInfo.ParseFlag & ParseOperations) == ParseNone {
continue
}
astDeclaration, ok := astDescription.(*ast.FuncDecl)
if ok && astDeclaration.Doc != nil && astDeclaration.Doc.List != nil {
if parser.matchTags(astDeclaration.Doc.List) &&
matchExtension(parser.parseExtension, astDeclaration.Doc.List) {
// for per 'function' comment, create a new 'Operation' object
operation := NewOperation(parser, SetCodeExampleFilesDirectory(parser.codeExampleFilesDir))
for _, comment := range astDeclaration.Doc.List {
err := operation.ParseComment(comment.Text, astFile)
err := operation.ParseComment(comment.Text, fileInfo.File)
if err != nil {
return fmt.Errorf("ParseComment error in file %s :%+v", fileName, err)
return fmt.Errorf("ParseComment error in file %s :%+v", fileInfo.Path, err)
}
}
err := processRouterOperation(parser, operation)
Expand Down Expand Up @@ -1518,7 +1535,7 @@ func (parser *Parser) getAllGoFileInfo(packageDir, searchDir string) error {
return err
}

return parser.parseFile(filepath.ToSlash(filepath.Dir(filepath.Clean(filepath.Join(packageDir, relPath)))), path, nil)
return parser.parseFile(filepath.ToSlash(filepath.Dir(filepath.Clean(filepath.Join(packageDir, relPath)))), path, nil, ParseAll)
})
}

Expand Down Expand Up @@ -1546,7 +1563,7 @@ func (parser *Parser) getAllGoFileInfoFromDeps(pkg *depth.Pkg) error {
}

path := filepath.Join(srcDir, f.Name())
if err := parser.parseFile(pkg.Name, path, nil); err != nil {
if err := parser.parseFile(pkg.Name, path, nil, ParseModels); err != nil {
return err
}
}
Expand All @@ -1560,12 +1577,12 @@ func (parser *Parser) getAllGoFileInfoFromDeps(pkg *depth.Pkg) error {
return nil
}

func (parser *Parser) parseFile(packageDir, path string, src interface{}) error {
func (parser *Parser) parseFile(packageDir, path string, src interface{}, flag ParseFlag) error {
if strings.HasSuffix(strings.ToLower(path), "_test.go") || filepath.Ext(path) != ".go" {
return nil
}

return parser.packages.ParseFile(packageDir, path, src)
return parser.packages.ParseFile(packageDir, path, src, flag)
}

func (parser *Parser) checkOperationIDUniqueness() error {
Expand Down
Loading

0 comments on commit f916213

Please sign in to comment.