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

Internal packages parsing support with a flag #509

Merged
merged 4 commits into from
May 29, 2020
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ OPTIONS:
--output value, -o value Output directory for all the generated files(swagger.json, swagger.yaml and doc.go) (default: "./docs")
--parseVendor Parse go files in 'vendor' folder, disabled by default
--parseDependency Parse go files in outside dependency folder, disabled by default
--parseInternal Parse go files in internal packages, disabled by default
```

## Supported Web Frameworks
Expand Down
6 changes: 6 additions & 0 deletions cmd/swag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
parseVendorFlag = "parseVendor"
parseDependencyFlag = "parseDependency"
markdownFilesFlag = "markdownFiles"
parseInternal = "parseInternal"
generatedTimeFlag = "generatedTime"
)

Expand Down Expand Up @@ -65,6 +66,10 @@ var initFlags = []cli.Flag{
Value: "",
Usage: "Parse folder containing markdown files to use as description, disabled by default",
},
&cli.BoolFlag{
Name: "parseInternal",
Usage: "Parse go files in internal packages, disabled by default",
},
&cli.BoolFlag{
Name: "generatedTime",
Usage: "Generate timestamp at the top of docs.go, true by default",
Expand All @@ -89,6 +94,7 @@ func initAction(c *cli.Context) error {
ParseVendor: c.Bool(parseVendorFlag),
ParseDependency: c.Bool(parseDependencyFlag),
MarkdownFilesDir: c.String(markdownFilesFlag),
ParseInternal: c.Bool(parseInternal),
GeneratedTime: c.Bool(generatedTimeFlag),
})
}
Expand Down
4 changes: 4 additions & 0 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type Config struct {
// ParseDependencies whether swag should be parse outside dependency folder
ParseDependency bool

// ParseInternal whether swag should parse internal packages
ParseInternal bool

// MarkdownFilesDir used to find markdownfiles, which can be used for tag descriptions
MarkdownFilesDir string

Expand All @@ -76,6 +79,7 @@ func (g *Gen) Build(config *Config) error {
p.PropNamingStrategy = config.PropNamingStrategy
p.ParseVendor = config.ParseVendor
p.ParseDependency = config.ParseDependency
p.ParseInternal = config.ParseInternal

if err := p.ParseAPI(config.SearchDir, config.MainAPIFile); err != nil {
return err
Expand Down
7 changes: 6 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ type Parser struct {
// ParseDependencies whether swag should be parse outside dependency folder
ParseDependency bool

// ParseInternal whether swag should parse internal packages
ParseInternal bool

// structStack stores full names of the structures that were already parsed or are being parsed now
structStack []string

Expand Down Expand Up @@ -135,6 +138,7 @@ func (parser *Parser) ParseAPI(searchDir string, mainAPIFile string) error {
}

var t depth.Tree
t.ResolveInternal = true

absMainAPIFilePath, err := filepath.Abs(filepath.Join(searchDir, mainAPIFile))
if err != nil {
Expand Down Expand Up @@ -1496,7 +1500,8 @@ func (parser *Parser) getAllGoFileInfo(searchDir string) error {
}

func (parser *Parser) getAllGoFileInfoFromDeps(pkg *depth.Pkg) error {
if pkg.Internal || !pkg.Resolved { // ignored internal and not resolved dependencies
ignoreInternal := pkg.Internal && !parser.ParseInternal
if ignoreInternal || !pkg.Resolved { // ignored internal and not resolved dependencies
return nil
}

Expand Down