genlogic.go 3.2 KB

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