api handler generate incompletely while has no request (#158)

* fix: api handler generate incompletely while has no request

* fix: api handler generate incompletely while has no request

* add handler generate test
This commit is contained in:
Keson
2020-10-23 16:10:33 +08:00
committed by GitHub
parent 6dbd3eada9
commit f5f873c6bd
2 changed files with 27 additions and 3 deletions

View File

@@ -23,14 +23,14 @@ import (
func {{.HandlerName}}(ctx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.{{.RequestType}}
{{if .HasRequest}}var req types.{{.RequestType}}
if err := httpx.Parse(r, &req); err != nil {
httpx.Error(w, err)
return
}
}{{end}}
l := logic.New{{.LogicType}}(r.Context(), ctx)
{{if .HasResp}}resp, {{end}}err := l.{{.Call}}(req)
{{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}req{{end}})
if err != nil {
httpx.Error(w, err)
} else {
@@ -47,6 +47,7 @@ type Handler struct {
LogicType string
Call string
HasResp bool
HasRequest bool
}
func genHandler(dir string, group spec.Group, route spec.Route) error {
@@ -71,6 +72,7 @@ func genHandler(dir string, group spec.Group, route spec.Route) error {
LogicType: strings.TrimSuffix(strings.Title(handler), "Handler") + "Logic",
Call: strings.Title(strings.TrimSuffix(handler, "Handler")),
HasResp: len(route.ResponseType.Name) > 0,
HasRequest: len(route.RequestType.Name) > 0,
})
}