Skip to content

Commit

Permalink
Merge 55bba71 into 7534a13
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverRainZ authored Jul 19, 2023
2 parents 7534a13 + 55bba71 commit d9e53c6
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/swag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
templateDelimsFlag = "templateDelims"
packageName = "packageName"
collectionFormatFlag = "collectionFormat"
packagePrefixFlag = "packagePrefix"
)

var initFlags = []cli.Flag{
Expand Down Expand Up @@ -167,6 +168,11 @@ var initFlags = []cli.Flag{
Value: "csv",
Usage: "Set default collection format",
},
&cli.StringFlag{
Name: packagePrefixFlag,
Value: "",
Usage: "Parse only packages whose import path match the given prefix, comma separated",
},
}

func initAction(ctx *cli.Context) error {
Expand Down Expand Up @@ -235,6 +241,7 @@ func initAction(ctx *cli.Context) error {
PackageName: ctx.String(packageName),
Debugger: logger,
CollectionFormat: collectionFormat,
PackagePrefix: ctx.String(packagePrefixFlag),
})
}

Expand Down
4 changes: 4 additions & 0 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ type Config struct {

// CollectionFormat set default collection format
CollectionFormat string

// Parse only packages whose import path match the given prefix, comma separated
PackagePrefix string
}

// Build builds swagger json file for given searchDir and mainAPIFile. Returns json.
Expand Down Expand Up @@ -197,6 +200,7 @@ func (g *Gen) Build(config *Config) error {
swag.ParseUsingGoList(config.ParseGoList),
swag.SetTags(config.Tags),
swag.SetCollectionFormat(config.CollectionFormat),
swag.SetPackagePrefix(config.PackagePrefix),
)

p.PropNamingStrategy = config.PropNamingStrategy
Expand Down
4 changes: 4 additions & 0 deletions golist.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func (parser *Parser) getAllGoFileInfoFromDepsByList(pkg *build.Package, parseFl
return nil
}

if parser.skipPackageByPrefix(pkg.ImportPath) {
return nil // ignored by user-defined package path prefixes

Check warning on line 57 in golist.go

View check run for this annotation

Codecov / codecov/patch

golist.go#L57

Added line #L57 was not covered by tests
}

srcDir := pkg.Dir
var err error
for i := range pkg.GoFiles {
Expand Down
39 changes: 39 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ type Parser struct {
// excludes excludes dirs and files in SearchDir
excludes map[string]struct{}

// packagePrefix is a list of package path prefixes, packages that do not
// match any one of them will be excluded when searching.
packagePrefix []string

// tells parser to include only specific extension
parseExtension string

Expand Down Expand Up @@ -273,6 +277,20 @@ func SetExcludedDirsAndFiles(excludes string) func(*Parser) {
}
}

// SetPackagePrefix sets a list of package path prefixes from a comma-separated
// string, packages that do not match any one of them will be excluded when
// searching.
func SetPackagePrefix(packagePrefix string) func(*Parser) {
return func(p *Parser) {
for _, f := range strings.Split(packagePrefix, ",") {
f = strings.TrimSpace(f)
if f != "" {
p.packagePrefix = append(p.packagePrefix, f)
}
}
}
}

// SetTags sets the tags to be included
func SetTags(include string) func(*Parser) {
return func(p *Parser) {
Expand Down Expand Up @@ -343,6 +361,20 @@ func (parser *Parser) ParseAPI(searchDir string, mainAPIFile string, parseDepth
return parser.ParseAPIMultiSearchDir([]string{searchDir}, mainAPIFile, parseDepth)
}

// skipPackageByPrefix returns true the given pkgpath does not match
// any user-defined package path prefixes.
func (parser *Parser) skipPackageByPrefix(pkgpath string) bool {
if len(parser.packagePrefix) == 0 {
return false
}
for _, prefix := range parser.packagePrefix {
if strings.HasPrefix(pkgpath, prefix) {
return false
}
}
return true
}

// ParseAPIMultiSearchDir is like ParseAPI but for multiple search dirs.
func (parser *Parser) ParseAPIMultiSearchDir(searchDirs []string, mainAPIFile string, parseDepth int) error {
for _, searchDir := range searchDirs {
Expand Down Expand Up @@ -1623,6 +1655,9 @@ func defineTypeOfExample(schemaType, arrayType, exampleValue string) (interface{

// GetAllGoFileInfo gets all Go source files information for given searchDir.
func (parser *Parser) getAllGoFileInfo(packageDir, searchDir string) error {
if parser.skipPackageByPrefix(packageDir) {
return nil // ignored by user-defined package path prefixes

Check warning on line 1659 in parser.go

View check run for this annotation

Codecov / codecov/patch

parser.go#L1659

Added line #L1659 was not covered by tests
}
return filepath.Walk(searchDir, func(path string, f os.FileInfo, _ error) error {
err := parser.Skip(path, f)
if err != nil {
Expand All @@ -1648,6 +1683,10 @@ func (parser *Parser) getAllGoFileInfoFromDeps(pkg *depth.Pkg, parseFlag ParseFl
return nil
}

if pkg.Raw != nil && parser.skipPackageByPrefix(pkg.Raw.ImportPath) {
return nil // ignored by user-defined package path prefixes

Check warning on line 1687 in parser.go

View check run for this annotation

Codecov / codecov/patch

parser.go#L1687

Added line #L1687 was not covered by tests
}

// Skip cgo
if pkg.Raw == nil && pkg.Name == "C" {
return nil
Expand Down
22 changes: 22 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4048,3 +4048,25 @@ func TestParser_collectionFormat(t *testing.T) {
})
}
}

func TestParser_skipPackageByPrefix(t *testing.T) {
t.Parallel()

parser := New()

assert.False(t, parser.skipPackageByPrefix("github.com/swaggo/swag"))
assert.False(t, parser.skipPackageByPrefix("github.com/swaggo/swag/cmd"))
assert.False(t, parser.skipPackageByPrefix("github.com/swaggo/swag/gen"))

parser = New(SetPackagePrefix("github.com/swaggo/swag/cmd"))

assert.True(t, parser.skipPackageByPrefix("github.com/swaggo/swag"))
assert.False(t, parser.skipPackageByPrefix("github.com/swaggo/swag/cmd"))
assert.True(t, parser.skipPackageByPrefix("github.com/swaggo/swag/gen"))

parser = New(SetPackagePrefix("github.com/swaggo/swag/cmd,github.com/swaggo/swag/gen"))

assert.True(t, parser.skipPackageByPrefix("github.com/swaggo/swag"))
assert.False(t, parser.skipPackageByPrefix("github.com/swaggo/swag/cmd"))
assert.False(t, parser.skipPackageByPrefix("github.com/swaggo/swag/gen"))
}

0 comments on commit d9e53c6

Please sign in to comment.