Code optimized (#474)

* optimized markdown generator

* optimized markdown generator

* optimized markdown generator

* optimized markdown generator
master
kingxt 4 years ago committed by GitHub
parent 8f1c88e07d
commit f14ab70035
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -24,9 +24,10 @@ const (
- Request: {{.requestType}}
- Response: {{.responseType}}
2.
{{.requestContent}}
2.
3.
{{.responseContent}}
`
@ -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
}
@ -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 {
@ -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)
@ -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)

@ -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
}

@ -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,
},

Loading…
Cancel
Save