Template customization

Overview

goctl code generation is go based on template to implement data drive. While currently goctl code generation meets some of the code successes, the template custom can enrich code generation.

Template instructions can be consulted goctl template

Sample

Scenes

Implementing unified body response, following:

  1. {
  2. "code": 0,
  3. "msg": "OK",
  4. "data": {} // ①
  5. }

① Actual Response Data

Template customization - 图1tip

go-zerogenerated code not handled

Preparation

我们提前在 modulegreet 的工程下的 response 包中写一个 Response 方法,目录树类似如下:

  1. greet
  2. ├── response
  3. └── response.go
  4. └── xxx...

Code:

  1. package response
  2. import (
  3. "net/http"
  4. "github.com/zeromicro/go-zero/rest/httpx"
  5. )
  6. type Body struct {
  7. Code int `json:"code"`
  8. Msg string `json:"msg"`
  9. Data interface{} `json:"data,omitempty"`
  10. }
  11. func Response(w http.ResponseWriter, resp interface{}, err error) {
  12. var body Body
  13. if err != nil {
  14. body.Code = -1
  15. body.Msg = err.Error()
  16. } else {
  17. body.Msg = "OK"
  18. body.Data = resp
  19. }
  20. httpx.OkJson(w, body)
  21. }

Edit handler template

  1. $ vim ~/.goctl/${goctl_version}/api/handler.tpl

Replace the template with the following

  1. package handler
  2. import (
  3. "net/http"
  4. "greet/response"// ①
  5. {{.ImportPackages}}
  6. )
  7. func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
  8. return func(w http.ResponseWriter, r *http.Request) {
  9. {{if .HasRequest}}var req types.{{.RequestType}}
  10. if err := httpx.Parse(r, &req); err != nil {
  11. httpx.Error(w, err)
  12. return
  13. }{{end}}
  14. l := logic.New{{.LogicType}}(r.Context(), svcCtx)
  15. {{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}&req{{end}})
  16. {{if .HasResp}}response.Response(w, resp, err){{else}}response.Response(w, nil, err){{end}}//②
  17. }
  18. }

① Replace with your realresponsepackage name, information

② Custom Template Content

::tip 1. If you don’t have~/.goctl/${goctl版本号}/api/handler.tplfile you can initialize the template initialization commandgoctl template format ininitialize : :::

Compare the template before and after

  • Modify before
  1. func GreetHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
  2. return func(w http.ResponseWriter, r *http.Request) {
  3. var req types.Request
  4. if err := httpx.Parse(r, &req); err != nil {
  5. httpx.Error(w, err)
  6. return
  7. }
  8. l := logic.NewGreetLogic(r.Context(), svcCtx)
  9. resp, err := l.Greet(&req)
  10. // 以下内容将被自定义模板替换
  11. if err != nil {
  12. httpx.Error(w, err)
  13. } else {
  14. httpx.OkJson(w, resp)
  15. }
  16. }
  17. }
  • Modified
  1. func GreetHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
  2. return func(w http.ResponseWriter, r *http.Request) {
  3. var req types.Request
  4. if err := httpx.Parse(r, &req); err != nil {
  5. httpx.Error(w, err)
  6. return
  7. }
  8. l := logic.NewGreetLogic(r.Context(), svcCtx)
  9. resp, err := l.Greet(&req)
  10. response.Response(w, resp, err)
  11. }
  12. }

Modify response body comparison before and after template

  • Modify before
  1. {
  2. "message": "Hello go-zero!"
  3. }
  • Modified
  1. {
  2. "code": 0,
  3. "msg": "OK",
  4. "data": {
  5. "message": "Hello go-zero!"
  6. }
  7. }

Template Custom Rules

  1. Modify within valid data range provided by goctl, i.e. external variables are not supported
  2. Adding template file is not supported
  3. Variable changes not supported

References