From 75e9d0156a952d2f090bcdc0ca88f2e2a4925313 Mon Sep 17 00:00:00 2001 From: Bogdan U Date: Wed, 1 Dec 2021 13:19:40 +0200 Subject: [PATCH] chore: remove duplicate walk func (#1070) --- formater.go | 26 ++++---------------------- formater_test.go | 21 ++------------------- parser.go | 32 +++++++++++++++++++------------- 3 files changed, 25 insertions(+), 54 deletions(-) diff --git a/formater.go b/formater.go index d9af1dd24..e516fd996 100644 --- a/formater.go +++ b/formater.go @@ -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 } @@ -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 } @@ -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{}{} } } @@ -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 @@ -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() diff --git a/formater_test.go b/formater_test.go index 10468f89f..dc73f3fed 100644 --- a/formater_test.go +++ b/formater_test.go @@ -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" ) @@ -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() diff --git a/parser.go b/parser.go index 281a195bd..779f059d7 100644 --- a/parser.go +++ b/parser.go @@ -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 @@ -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), } @@ -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{}{} } } } @@ -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.