182 lines
6.0 KiB
YAML
182 lines
6.0 KiB
YAML
layouts:
|
|
- path: handler.go
|
|
body: |-
|
|
{{$OutDirs := GetUniqueHandlerOutDir .Methods}}
|
|
package {{.PackageName}}
|
|
import (
|
|
"context"
|
|
|
|
"github.com/cloudwego/hertz/pkg/app"
|
|
"github.com/cloudwego/hertz/pkg/protocol/consts"
|
|
{{- range $k, $v := .Imports}}
|
|
{{$k}} "{{$v.Package}}"
|
|
{{- end}}
|
|
{{- range $_, $OutDir := $OutDirs}}
|
|
{{if eq $OutDir "" -}}
|
|
"{{$.ProjPackage}}/biz/service"
|
|
{{- else -}}
|
|
"{{$.ProjPackage}}/biz/service/{{$OutDir}}"
|
|
{{- end -}}
|
|
{{- end}}
|
|
"{{$.ProjPackage}}/biz/utils"
|
|
)
|
|
{{range $_, $MethodInfo := .Methods}}
|
|
{{$MethodInfo.Comment}}
|
|
func {{$MethodInfo.Name}}(ctx context.Context, c *app.RequestContext) {
|
|
var err error
|
|
{{if ne $MethodInfo.RequestTypeName "" -}}
|
|
var req {{$MethodInfo.RequestTypeName}}
|
|
err = c.BindAndValidate(&req)
|
|
if err != nil {
|
|
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
|
|
return
|
|
}
|
|
{{end}}
|
|
{{if eq $MethodInfo.OutputDir "" -}}
|
|
resp,err := service.New{{$MethodInfo.Name}}Service(ctx, c).Run(&req)
|
|
if err != nil {
|
|
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
|
|
return
|
|
}
|
|
{{else}}
|
|
resp,err := {{$MethodInfo.OutputDir}}.New{{$MethodInfo.Name}}Service(ctx, c).Run(&req)
|
|
if err != nil {
|
|
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
|
|
return
|
|
}
|
|
{{end}}
|
|
utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp)
|
|
}
|
|
{{end}}
|
|
update_behavior:
|
|
import_tpl:
|
|
- |-
|
|
{{$OutDirs := GetUniqueHandlerOutDir .Methods}}
|
|
{{- range $_, $OutDir := $OutDirs}}
|
|
{{if eq $OutDir "" -}}
|
|
"{{$.ProjPackage}}/biz/service"
|
|
{{- else -}}
|
|
"{{$.ProjPackage}}/biz/service/{{$OutDir}}"
|
|
{{end}}
|
|
{{- end}}
|
|
- path: handler_single.go
|
|
body: |+
|
|
{{.Comment}}
|
|
func {{.Name}}(ctx context.Context, c *app.RequestContext) {
|
|
var err error
|
|
{{if ne .RequestTypeName "" -}}
|
|
var req {{.RequestTypeName}}
|
|
err = c.BindAndValidate(&req)
|
|
if err != nil {
|
|
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
|
|
return
|
|
}
|
|
{{end}}
|
|
{{if eq .OutputDir "" -}}
|
|
resp,err := service.New{{.Name}}Service(ctx, c).Run(&req)
|
|
{{else}}
|
|
resp,err := {{.OutputDir}}.New{{.Name}}Service(ctx, c).Run(&req)
|
|
{{end}}
|
|
if err != nil {
|
|
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
|
|
return
|
|
}
|
|
utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp)
|
|
}
|
|
- path: "biz/service/{{.HandlerGenPath}}/{{ToSnakeCase .MethodName}}.go"
|
|
loop_method: true
|
|
update_behavior:
|
|
type: "skip"
|
|
body: |-
|
|
package {{.FilePackage}}
|
|
import (
|
|
"context"
|
|
|
|
"github.com/cloudwego/hertz/pkg/app"
|
|
{{- range $k, $v := .Models}}
|
|
{{$k}} "{{$v.Package}}"
|
|
{{- end}}
|
|
)
|
|
type {{.Name}}Service struct {
|
|
RequestContext *app.RequestContext
|
|
Context context.Context
|
|
}
|
|
|
|
func New{{.Name}}Service(Context context.Context, RequestContext *app.RequestContext) *{{.Name}}Service {
|
|
return &{{.Name}}Service{RequestContext: RequestContext, Context: Context}
|
|
}
|
|
|
|
func (h *{{.Name}}Service) Run(req *{{.RequestTypeName}}) ( resp *{{.ReturnTypeName}}, err error) {
|
|
//defer func() {
|
|
// hlog.CtxInfof(h.Context, "req = %+v", req)
|
|
// hlog.CtxInfof(h.Context, "resp = %+v", resp)
|
|
//}()
|
|
// todo edit your code
|
|
return
|
|
}
|
|
- path: "biz/service/{{.HandlerGenPath}}/{{ToSnakeCase .MethodName}}_test.go"
|
|
loop_method: true
|
|
update_behavior:
|
|
type: "skip"
|
|
body: |-
|
|
package {{.FilePackage}}
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/cloudwego/hertz/pkg/app"
|
|
"github.com/cloudwego/hertz/pkg/common/test/assert"
|
|
{{- range $k, $v := .Models}}
|
|
{{$k}} "{{$v.Package}}"
|
|
{{- end}}
|
|
)
|
|
func Test{{.Name}}Service_Run(t *testing.T) {
|
|
ctx := context.Background()
|
|
c := app.NewContext(1)
|
|
s := New{{.Name}}Service(ctx, c)
|
|
// init req and assert value
|
|
req := &{{.RequestTypeName}}{}
|
|
resp, err := s.Run(req)
|
|
assert.DeepEqual(t, nil, resp)
|
|
assert.DeepEqual(t, nil, err)
|
|
// todo edit your unit test.
|
|
}
|
|
- path: "{{.HandlerDir}}/{{.GenPackage}}/{{ToSnakeCase .ServiceName}}_test.go"
|
|
loop_service: true
|
|
update_behavior:
|
|
type: "append"
|
|
append_key: "method"
|
|
insert_key: "Test{{$.Name}}"
|
|
append_tpl: |-
|
|
func Test{{.Name}}(t *testing.T) {
|
|
h := server.Default()
|
|
h.GET("{{.Path}}", {{.Name}})
|
|
w := ut.PerformRequest(h.Engine, "{{.HTTPMethod}}", "{{.Path}}", &ut.Body{Body: bytes.NewBufferString(""), Len: 1},
|
|
ut.Header{})
|
|
resp := w.Result()
|
|
assert.DeepEqual(t, 201, resp.StatusCode())
|
|
assert.DeepEqual(t, "", string(resp.Body()))
|
|
// todo edit your unit test.
|
|
}
|
|
body: |-
|
|
package {{.FilePackage}}
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
|
|
"github.com/cloudwego/hertz/pkg/app/server"
|
|
"github.com/cloudwego/hertz/pkg/common/test/assert"
|
|
"github.com/cloudwego/hertz/pkg/common/ut"
|
|
)
|
|
{{range $_, $MethodInfo := $.Methods}}
|
|
func Test{{$MethodInfo.Name}}(t *testing.T) {
|
|
h := server.Default()
|
|
h.GET("{{$MethodInfo.Path}}", {{$MethodInfo.Name}})
|
|
w := ut.PerformRequest(h.Engine, "{{$MethodInfo.HTTPMethod}}", "{{$MethodInfo.Path}}", &ut.Body{Body: bytes.NewBufferString(""), Len: 1},
|
|
ut.Header{})
|
|
resp := w.Result()
|
|
assert.DeepEqual(t, 201, resp.StatusCode())
|
|
assert.DeepEqual(t, "", string(resp.Body()))
|
|
// todo edit your unit test.
|
|
}
|
|
{{end}} |