@@ -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
277295func 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.
347379func (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.
16251657func (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
0 commit comments