genlogic.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package gogen
  2. import (
  3. "fmt"
  4. "path"
  5. "strings"
  6. "git.i2edu.net/i2/go-zero/tools/goctl/api/spec"
  7. "git.i2edu.net/i2/go-zero/tools/goctl/config"
  8. ctlutil "git.i2edu.net/i2/go-zero/tools/goctl/util"
  9. "git.i2edu.net/i2/go-zero/tools/goctl/util/format"
  10. "git.i2edu.net/i2/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, rootPkg 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, rootPkg, cfg, g, r)
  37. if err != nil {
  38. return err
  39. }
  40. }
  41. }
  42. return nil
  43. }
  44. func genLogicByRoute(dir, rootPkg 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. imports := genLogicImports(route, rootPkg)
  51. var responseString string
  52. var returnString string
  53. var requestString string
  54. if len(route.ResponseTypeName()) > 0 {
  55. resp := responseGoTypeName(route, typesPacket)
  56. responseString = "(" + resp + ", error)"
  57. if strings.HasPrefix(resp, "*") {
  58. returnString = fmt.Sprintf("return &%s{}, nil", strings.TrimPrefix(resp, "*"))
  59. } else {
  60. returnString = fmt.Sprintf("return %s{}, nil", resp)
  61. }
  62. } else {
  63. responseString = "error"
  64. returnString = "return nil"
  65. }
  66. if len(route.RequestTypeName()) > 0 {
  67. requestString = "req " + requestGoTypeName(route, typesPacket)
  68. }
  69. return genFile(fileGenConfig{
  70. dir: dir,
  71. subdir: getLogicFolderPath(group, route),
  72. filename: goFile + ".go",
  73. templateName: "logicTemplate",
  74. category: category,
  75. templateFile: logicTemplateFile,
  76. builtinTemplate: logicTemplate,
  77. data: map[string]string{
  78. "imports": imports,
  79. "logic": strings.Title(logic),
  80. "function": strings.Title(strings.TrimSuffix(logic, "Logic")),
  81. "responseType": responseString,
  82. "returnString": returnString,
  83. "request": requestString,
  84. },
  85. })
  86. }
  87. func getLogicFolderPath(group spec.Group, route spec.Route) string {
  88. folder := route.GetAnnotation(groupProperty)
  89. if len(folder) == 0 {
  90. folder = group.GetAnnotation(groupProperty)
  91. if len(folder) == 0 {
  92. return logicDir
  93. }
  94. }
  95. folder = strings.TrimPrefix(folder, "/")
  96. folder = strings.TrimSuffix(folder, "/")
  97. return path.Join(logicDir, folder)
  98. }
  99. func genLogicImports(route spec.Route, parentPkg string) string {
  100. var imports []string
  101. imports = append(imports, `"context"`+"\n")
  102. imports = append(imports, fmt.Sprintf("\"%s\"", ctlutil.JoinPackages(parentPkg, contextDir)))
  103. if len(route.ResponseTypeName()) > 0 || len(route.RequestTypeName()) > 0 {
  104. imports = append(imports, fmt.Sprintf("\"%s\"\n", ctlutil.JoinPackages(parentPkg, typesDir)))
  105. }
  106. imports = append(imports, fmt.Sprintf("\"%s/core/logx\"", vars.ProjectOpenSourceURL))
  107. return strings.Join(imports, "\n\t")
  108. }