genlogic.go 3.4 KB

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