genlogic.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package generator
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "path/filepath"
  6. "strings"
  7. "github.com/wuntsong-org/go-zero-plus/core/collection"
  8. conf "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/config"
  9. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/rpc/parser"
  10. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util"
  11. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/format"
  12. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/pathx"
  13. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/stringx"
  14. )
  15. const logicFunctionTemplate = `{{if .hasComment}}{{.comment}}{{end}}
  16. func (l *{{.logicName}}) {{.method}} ({{if .hasReq}}in {{.request}}{{if .stream}},stream {{.streamBody}}{{end}}{{else}}stream {{.streamBody}}{{end}}) ({{if .hasReply}}{{.response}},{{end}} error) {
  17. // todo: add your logic here and delete this line
  18. return {{if .hasReply}}&{{.responseType}}{},{{end}} nil
  19. }
  20. `
  21. //go:embed logic.tpl
  22. var logicTemplate string
  23. // GenLogic generates the logic file of the rpc service, which corresponds to the RPC definition items in proto.
  24. func (g *Generator) GenLogic(ctx DirContext, proto parser.Proto, cfg *conf.Config,
  25. c *ZRpcContext) error {
  26. if !c.Multiple {
  27. return g.genLogicInCompatibility(ctx, proto, cfg)
  28. }
  29. return g.genLogicGroup(ctx, proto, cfg)
  30. }
  31. func (g *Generator) genLogicInCompatibility(ctx DirContext, proto parser.Proto,
  32. cfg *conf.Config) error {
  33. dir := ctx.GetLogic()
  34. service := proto.Service[0].Service.Name
  35. for _, rpc := range proto.Service[0].RPC {
  36. logicName := fmt.Sprintf("%sLogic", stringx.From(rpc.Name).ToCamel())
  37. logicFilename, err := format.FileNamingFormat(cfg.NamingFormat, rpc.Name+"_logic")
  38. if err != nil {
  39. return err
  40. }
  41. filename := filepath.Join(dir.Filename, logicFilename+".go")
  42. functions, err := g.genLogicFunction(service, proto.PbPackage, logicName, rpc)
  43. if err != nil {
  44. return err
  45. }
  46. imports := collection.NewSet()
  47. imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetSvc().Package))
  48. imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetPb().Package))
  49. text, err := pathx.LoadTemplate(category, logicTemplateFileFile, logicTemplate)
  50. if err != nil {
  51. return err
  52. }
  53. err = util.With("logic").GoFmt(true).Parse(text).SaveTo(map[string]any{
  54. "logicName": fmt.Sprintf("%sLogic", stringx.From(rpc.Name).ToCamel()),
  55. "functions": functions,
  56. "packageName": "logic",
  57. "imports": strings.Join(imports.KeysStr(), pathx.NL),
  58. }, filename, false)
  59. if err != nil {
  60. return err
  61. }
  62. }
  63. return nil
  64. }
  65. func (g *Generator) genLogicGroup(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
  66. dir := ctx.GetLogic()
  67. for _, item := range proto.Service {
  68. serviceName := item.Name
  69. for _, rpc := range item.RPC {
  70. var (
  71. err error
  72. filename string
  73. logicName string
  74. logicFilename string
  75. packageName string
  76. )
  77. logicName = fmt.Sprintf("%sLogic", stringx.From(rpc.Name).ToCamel())
  78. childPkg, err := dir.GetChildPackage(serviceName)
  79. if err != nil {
  80. return err
  81. }
  82. serviceDir := filepath.Base(childPkg)
  83. nameJoin := fmt.Sprintf("%s_logic", serviceName)
  84. packageName = strings.ToLower(stringx.From(nameJoin).ToCamel())
  85. logicFilename, err = format.FileNamingFormat(cfg.NamingFormat, rpc.Name+"_logic")
  86. if err != nil {
  87. return err
  88. }
  89. filename = filepath.Join(dir.Filename, serviceDir, logicFilename+".go")
  90. functions, err := g.genLogicFunction(serviceName, proto.PbPackage, logicName, rpc)
  91. if err != nil {
  92. return err
  93. }
  94. imports := collection.NewSet()
  95. imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetSvc().Package))
  96. imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetPb().Package))
  97. text, err := pathx.LoadTemplate(category, logicTemplateFileFile, logicTemplate)
  98. if err != nil {
  99. return err
  100. }
  101. if err = util.With("logic").GoFmt(true).Parse(text).SaveTo(map[string]any{
  102. "logicName": logicName,
  103. "functions": functions,
  104. "packageName": packageName,
  105. "imports": strings.Join(imports.KeysStr(), pathx.NL),
  106. }, filename, false); err != nil {
  107. return err
  108. }
  109. }
  110. }
  111. return nil
  112. }
  113. func (g *Generator) genLogicFunction(serviceName, goPackage, logicName string,
  114. rpc *parser.RPC) (string,
  115. error) {
  116. functions := make([]string, 0)
  117. text, err := pathx.LoadTemplate(category, logicFuncTemplateFileFile, logicFunctionTemplate)
  118. if err != nil {
  119. return "", err
  120. }
  121. comment := parser.GetComment(rpc.Doc())
  122. streamServer := fmt.Sprintf("%s.%s_%s%s", goPackage, parser.CamelCase(serviceName),
  123. parser.CamelCase(rpc.Name), "Server")
  124. buffer, err := util.With("fun").Parse(text).Execute(map[string]any{
  125. "logicName": logicName,
  126. "method": parser.CamelCase(rpc.Name),
  127. "hasReq": !rpc.StreamsRequest,
  128. "request": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.RequestType)),
  129. "hasReply": !rpc.StreamsRequest && !rpc.StreamsReturns,
  130. "response": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.ReturnsType)),
  131. "responseType": fmt.Sprintf("%s.%s", goPackage, parser.CamelCase(rpc.ReturnsType)),
  132. "stream": rpc.StreamsRequest || rpc.StreamsReturns,
  133. "streamBody": streamServer,
  134. "hasComment": len(comment) > 0,
  135. "comment": comment,
  136. })
  137. if err != nil {
  138. return "", err
  139. }
  140. functions = append(functions, buffer.String())
  141. return strings.Join(functions, pathx.NL), nil
  142. }