genlogic.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package generator
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "path/filepath"
  6. "strings"
  7. "github.com/zeromicro/go-zero/core/collection"
  8. conf "github.com/zeromicro/go-zero/tools/goctl/config"
  9. "github.com/zeromicro/go-zero/tools/goctl/rpc/parser"
  10. "github.com/zeromicro/go-zero/tools/goctl/util"
  11. "github.com/zeromicro/go-zero/tools/goctl/util/format"
  12. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  13. "github.com/zeromicro/go-zero/tools/goctl/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) error {
  25. dir := ctx.GetLogic()
  26. service := proto.Service.Service.Name
  27. for _, rpc := range proto.Service.RPC {
  28. logicFilename, err := format.FileNamingFormat(cfg.NamingFormat, rpc.Name+"_logic")
  29. if err != nil {
  30. return err
  31. }
  32. filename := filepath.Join(dir.Filename, logicFilename+".go")
  33. functions, err := g.genLogicFunction(service, proto.PbPackage, rpc)
  34. if err != nil {
  35. return err
  36. }
  37. imports := collection.NewSet()
  38. imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetSvc().Package))
  39. imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetPb().Package))
  40. text, err := pathx.LoadTemplate(category, logicTemplateFileFile, logicTemplate)
  41. if err != nil {
  42. return err
  43. }
  44. err = util.With("logic").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
  45. "logicName": fmt.Sprintf("%sLogic", stringx.From(rpc.Name).ToCamel()),
  46. "functions": functions,
  47. "imports": strings.Join(imports.KeysStr(), pathx.NL),
  48. }, filename, false)
  49. if err != nil {
  50. return err
  51. }
  52. }
  53. return nil
  54. }
  55. func (g *Generator) genLogicFunction(serviceName, goPackage string, rpc *parser.RPC) (string, error) {
  56. functions := make([]string, 0)
  57. text, err := pathx.LoadTemplate(category, logicFuncTemplateFileFile, logicFunctionTemplate)
  58. if err != nil {
  59. return "", err
  60. }
  61. logicName := stringx.From(rpc.Name + "_logic").ToCamel()
  62. comment := parser.GetComment(rpc.Doc())
  63. streamServer := fmt.Sprintf("%s.%s_%s%s", goPackage, parser.CamelCase(serviceName), parser.CamelCase(rpc.Name), "Server")
  64. buffer, err := util.With("fun").Parse(text).Execute(map[string]interface{}{
  65. "logicName": logicName,
  66. "method": parser.CamelCase(rpc.Name),
  67. "hasReq": !rpc.StreamsRequest,
  68. "request": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.RequestType)),
  69. "hasReply": !rpc.StreamsRequest && !rpc.StreamsReturns,
  70. "response": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.ReturnsType)),
  71. "responseType": fmt.Sprintf("%s.%s", goPackage, parser.CamelCase(rpc.ReturnsType)),
  72. "stream": rpc.StreamsRequest || rpc.StreamsReturns,
  73. "streamBody": streamServer,
  74. "hasComment": len(comment) > 0,
  75. "comment": comment,
  76. })
  77. if err != nil {
  78. return "", err
  79. }
  80. functions = append(functions, buffer.String())
  81. return strings.Join(functions, pathx.NL), nil
  82. }