genlogic.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/api/util"
  8. "github.com/tal-tech/go-zero/tools/goctl/config"
  9. ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
  10. "github.com/tal-tech/go-zero/tools/goctl/util/format"
  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, cfg *config.Config, api *spec.ApiSpec) error {
  35. for _, g := range api.Service.Groups {
  36. for _, r := range g.Routes {
  37. err := genLogicByRoute(dir, cfg, g, r)
  38. if err != nil {
  39. return err
  40. }
  41. }
  42. }
  43. return nil
  44. }
  45. func genLogicByRoute(dir string, cfg *config.Config, group spec.Group, route spec.Route) error {
  46. logic := getLogicName(route)
  47. goFile, err := format.FileNamingFormat(cfg.NamingFormat, logic)
  48. if err != nil {
  49. return err
  50. }
  51. parentPkg, err := getParentPackage(dir)
  52. if err != nil {
  53. return err
  54. }
  55. imports := genLogicImports(route, parentPkg)
  56. var responseString string
  57. var returnString string
  58. var requestString string
  59. if len(route.ResponseType.Name) > 0 {
  60. resp := strings.Title(route.ResponseType.Name)
  61. responseString = "(*types." + resp + ", error)"
  62. returnString = fmt.Sprintf("return &types.%s{}, nil", resp)
  63. } else {
  64. responseString = "error"
  65. returnString = "return nil"
  66. }
  67. if len(route.RequestType.Name) > 0 {
  68. requestString = "req " + "types." + strings.Title(route.RequestType.Name)
  69. }
  70. return genFile(fileGenConfig{
  71. dir: dir,
  72. subdir: getLogicFolderPath(group, route),
  73. filename: goFile + ".go",
  74. templateName: "logicTemplate",
  75. category: category,
  76. templateFile: logicTemplateFile,
  77. builtinTemplate: logicTemplate,
  78. data: map[string]string{
  79. "imports": imports,
  80. "logic": strings.Title(logic),
  81. "function": strings.Title(strings.TrimSuffix(logic, "Logic")),
  82. "responseType": responseString,
  83. "returnString": returnString,
  84. "request": requestString,
  85. },
  86. })
  87. }
  88. func getLogicFolderPath(group spec.Group, route spec.Route) string {
  89. folder, ok := util.GetAnnotationValue(route.Annotations, "server", groupProperty)
  90. if !ok {
  91. folder, ok = util.GetAnnotationValue(group.Annotations, "server", groupProperty)
  92. if !ok {
  93. return logicDir
  94. }
  95. }
  96. folder = strings.TrimPrefix(folder, "/")
  97. folder = strings.TrimSuffix(folder, "/")
  98. return path.Join(logicDir, folder)
  99. }
  100. func genLogicImports(route spec.Route, parentPkg string) string {
  101. var imports []string
  102. imports = append(imports, `"context"`+"\n")
  103. imports = append(imports, fmt.Sprintf("\"%s\"", ctlutil.JoinPackages(parentPkg, contextDir)))
  104. if len(route.ResponseType.Name) > 0 || len(route.RequestType.Name) > 0 {
  105. imports = append(imports, fmt.Sprintf("\"%s\"\n", ctlutil.JoinPackages(parentPkg, typesDir)))
  106. }
  107. imports = append(imports, fmt.Sprintf("\"%s/core/logx\"", vars.ProjectOpenSourceUrl))
  108. return strings.Join(imports, "\n\t")
  109. }