genlogic.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package gogen
  2. import (
  3. "fmt"
  4. "path"
  5. "strings"
  6. "github.com/tal-tech/go-zero/tools/goctl/api/spec"
  7. "github.com/tal-tech/go-zero/tools/goctl/config"
  8. ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
  9. "github.com/tal-tech/go-zero/tools/goctl/util/format"
  10. "github.com/tal-tech/go-zero/tools/goctl/vars"
  11. )
  12. const logicTemplate = `package logic
  13. import (
  14. {{.imports}}
  15. )
  16. type {{.logic}} struct {
  17. logx.Logger
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. }
  21. func New{{.logic}}(ctx context.Context, svcCtx *svc.ServiceContext) {{.logic}} {
  22. return {{.logic}}{
  23. Logger: logx.WithContext(ctx),
  24. ctx: ctx,
  25. svcCtx: svcCtx,
  26. }
  27. }
  28. func (l *{{.logic}}) {{.function}}({{.request}}) {{.responseType}} {
  29. // todo: add your logic here and delete this line
  30. {{.returnString}}
  31. }
  32. `
  33. func genLogic(dir string, cfg *config.Config, api *spec.ApiSpec) error {
  34. for _, g := range api.Service.Groups {
  35. for _, r := range g.Routes {
  36. err := genLogicByRoute(dir, cfg, g, r)
  37. if err != nil {
  38. return err
  39. }
  40. }
  41. }
  42. return nil
  43. }
  44. func genLogicByRoute(dir string, cfg *config.Config, group spec.Group, route spec.Route) error {
  45. logic := getLogicName(route)
  46. goFile, err := format.FileNamingFormat(cfg.NamingFormat, logic)
  47. if err != nil {
  48. return err
  49. }
  50. parentPkg, err := getParentPackage(dir)
  51. if err != nil {
  52. return err
  53. }
  54. imports := genLogicImports(route, parentPkg)
  55. var responseString string
  56. var returnString string
  57. var requestString string
  58. if len(route.ResponseTypeName()) > 0 {
  59. resp := responseGoTypeName(route, typesPacket)
  60. responseString = "(" + resp + ", error)"
  61. if strings.HasPrefix(resp, "*") {
  62. returnString = fmt.Sprintf("return &%s{}, nil", strings.TrimPrefix(resp, "*"))
  63. } else {
  64. returnString = fmt.Sprintf("return %s{}, nil", resp)
  65. }
  66. } else {
  67. responseString = "error"
  68. returnString = "return nil"
  69. }
  70. if len(route.RequestTypeName()) > 0 {
  71. requestString = "req " + requestGoTypeName(route, typesPacket)
  72. }
  73. return genFile(fileGenConfig{
  74. dir: dir,
  75. subdir: getLogicFolderPath(group, route),
  76. filename: goFile + ".go",
  77. templateName: "logicTemplate",
  78. category: category,
  79. templateFile: logicTemplateFile,
  80. builtinTemplate: logicTemplate,
  81. data: map[string]string{
  82. "imports": imports,
  83. "logic": strings.Title(logic),
  84. "function": strings.Title(strings.TrimSuffix(logic, "Logic")),
  85. "responseType": responseString,
  86. "returnString": returnString,
  87. "request": requestString,
  88. },
  89. })
  90. }
  91. func getLogicFolderPath(group spec.Group, route spec.Route) string {
  92. folder := route.GetAnnotation(groupProperty)
  93. if len(folder) == 0 {
  94. folder = group.GetAnnotation(groupProperty)
  95. if len(folder) == 0 {
  96. return logicDir
  97. }
  98. }
  99. folder = strings.TrimPrefix(folder, "/")
  100. folder = strings.TrimSuffix(folder, "/")
  101. return path.Join(logicDir, folder)
  102. }
  103. func genLogicImports(route spec.Route, parentPkg string) string {
  104. var imports []string
  105. imports = append(imports, `"context"`+"\n")
  106. imports = append(imports, fmt.Sprintf("\"%s\"", ctlutil.JoinPackages(parentPkg, contextDir)))
  107. if len(route.ResponseTypeName()) > 0 || len(route.RequestTypeName()) > 0 {
  108. imports = append(imports, fmt.Sprintf("\"%s\"\n", ctlutil.JoinPackages(parentPkg, typesDir)))
  109. }
  110. imports = append(imports, fmt.Sprintf("\"%s/core/logx\"", vars.ProjectOpenSourceUrl))
  111. return strings.Join(imports, "\n\t")
  112. }