Skip to content

Commit d0f9dc5

Browse files
authored
feat: add --packagePrefix=P for only parse packages matched by prefix P (#1582)
1 parent 7534a13 commit d0f9dc5

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

cmd/swag/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const (
3939
templateDelimsFlag = "templateDelims"
4040
packageName = "packageName"
4141
collectionFormatFlag = "collectionFormat"
42+
packagePrefixFlag = "packagePrefix"
4243
)
4344

4445
var initFlags = []cli.Flag{
@@ -167,6 +168,11 @@ var initFlags = []cli.Flag{
167168
Value: "csv",
168169
Usage: "Set default collection format",
169170
},
171+
&cli.StringFlag{
172+
Name: packagePrefixFlag,
173+
Value: "",
174+
Usage: "Parse only packages whose import path match the given prefix, comma separated",
175+
},
170176
}
171177

172178
func initAction(ctx *cli.Context) error {
@@ -235,6 +241,7 @@ func initAction(ctx *cli.Context) error {
235241
PackageName: ctx.String(packageName),
236242
Debugger: logger,
237243
CollectionFormat: collectionFormat,
244+
PackagePrefix: ctx.String(packagePrefixFlag),
238245
})
239246
}
240247

gen/gen.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ type Config struct {
138138

139139
// CollectionFormat set default collection format
140140
CollectionFormat string
141+
142+
// Parse only packages whose import path match the given prefix, comma separated
143+
PackagePrefix string
141144
}
142145

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

202206
p.PropNamingStrategy = config.PropNamingStrategy

golist.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ func (parser *Parser) getAllGoFileInfoFromDepsByList(pkg *build.Package, parseFl
5353
return nil
5454
}
5555

56+
if parser.skipPackageByPrefix(pkg.ImportPath) {
57+
return nil // ignored by user-defined package path prefixes
58+
}
59+
5660
srcDir := pkg.Dir
5761
var err error
5862
for i := range pkg.GoFiles {

parser.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ type Parser struct {
153153
// excludes excludes dirs and files in SearchDir
154154
excludes map[string]struct{}
155155

156+
// packagePrefix is a list of package path prefixes, packages that do not
157+
// match any one of them will be excluded when searching.
158+
packagePrefix []string
159+
156160
// tells parser to include only specific extension
157161
parseExtension string
158162

@@ -273,6 +277,20 @@ func SetExcludedDirsAndFiles(excludes string) func(*Parser) {
273277
}
274278
}
275279

280+
// SetPackagePrefix sets a list of package path prefixes from a comma-separated
281+
// string, packages that do not match any one of them will be excluded when
282+
// searching.
283+
func SetPackagePrefix(packagePrefix string) func(*Parser) {
284+
return func(p *Parser) {
285+
for _, f := range strings.Split(packagePrefix, ",") {
286+
f = strings.TrimSpace(f)
287+
if f != "" {
288+
p.packagePrefix = append(p.packagePrefix, f)
289+
}
290+
}
291+
}
292+
}
293+
276294
// SetTags sets the tags to be included
277295
func SetTags(include string) func(*Parser) {
278296
return func(p *Parser) {
@@ -343,6 +361,20 @@ func (parser *Parser) ParseAPI(searchDir string, mainAPIFile string, parseDepth
343361
return parser.ParseAPIMultiSearchDir([]string{searchDir}, mainAPIFile, parseDepth)
344362
}
345363

364+
// skipPackageByPrefix returns true the given pkgpath does not match
365+
// any user-defined package path prefixes.
366+
func (parser *Parser) skipPackageByPrefix(pkgpath string) bool {
367+
if len(parser.packagePrefix) == 0 {
368+
return false
369+
}
370+
for _, prefix := range parser.packagePrefix {
371+
if strings.HasPrefix(pkgpath, prefix) {
372+
return false
373+
}
374+
}
375+
return true
376+
}
377+
346378
// ParseAPIMultiSearchDir is like ParseAPI but for multiple search dirs.
347379
func (parser *Parser) ParseAPIMultiSearchDir(searchDirs []string, mainAPIFile string, parseDepth int) error {
348380
for _, searchDir := range searchDirs {
@@ -1623,6 +1655,9 @@ func defineTypeOfExample(schemaType, arrayType, exampleValue string) (interface{
16231655

16241656
// GetAllGoFileInfo gets all Go source files information for given searchDir.
16251657
func (parser *Parser) getAllGoFileInfo(packageDir, searchDir string) error {
1658+
if parser.skipPackageByPrefix(packageDir) {
1659+
return nil // ignored by user-defined package path prefixes
1660+
}
16261661
return filepath.Walk(searchDir, func(path string, f os.FileInfo, _ error) error {
16271662
err := parser.Skip(path, f)
16281663
if err != nil {
@@ -1648,6 +1683,10 @@ func (parser *Parser) getAllGoFileInfoFromDeps(pkg *depth.Pkg, parseFlag ParseFl
16481683
return nil
16491684
}
16501685

1686+
if pkg.Raw != nil && parser.skipPackageByPrefix(pkg.Raw.ImportPath) {
1687+
return nil // ignored by user-defined package path prefixes
1688+
}
1689+
16511690
// Skip cgo
16521691
if pkg.Raw == nil && pkg.Name == "C" {
16531692
return nil

parser_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4048,3 +4048,25 @@ func TestParser_collectionFormat(t *testing.T) {
40484048
})
40494049
}
40504050
}
4051+
4052+
func TestParser_skipPackageByPrefix(t *testing.T) {
4053+
t.Parallel()
4054+
4055+
parser := New()
4056+
4057+
assert.False(t, parser.skipPackageByPrefix("github.com/swaggo/swag"))
4058+
assert.False(t, parser.skipPackageByPrefix("github.com/swaggo/swag/cmd"))
4059+
assert.False(t, parser.skipPackageByPrefix("github.com/swaggo/swag/gen"))
4060+
4061+
parser = New(SetPackagePrefix("github.com/swaggo/swag/cmd"))
4062+
4063+
assert.True(t, parser.skipPackageByPrefix("github.com/swaggo/swag"))
4064+
assert.False(t, parser.skipPackageByPrefix("github.com/swaggo/swag/cmd"))
4065+
assert.True(t, parser.skipPackageByPrefix("github.com/swaggo/swag/gen"))
4066+
4067+
parser = New(SetPackagePrefix("github.com/swaggo/swag/cmd,github.com/swaggo/swag/gen"))
4068+
4069+
assert.True(t, parser.skipPackageByPrefix("github.com/swaggo/swag"))
4070+
assert.False(t, parser.skipPackageByPrefix("github.com/swaggo/swag/cmd"))
4071+
assert.False(t, parser.skipPackageByPrefix("github.com/swaggo/swag/gen"))
4072+
}

0 commit comments

Comments
 (0)