genlogic.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package gogen
  2. import (
  3. "bytes"
  4. "fmt"
  5. "path"
  6. "strings"
  7. "text/template"
  8. "github.com/tal-tech/go-zero/tools/goctl/api/spec"
  9. "github.com/tal-tech/go-zero/tools/goctl/api/util"
  10. "github.com/tal-tech/go-zero/tools/goctl/templatex"
  11. ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
  12. "github.com/tal-tech/go-zero/tools/goctl/vars"
  13. )
  14. const logicTemplate = `package logic
  15. import (
  16. {{.imports}}
  17. )
  18. type {{.logic}} struct {
  19. logx.Logger
  20. ctx context.Context
  21. svcCtx *svc.ServiceContext
  22. }
  23. func New{{.logic}}(ctx context.Context, svcCtx *svc.ServiceContext) {{.logic}} {
  24. return {{.logic}}{
  25. Logger: logx.WithContext(ctx),
  26. ctx: ctx,
  27. svcCtx: svcCtx,
  28. }
  29. }
  30. func (l *{{.logic}}) {{.function}}({{.request}}) {{.responseType}} {
  31. // todo: add your logic here and delete this line
  32. {{.returnString}}
  33. }
  34. `
  35. func genLogic(dir string, api *spec.ApiSpec) error {
  36. for _, g := range api.Service.Groups {
  37. for _, r := range g.Routes {
  38. err := genLogicByRoute(dir, g, r)
  39. if err != nil {
  40. return err
  41. }
  42. }
  43. }
  44. return nil
  45. }
  46. func genLogicByRoute(dir string, group spec.Group, route spec.Route) error {
  47. handler, ok := util.GetAnnotationValue(route.Annotations, "server", "handler")
  48. if !ok {
  49. return fmt.Errorf("missing handler annotation for %q", route.Path)
  50. }
  51. handler = strings.TrimSuffix(handler, "handler")
  52. handler = strings.TrimSuffix(handler, "Handler")
  53. filename := strings.ToLower(handler)
  54. goFile := filename + "logic.go"
  55. fp, created, err := util.MaybeCreateFile(dir, getLogicFolderPath(group, route), goFile)
  56. if err != nil {
  57. return err
  58. }
  59. if !created {
  60. return nil
  61. }
  62. defer fp.Close()
  63. parentPkg, err := getParentPackage(dir)
  64. if err != nil {
  65. return err
  66. }
  67. imports := genLogicImports(route, parentPkg)
  68. var responseString string
  69. var returnString string
  70. var requestString string
  71. if len(route.ResponseType.Name) > 0 {
  72. resp := strings.Title(route.ResponseType.Name)
  73. responseString = "(*types." + resp + ", error)"
  74. returnString = fmt.Sprintf("return &types.%s{}, nil", resp)
  75. } else {
  76. responseString = "error"
  77. returnString = "return nil"
  78. }
  79. if len(route.RequestType.Name) > 0 {
  80. requestString = "req " + "types." + strings.Title(route.RequestType.Name)
  81. }
  82. text, err := templatex.LoadTemplate(category, logicTemplateFile, logicTemplate)
  83. if err != nil {
  84. return err
  85. }
  86. t := template.Must(template.New("logicTemplate").Parse(text))
  87. buffer := new(bytes.Buffer)
  88. err = t.Execute(fp, map[string]string{
  89. "imports": imports,
  90. "logic": strings.Title(handler) + "Logic",
  91. "function": strings.Title(strings.TrimSuffix(handler, "Handler")),
  92. "responseType": responseString,
  93. "returnString": returnString,
  94. "request": requestString,
  95. })
  96. if err != nil {
  97. return err
  98. }
  99. formatCode := formatCode(buffer.String())
  100. _, err = fp.WriteString(formatCode)
  101. return err
  102. }
  103. func getLogicFolderPath(group spec.Group, route spec.Route) string {
  104. folder, ok := util.GetAnnotationValue(route.Annotations, "server", groupProperty)
  105. if !ok {
  106. folder, ok = util.GetAnnotationValue(group.Annotations, "server", groupProperty)
  107. if !ok {
  108. return logicDir
  109. }
  110. }
  111. folder = strings.TrimPrefix(folder, "/")
  112. folder = strings.TrimSuffix(folder, "/")
  113. return path.Join(logicDir, folder)
  114. }
  115. func genLogicImports(route spec.Route, parentPkg string) string {
  116. var imports []string
  117. imports = append(imports, `"context"`+"\n")
  118. imports = append(imports, fmt.Sprintf("\"%s\"", ctlutil.JoinPackages(parentPkg, contextDir)))
  119. if len(route.ResponseType.Name) > 0 || len(route.RequestType.Name) > 0 {
  120. imports = append(imports, fmt.Sprintf("\"%s\"\n", ctlutil.JoinPackages(parentPkg, typesDir)))
  121. }
  122. imports = append(imports, fmt.Sprintf("\"%s/core/logx\"", vars.ProjectOpenSourceUrl))
  123. return strings.Join(imports, "\n\t")
  124. }