Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --packagePrefix=P for only parse packages matched by prefix P #1582

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}

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
}
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
}

// 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"))
}