genlogic.go 3.5 KB

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