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}} - Request: {{.requestType}}
- Response: {{.responseType}} - Response: {{.responseType}}
2.
{{.requestContent}}
2. 3.
{{.responseContent}} {{.responseContent}}
` `
@ -46,7 +47,12 @@ func genDoc(api *spec.ApiSpec, dir string, filename string) error {
routeComment = "N/A" 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 { if err != nil {
return err return err
} }
@ -60,6 +66,7 @@ func genDoc(api *spec.ApiSpec, dir string, filename string) error {
"uri": route.Path, "uri": route.Path,
"requestType": "`" + stringx.TakeOne(route.RequestTypeName(), "-") + "`", "requestType": "`" + stringx.TakeOne(route.RequestTypeName(), "-") + "`",
"responseType": "`" + stringx.TakeOne(route.ResponseTypeName(), "-") + "`", "responseType": "`" + stringx.TakeOne(route.ResponseTypeName(), "-") + "`",
"requestContent": requestContent,
"responseContent": responseContent, "responseContent": responseContent,
}) })
if err != nil { if err != nil {
@ -72,14 +79,14 @@ func genDoc(api *spec.ApiSpec, dir string, filename string) error {
return err return err
} }
func responseBody(api *spec.ApiSpec, route spec.Route) (string, error) { func buildDoc(route spec.Type) (string, error) {
if len(route.ResponseTypeName()) == 0 { if route == nil || len(route.Name()) == 0 {
return "", nil return "", nil
} }
var tps = make([]spec.Type, 0) var tps = make([]spec.Type, 0)
tps = append(tps, route.ResponseType) tps = append(tps, route)
if definedType, ok := route.ResponseType.(spec.DefineStruct); ok { if definedType, ok := route.(spec.DefineStruct); ok {
associatedTypes(definedType, &tps) associatedTypes(definedType, &tps)
} }
value, err := gogen.BuildTypes(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) { func associatedTypes(tp spec.DefineStruct, tps *[]spec.Type) {
var hasAdded = false
for _, item := range *tps {
if item.Name() == tp.Name() {
hasAdded = true
break
}
}
if !hasAdded {
*tps = append(*tps, tp) *tps = append(*tps, tp)
}
for _, item := range tp.Members { for _, item := range tp.Members {
if definedType, ok := item.Type.(spec.DefineStruct); ok { if definedType, ok := item.Type.(spec.DefineStruct); ok {
associatedTypes(definedType, tps) associatedTypes(definedType, tps)

@ -8,43 +8,50 @@ import (
"strings" "strings"
"github.com/tal-tech/go-zero/tools/goctl/api/parser" "github.com/tal-tech/go-zero/tools/goctl/api/parser"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
var docDir = "doc"
func DocCommand(c *cli.Context) error { func DocCommand(c *cli.Context) error {
dir := c.String("dir") dir := c.String("dir")
if len(dir) == 0 { if len(dir) == 0 {
return errors.New("missing -dir") 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 { if err != nil {
return fmt.Errorf("dir %s not exist", dir) return err
}
} }
err = os.RemoveAll(dir + "/" + docDir + "/") if !util.FileExists(dir) {
return errors.New(fmt.Sprintf("dir %s not exsit", dir))
}
dir, err := filepath.Abs(dir)
if err != nil { if err != nil {
return err return err
} }
for _, f := range files {
api, err := parser.Parse(f) files, err := filePathWalkDir(dir)
if err != nil { if err != nil {
return fmt.Errorf("parse file: %s, err: %s", f, err.Error()) return err
} }
index := strings.Index(f, dir) for _, path := range files {
if index < 0 { api, err := parser.Parse(path)
continue if err != nil {
return fmt.Errorf("parse file: %s, err: %s", path, err.Error())
} }
dst := dir + "/" + docDir + f[index+len(dir):]
index = strings.LastIndex(dst, "/") err = genDoc(api, filepath.Dir(filepath.Join(outputDir, path[len(dir):])),
if index < 0 { strings.Replace(path[len(filepath.Dir(path)):], ".api", ".md", 1))
continue if err != nil {
return err
} }
dir := dst[:index]
genDoc(api, dir, strings.Replace(dst[index+1:], ".api", ".md", 1))
} }
return nil return nil
} }

@ -89,6 +89,11 @@ var (
Name: "dir", Name: "dir",
Usage: "the target dir", Usage: "the target dir",
}, },
cli.StringFlag{
Name: "o",
Required: false,
Usage: "the output markdown directory",
},
}, },
Action: docgen.DocCommand, Action: docgen.DocCommand,
}, },

Loading…
Cancel
Save