Skip to content

Commit

Permalink
chore: remove duplicate walk func (#1070)
Browse files Browse the repository at this point in the history
  • Loading branch information
ubogdan authored Dec 1, 2021
1 parent 47d0cd2 commit 75e9d01
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 54 deletions.
26 changes: 4 additions & 22 deletions formater.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Formater struct {
debug Debugger

// excludes excludes dirs and files in SearchDir
excludes map[string]bool
excludes map[string]struct{}

mainFile string
}
Expand All @@ -34,7 +34,7 @@ type Formater struct {
func NewFormater() *Formater {
formater := &Formater{
debug: log.New(os.Stdout, "", log.LstdFlags),
excludes: make(map[string]bool),
excludes: make(map[string]struct{}),
}
return formater
}
Expand All @@ -51,7 +51,7 @@ func (f *Formater) FormatAPI(searchDir, excludeDir, mainFile string) error {
fi = strings.TrimSpace(fi)
if fi != "" {
fi = filepath.Clean(fi)
f.excludes[fi] = true
f.excludes[fi] = struct{}{}
}
}

Expand Down Expand Up @@ -87,7 +87,7 @@ func (f *Formater) formatMultiSearchDir(searchDirs []string) error {
}

func (f *Formater) visit(path string, fileInfo os.FileInfo, err error) error {
if err := f.skip(path, fileInfo); err != nil {
if err := walkWith(f.excludes, false)(path, fileInfo); err != nil {
return err
} else if fileInfo.IsDir() {
// skip if file is folder
Expand All @@ -110,24 +110,6 @@ func (f *Formater) visit(path string, fileInfo os.FileInfo, err error) error {
return nil
}

// skip skip folder in ('vendor' 'docs' 'excludes' 'hidden folder')
func (f *Formater) skip(path string, fileInfo os.FileInfo) error {
if fileInfo.IsDir() {
if fileInfo.Name() == "vendor" || // ignore "vendor"
fileInfo.Name() == "docs" || // exclude docs
len(fileInfo.Name()) > 1 && fileInfo.Name()[0] == '.' { // exclude all hidden folder
return filepath.SkipDir
}

if f.excludes != nil {
if _, ok := f.excludes[path]; ok {
return filepath.SkipDir
}
}
}
return nil
}

// FormatMain format the main.go comment
func (f *Formater) FormatMain(mainFilepath string) error {
fileSet := token.NewFileSet()
Expand Down
21 changes: 2 additions & 19 deletions formater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package swag
import (
"bytes"
"fmt"
"github.com/agiledragon/gomonkey/v2"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/agiledragon/gomonkey/v2"

"github.com/otiai10/copy"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -132,24 +133,6 @@ func Test_writeFormatedComments(t *testing.T) {
})
}

func TestFormater_skip(t *testing.T) {
formater := NewFormater()

err := formater.skip("./testdata", &mockFS{FileName: "vendor", IsDirectory: true})
assert.ErrorIs(t, err, filepath.SkipDir)
err = formater.skip("/testdata", &mockFS{FileName: "docs", IsDirectory: true})
assert.ErrorIs(t, err, filepath.SkipDir)
err = formater.skip("/testdata", &mockFS{FileName: ".hidden", IsDirectory: true})
assert.ErrorIs(t, err, filepath.SkipDir)

formater.excludes["/testdata/excludes"] = true
err = formater.skip("/testdata/excludes", &mockFS{FileName: ".hidden", IsDirectory: true})
assert.ErrorIs(t, err, filepath.SkipDir)

err = formater.skip("/testdata", &mockFS{FileName: ".hidden", IsDirectory: false})
assert.NoError(t, err)
}

func TestFormater_visit(t *testing.T) {
formater := NewFormater()

Expand Down
32 changes: 19 additions & 13 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ type Parser struct {
collectionFormatInQuery string

// excludes excludes dirs and files in SearchDir
excludes map[string]bool
excludes map[string]struct{}

// debugging output goes here
debug Debugger
Expand Down Expand Up @@ -173,7 +173,7 @@ func New(options ...func(*Parser)) *Parser {
outputSchemas: make(map[*TypeSpecDef]*Schema),
existSchemaNames: make(map[string]*Schema),
toBeRenamedSchemas: make(map[string]string),
excludes: make(map[string]bool),
excludes: make(map[string]struct{}),
fieldParserFactory: newTagBaseFieldParser,
Overrides: make(map[string]string),
}
Expand Down Expand Up @@ -206,7 +206,7 @@ func SetExcludedDirsAndFiles(excludes string) func(*Parser) {
f = strings.TrimSpace(f)
if f != "" {
f = filepath.Clean(f)
p.excludes[f] = true
p.excludes[f] = struct{}{}
}
}
}
Expand Down Expand Up @@ -1371,21 +1371,27 @@ func (parser *Parser) checkOperationIDUniqueness() error {

// Skip returns filepath.SkipDir error if match vendor and hidden folder.
func (parser *Parser) Skip(path string, f os.FileInfo) error {
if f.IsDir() {
if !parser.ParseVendor && f.Name() == "vendor" || // ignore "vendor"
f.Name() == "docs" || // exclude docs
len(f.Name()) > 1 && f.Name()[0] == '.' { // exclude all hidden folder
return filepath.SkipDir
}
return walkWith(parser.excludes, parser.ParseVendor)(path, f)
}

if parser.excludes != nil {
if _, ok := parser.excludes[path]; ok {
func walkWith(excludes map[string]struct{}, parseVendor bool) func(path string, fileInfo os.FileInfo) error {
return func(path string, f os.FileInfo) error {
if f.IsDir() {
if !parseVendor && f.Name() == "vendor" || // ignore "vendor"
f.Name() == "docs" || // exclude docs
len(f.Name()) > 1 && f.Name()[0] == '.' { // exclude all hidden folder
return filepath.SkipDir
}

if excludes != nil {
if _, ok := excludes[path]; ok {
return filepath.SkipDir
}
}
}
}

return nil
return nil
}
}

// GetSwagger returns *spec.Swagger which is the root document object for the API specification.
Expand Down

0 comments on commit 75e9d01

Please sign in to comment.