Skip to content

Commit

Permalink
feat: add --packagePrefix=P for only parse packages matched by prefix…
Browse files Browse the repository at this point in the history
… P (#1582)
  • Loading branch information
SilverRainZ authored Jul 19, 2023
1 parent 7534a13 commit d0f9dc5
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
@@ -39,6 +39,7 @@ const (
templateDelimsFlag = "templateDelims"
packageName = "packageName"
collectionFormatFlag = "collectionFormat"
packagePrefixFlag = "packagePrefix"
)

var initFlags = []cli.Flag{
@@ -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 {
@@ -235,6 +241,7 @@ func initAction(ctx *cli.Context) error {
PackageName: ctx.String(packageName),
Debugger: logger,
CollectionFormat: collectionFormat,
PackagePrefix: ctx.String(packagePrefixFlag),
})
}

4 changes: 4 additions & 0 deletions gen/gen.go
Original file line number Diff line number Diff line change
@@ -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.
@@ -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
4 changes: 4 additions & 0 deletions golist.go
Original file line number Diff line number Diff line change
@@ -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
}

srcDir := pkg.Dir
var err error
for i := range pkg.GoFiles {
39 changes: 39 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
@@ -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

@@ -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) {
@@ -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 {
@@ -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
}
return filepath.Walk(searchDir, func(path string, f os.FileInfo, _ error) error {
err := parser.Skip(path, f)
if err != nil {
@@ -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
}

// Skip cgo
if pkg.Raw == nil && pkg.Name == "C" {
return nil
22 changes: 22 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
@@ -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 d0f9dc5

Please sign in to comment.