|
@@ -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) {
|
|
|
|
- if len(route.ResponseTypeName()) == 0 {
|
|
|
|
|
|
+func buildDoc(route spec.Type) (string, error) {
|
|
|
|
+ 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)
|
|
|
|
- if definedType, ok := route.ResponseType.(spec.DefineStruct); ok {
|
|
|
|
|
|
+ tps = append(tps, route)
|
|
|
|
+ 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) {
|
|
- *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 {
|
|
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)
|