Skip to content

Commit

Permalink
feat: add parse depth flag (#766)
Browse files Browse the repository at this point in the history
* parse depth flag

* fix imports

* move to if

Co-authored-by: g.kirilenko <[email protected]>
  • Loading branch information
necryin and g.kirilenko authored Aug 6, 2020
1 parent e769bbe commit b712c54
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 29 deletions.
15 changes: 11 additions & 4 deletions cmd/swag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ const (
parseVendorFlag = "parseVendor"
parseDependencyFlag = "parseDependency"
markdownFilesFlag = "markdownFiles"
parseInternal = "parseInternal"
parseInternalFlag = "parseInternal"
generatedTimeFlag = "generatedTime"
parseDepthFlag = "parseDepth"
)

var initFlags = []cli.Flag{
Expand Down Expand Up @@ -67,13 +68,18 @@ var initFlags = []cli.Flag{
Usage: "Parse folder containing markdown files to use as description, disabled by default",
},
&cli.BoolFlag{
Name: "parseInternal",
Name: parseInternalFlag,
Usage: "Parse go files in internal packages, disabled by default",
},
&cli.BoolFlag{
Name: "generatedTime",
Name: generatedTimeFlag,
Usage: "Generate timestamp at the top of docs.go, true by default",
},
&cli.IntFlag{
Name: parseDepthFlag,
Value: 100,
Usage: "Dependency parse depth",
},
}

func initAction(c *cli.Context) error {
Expand All @@ -94,8 +100,9 @@ func initAction(c *cli.Context) error {
ParseVendor: c.Bool(parseVendorFlag),
ParseDependency: c.Bool(parseDependencyFlag),
MarkdownFilesDir: c.String(markdownFilesFlag),
ParseInternal: c.Bool(parseInternal),
ParseInternal: c.Bool(parseInternalFlag),
GeneratedTime: c.Bool(generatedTimeFlag),
ParseDepth: c.Int(parseDepthFlag),
})
}

Expand Down
5 changes: 4 additions & 1 deletion gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ type Config struct {

// GeneratedTime whether swag should generate the timestamp at the top of docs.go
GeneratedTime bool

// ParseDepth dependency parse depth
ParseDepth int
}

// Build builds swagger json file for given searchDir and mainAPIFile. Returns json
Expand All @@ -81,7 +84,7 @@ func (g *Gen) Build(config *Config) error {
p.ParseDependency = config.ParseDependency
p.ParseInternal = config.ParseInternal

if err := p.ParseAPI(config.SearchDir, config.MainAPIFile); err != nil {
if err := p.ParseAPI(config.SearchDir, config.MainAPIFile, config.ParseDepth); err != nil {
return err
}
swagger := p.GetSwagger()
Expand Down
9 changes: 5 additions & 4 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func SetExcludedDirsAndFiles(excludes string) func(*Parser) {
}

// ParseAPI parses general api info for given searchDir and mainAPIFile
func (parser *Parser) ParseAPI(searchDir string, mainAPIFile string) error {
func (parser *Parser) ParseAPI(searchDir, mainAPIFile string, parseDepth int) error {
Printf("Generate general API Info, search dir:%s", searchDir)

packageDir, err := getPkgName(searchDir)
Expand All @@ -157,15 +157,16 @@ func (parser *Parser) ParseAPI(searchDir string, mainAPIFile string) error {
return err
}

var t depth.Tree
t.ResolveInternal = true

absMainAPIFilePath, err := filepath.Abs(filepath.Join(searchDir, mainAPIFile))
if err != nil {
return err
}

if parser.ParseDependency {
var t depth.Tree
t.ResolveInternal = true
t.MaxDepth = parseDepth

pkgName, err := getPkgName(filepath.Dir(absMainAPIFilePath))
if err != nil {
return err
Expand Down
42 changes: 22 additions & 20 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/stretchr/testify/assert"
)

const defaultParseDepth = 100

func TestNew(t *testing.T) {
swagMode = test
New()
Expand Down Expand Up @@ -282,7 +284,7 @@ func TestParseSimpleApi1(t *testing.T) {
mainAPIFile := "main.go"
p := New()
p.PropNamingStrategy = PascalCase
err = p.ParseAPI(searchDir, mainAPIFile)
err = p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.NoError(t, err)

b, _ := json.MarshalIndent(p.swagger, "", " ")
Expand Down Expand Up @@ -765,7 +767,7 @@ func TestParseSimpleApi_ForSnakecase(t *testing.T) {
mainAPIFile := "main.go"
p := New()
p.PropNamingStrategy = SnakeCase
err := p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.NoError(t, err)

b, _ := json.MarshalIndent(p.swagger, "", " ")
Expand Down Expand Up @@ -1220,7 +1222,7 @@ func TestParseSimpleApi_ForLowerCamelcase(t *testing.T) {
searchDir := "testdata/simple3"
mainAPIFile := "main.go"
p := New()
err := p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.NoError(t, err)

b, _ := json.MarshalIndent(p.swagger, "", " ")
Expand Down Expand Up @@ -1310,7 +1312,7 @@ func TestParseStructComment(t *testing.T) {
searchDir := "testdata/struct_comment"
mainAPIFile := "main.go"
p := New()
err := p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.NoError(t, err)
b, _ := json.MarshalIndent(p.swagger, "", " ")
assert.Equal(t, expected, string(b))
Expand Down Expand Up @@ -1385,7 +1387,7 @@ func TestParseNonExportedJSONFields(t *testing.T) {
searchDir := "testdata/non_exported_json_fields"
mainAPIFile := "main.go"
p := New()
err := p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.NoError(t, err)
b, _ := json.MarshalIndent(p.swagger, "", " ")
assert.Equal(t, expected, string(b))
Expand Down Expand Up @@ -1418,7 +1420,7 @@ func TestParsePetApi(t *testing.T) {
searchDir := "testdata/pet"
mainAPIFile := "main.go"
p := New()
err := p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.NoError(t, err)
b, _ := json.MarshalIndent(p.swagger, "", " ")
assert.Equal(t, expected, string(b))
Expand Down Expand Up @@ -1487,7 +1489,7 @@ func TestParseModelAsTypeAlias(t *testing.T) {
searchDir := "testdata/alias_type"
mainAPIFile := "main.go"
p := New()
err := p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.NoError(t, err)

b, _ := json.MarshalIndent(p.swagger, "", " ")
Expand All @@ -1498,7 +1500,7 @@ func TestParseComposition(t *testing.T) {
searchDir := "testdata/composition"
mainAPIFile := "main.go"
p := New()
err := p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.NoError(t, err)

expected, err := ioutil.ReadFile(filepath.Join(searchDir, "expected.json"))
Expand All @@ -1514,7 +1516,7 @@ func TestParseImportAliases(t *testing.T) {
searchDir := "testdata/alias_import"
mainAPIFile := "main.go"
p := New()
err := p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.NoError(t, err)

expected, err := ioutil.ReadFile(filepath.Join(searchDir, "expected.json"))
Expand All @@ -1530,7 +1532,7 @@ func TestParseNested(t *testing.T) {
mainAPIFile := "main.go"
p := New()
p.ParseDependency = true
err := p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.NoError(t, err)

expected, err := ioutil.ReadFile(filepath.Join(searchDir, "expected.json"))
Expand All @@ -1545,7 +1547,7 @@ func TestParseDuplicated(t *testing.T) {
mainAPIFile := "main.go"
p := New()
p.ParseDependency = true
err := p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.Errorf(t, err, "duplicated @id declarations successfully found")
}

Expand All @@ -1554,7 +1556,7 @@ func TestParseConflictSchemaName(t *testing.T) {
mainAPIFile := "main.go"
p := New()
p.ParseDependency = true
err := p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.NoError(t, err)
b, _ := json.MarshalIndent(p.swagger, "", " ")
expected, err := ioutil.ReadFile(filepath.Join(searchDir, "expected.json"))
Expand Down Expand Up @@ -2104,7 +2106,7 @@ func Test3(){
// for i := 0; i < 100; i++ {
// p := New()
// p.PropNamingStrategy = PascalCase
// err := p.ParseAPI(searchDir, mainAPIFile)
// err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
// b, _ := json.MarshalIndent(p.swagger, "", " ")
// assert.NotEqual(t, "", string(b))

Expand All @@ -2123,7 +2125,7 @@ func TestApiParseTag(t *testing.T) {
mainAPIFile := "main.go"
p := New(SetMarkdownFileDirectory(searchDir))
p.PropNamingStrategy = PascalCase
err := p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.NoError(t, err)

if len(p.swagger.Tags) != 3 {
Expand Down Expand Up @@ -2152,7 +2154,7 @@ func TestParseTagMarkdownDescription(t *testing.T) {
mainAPIFile := "main.go"
p := New(SetMarkdownFileDirectory(searchDir))
p.PropNamingStrategy = PascalCase
err := p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
if err != nil {
t.Error("Failed to parse api description: " + err.Error())
}
Expand All @@ -2172,7 +2174,7 @@ func TestParseApiMarkdownDescription(t *testing.T) {
mainAPIFile := "main.go"
p := New(SetMarkdownFileDirectory(searchDir))
p.PropNamingStrategy = PascalCase
err := p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
if err != nil {
t.Error("Failed to parse api description: " + err.Error())
}
Expand All @@ -2186,7 +2188,7 @@ func TestIgnoreInvalidPkg(t *testing.T) {
searchDir := "testdata/deps_having_invalid_pkg"
mainAPIFile := "main.go"
p := New()
if err := p.ParseAPI(searchDir, mainAPIFile); err != nil {
if err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth); err != nil {
t.Error("Failed to ignore valid pkg: " + err.Error())
}
}
Expand All @@ -2196,7 +2198,7 @@ func TestFixes432(t *testing.T) {
mainAPIFile := "cmd/main.go"

p := New()
if err := p.ParseAPI(searchDir, mainAPIFile); err != nil {
if err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth); err != nil {
t.Error("Failed to ignore valid pkg: " + err.Error())
}
}
Expand All @@ -2207,7 +2209,7 @@ func TestParseOutsideDependencies(t *testing.T) {

p := New()
p.ParseDependency = true
if err := p.ParseAPI(searchDir, mainAPIFile); err != nil {
if err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth); err != nil {
t.Error("Failed to parse api: " + err.Error())
}
}
Expand Down Expand Up @@ -2408,7 +2410,7 @@ func TestParseJSONFieldString(t *testing.T) {
searchDir := "testdata/json_field_string"
mainAPIFile := "main.go"
p := New()
err := p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.NoError(t, err)
b, _ := json.MarshalIndent(p.swagger, "", " ")
assert.Equal(t, expected, string(b))
Expand Down

0 comments on commit b712c54

Please sign in to comment.