genlogic.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/config"
  11. ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
  12. "github.com/tal-tech/go-zero/tools/goctl/util/format"
  13. "github.com/tal-tech/go-zero/tools/goctl/vars"
  14. )
  15. const logicTemplate = `package logic
  16. import (
  17. {{.imports}}
  18. )
  19. type {{.logic}} struct {
  20. logx.Logger
  21. ctx context.Context
  22. svcCtx *svc.ServiceContext
  23. }
  24. func New{{.logic}}(ctx context.Context, svcCtx *svc.ServiceContext) {{.logic}} {
  25. return {{.logic}}{
  26. Logger: logx.WithContext(ctx),
  27. ctx: ctx,
  28. svcCtx: svcCtx,
  29. }
  30. }
  31. func (l *{{.logic}}) {{.function}}({{.request}}) {{.responseType}} {
  32. // todo: add your logic here and delete this line
  33. {{.returnString}}
  34. }
  35. `
  36. func genLogic(dir string, cfg *config.Config, api *spec.ApiSpec) error {
  37. for _, g := range api.Service.Groups {
  38. for _, r := range g.Routes {
  39. err := genLogicByRoute(dir, cfg, g, r)
  40. if err != nil {
  41. return err
  42. }
  43. }
  44. }
  45. return nil
  46. }
  47. func genLogicByRoute(dir string, cfg *config.Config, group spec.Group, route spec.Route) error {
  48. logic := getLogicName(route)
  49. goFile, err := format.FileNamingFormat(cfg.NamingFormat, logic)
  50. if err != nil {
  51. return err
  52. }
  53. goFile = goFile + ".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. text, err := ctlutil.LoadTemplate(category, logicTemplateFile, logicTemplate)
  82. if err != nil {
  83. return err
  84. }
  85. t := template.Must(template.New("logicTemplate").Parse(text))
  86. buffer := new(bytes.Buffer)
  87. err = t.Execute(fp, map[string]string{
  88. "imports": imports,
  89. "logic": strings.Title(logic),
  90. "function": strings.Title(strings.TrimSuffix(logic, "Logic")),
  91. "responseType": responseString,
  92. "returnString": returnString,
  93. "request": requestString,
  94. })
  95. if err != nil {
  96. return err
  97. }
  98. formatCode := formatCode(buffer.String())
  99. _, err = fp.WriteString(formatCode)
  100. return err
  101. }
  102. func getLogicFolderPath(group spec.Group, route spec.Route) string {
  103. folder, ok := util.GetAnnotationValue(route.Annotations, "server", groupProperty)
  104. if !ok {
  105. folder, ok = util.GetAnnotationValue(group.Annotations, "server", groupProperty)
  106. if !ok {
  107. return logicDir
  108. }
  109. }
  110. folder = strings.TrimPrefix(folder, "/")
  111. folder = strings.TrimSuffix(folder, "/")
  112. return path.Join(logicDir, folder)
  113. }
  114. func genLogicImports(route spec.Route, parentPkg string) string {
  115. var imports []string
  116. imports = append(imports, `"context"`+"\n")
  117. imports = append(imports, fmt.Sprintf("\"%s\"", ctlutil.JoinPackages(parentPkg, contextDir)))
  118. if len(route.ResponseType.Name) > 0 || len(route.RequestType.Name) > 0 {
  119. imports = append(imports, fmt.Sprintf("\"%s\"\n", ctlutil.JoinPackages(parentPkg, typesDir)))
  120. }
  121. imports = append(imports, fmt.Sprintf("\"%s/core/logx\"", vars.ProjectOpenSourceUrl))
  122. return strings.Join(imports, "\n\t")
  123. }