Skip to content

Commit

Permalink
Code optimized (zeromicro#474)
Browse files Browse the repository at this point in the history
* optimized markdown generator

* optimized markdown generator

* optimized markdown generator

* optimized markdown generator
  • Loading branch information
kingxt authored Feb 18, 2021
1 parent 8f1c88e commit f14ab70
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 26 deletions.
33 changes: 25 additions & 8 deletions tools/goctl/api/docgen/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ const (
- Request: {{.requestType}}
- Response: {{.responseType}}
2. 请求定义
{{.requestContent}}
2. 类型定义
3. 返回定义
{{.responseContent}}
`
Expand All @@ -46,7 +47,12 @@ func genDoc(api *spec.ApiSpec, dir string, filename string) error {
routeComment = "N/A"
}

responseContent, err := responseBody(api, route)
requestContent, err := buildDoc(route.RequestType)
if err != nil {
return err
}

responseContent, err := buildDoc(route.ResponseType)
if err != nil {
return err
}
Expand All @@ -60,6 +66,7 @@ func genDoc(api *spec.ApiSpec, dir string, filename string) error {
"uri": route.Path,
"requestType": "`" + stringx.TakeOne(route.RequestTypeName(), "-") + "`",
"responseType": "`" + stringx.TakeOne(route.ResponseTypeName(), "-") + "`",
"requestContent": requestContent,
"responseContent": responseContent,
})
if err != nil {
Expand All @@ -72,14 +79,14 @@ func genDoc(api *spec.ApiSpec, dir string, filename string) error {
return err
}

func responseBody(api *spec.ApiSpec, route spec.Route) (string, error) {
if len(route.ResponseTypeName()) == 0 {
func buildDoc(route spec.Type) (string, error) {
if route == nil || len(route.Name()) == 0 {
return "", nil
}

var tps = make([]spec.Type, 0)
tps = append(tps, route.ResponseType)
if definedType, ok := route.ResponseType.(spec.DefineStruct); ok {
tps = append(tps, route)
if definedType, ok := route.(spec.DefineStruct); ok {
associatedTypes(definedType, &tps)
}
value, err := gogen.BuildTypes(tps)
Expand All @@ -91,7 +98,17 @@ func responseBody(api *spec.ApiSpec, route spec.Route) (string, error) {
}

func associatedTypes(tp spec.DefineStruct, tps *[]spec.Type) {
*tps = append(*tps, tp)
var hasAdded = false
for _, item := range *tps {
if item.Name() == tp.Name() {
hasAdded = true
break
}
}
if !hasAdded {
*tps = append(*tps, tp)
}

for _, item := range tp.Members {
if definedType, ok := item.Type.(spec.DefineStruct); ok {
associatedTypes(definedType, tps)
Expand Down
43 changes: 25 additions & 18 deletions tools/goctl/api/docgen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,50 @@ import (
"strings"

"github.com/tal-tech/go-zero/tools/goctl/api/parser"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/urfave/cli"
)

var docDir = "doc"

func DocCommand(c *cli.Context) error {
dir := c.String("dir")
if len(dir) == 0 {
return errors.New("missing -dir")
}

files, err := filePathWalkDir(dir)
outputDir := c.String("o")
if len(outputDir) == 0 {
var err error
outputDir, err = os.Getwd()
if err != nil {
return err
}
}

if !util.FileExists(dir) {
return errors.New(fmt.Sprintf("dir %s not exsit", dir))
}

dir, err := filepath.Abs(dir)
if err != nil {
return fmt.Errorf("dir %s not exist", dir)
return err
}

err = os.RemoveAll(dir + "/" + docDir + "/")
files, err := filePathWalkDir(dir)
if err != nil {
return err
}
for _, f := range files {
api, err := parser.Parse(f)

for _, path := range files {
api, err := parser.Parse(path)
if err != nil {
return fmt.Errorf("parse file: %s, err: %s", f, err.Error())
return fmt.Errorf("parse file: %s, err: %s", path, err.Error())
}

index := strings.Index(f, dir)
if index < 0 {
continue
}
dst := dir + "/" + docDir + f[index+len(dir):]
index = strings.LastIndex(dst, "/")
if index < 0 {
continue
err = genDoc(api, filepath.Dir(filepath.Join(outputDir, path[len(dir):])),
strings.Replace(path[len(filepath.Dir(path)):], ".api", ".md", 1))
if err != nil {
return err
}
dir := dst[:index]
genDoc(api, dir, strings.Replace(dst[index+1:], ".api", ".md", 1))
}
return nil
}
Expand Down
5 changes: 5 additions & 0 deletions tools/goctl/goctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ var (
Name: "dir",
Usage: "the target dir",
},
cli.StringFlag{
Name: "o",
Required: false,
Usage: "the output markdown directory",
},
},
Action: docgen.DocCommand,
},
Expand Down

0 comments on commit f14ab70

Please sign in to comment.