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
master
Keson 4 years ago committed by GitHub
parent 6dbd3eada9
commit f5f873c6bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -178,6 +178,13 @@ service A-api {
} }
` `
const apiHasNoRequest = `
service A-api {
@handler GreetHandler
post /greet/ping ()
}
`
func TestParser(t *testing.T) { func TestParser(t *testing.T) {
filename := "greet.api" filename := "greet.api"
err := ioutil.WriteFile(filename, []byte(testApiTemplate), os.ModePerm) err := ioutil.WriteFile(filename, []byte(testApiTemplate), os.ModePerm)
@ -311,6 +318,21 @@ func TestApiHasJwtAndMiddleware(t *testing.T) {
validate(t, filename) validate(t, filename)
} }
func TestApiHasNoRequestBody(t *testing.T) {
filename := "greet.api"
err := ioutil.WriteFile(filename, []byte(apiHasNoRequest), os.ModePerm)
assert.Nil(t, err)
defer os.Remove(filename)
parser, err := parser.NewParser(filename)
assert.Nil(t, err)
_, err = parser.Parse()
assert.Nil(t, err)
validate(t, filename)
}
func validate(t *testing.T, api string) { func validate(t *testing.T, api string) {
dir := "_go" dir := "_go"
err := DoGenProject(api, dir, true) err := DoGenProject(api, dir, true)

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

Loading…
Cancel
Save