genlogic.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package generator
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strings"
  6. "github.com/tal-tech/go-zero/core/collection"
  7. conf "github.com/tal-tech/go-zero/tools/goctl/config"
  8. "github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
  9. "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/util/stringx"
  12. )
  13. const (
  14. logicTemplate = `package logic
  15. import (
  16. "context"
  17. {{.imports}}
  18. "github.com/tal-tech/go-zero/core/logx"
  19. )
  20. type {{.logicName}} struct {
  21. ctx context.Context
  22. svcCtx *svc.ServiceContext
  23. logx.Logger
  24. }
  25. func New{{.logicName}}(ctx context.Context,svcCtx *svc.ServiceContext) *{{.logicName}} {
  26. return &{{.logicName}}{
  27. ctx: ctx,
  28. svcCtx: svcCtx,
  29. Logger: logx.WithContext(ctx),
  30. }
  31. }
  32. {{.functions}}
  33. `
  34. logicFunctionTemplate = `{{if .hasComment}}{{.comment}}{{end}}
  35. func (l *{{.logicName}}) {{.method}} ({{if .hasReq}}in {{.request}}{{if .stream}},stream {{.streamBody}}{{end}}{{else}}stream {{.streamBody}}{{end}}) ({{if .hasReply}}{{.response}},{{end}} error) {
  36. // todo: add your logic here and delete this line
  37. return {{if .hasReply}}&{{.responseType}}{},{{end}} nil
  38. }
  39. `
  40. )
  41. // GenLogic generates the logic file of the rpc service, which corresponds to the RPC definition items in proto.
  42. func (g *DefaultGenerator) GenLogic(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
  43. dir := ctx.GetLogic()
  44. service := proto.Service.Service.Name
  45. for _, rpc := range proto.Service.RPC {
  46. logicFilename, err := format.FileNamingFormat(cfg.NamingFormat, rpc.Name+"_logic")
  47. if err != nil {
  48. return err
  49. }
  50. filename := filepath.Join(dir.Filename, logicFilename+".go")
  51. functions, err := g.genLogicFunction(service, proto.PbPackage, rpc)
  52. if err != nil {
  53. return err
  54. }
  55. imports := collection.NewSet()
  56. imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetSvc().Package))
  57. imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetPb().Package))
  58. text, err := util.LoadTemplate(category, logicTemplateFileFile, logicTemplate)
  59. if err != nil {
  60. return err
  61. }
  62. err = util.With("logic").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
  63. "logicName": fmt.Sprintf("%sLogic", stringx.From(rpc.Name).ToCamel()),
  64. "functions": functions,
  65. "imports": strings.Join(imports.KeysStr(), util.NL),
  66. }, filename, false)
  67. if err != nil {
  68. return err
  69. }
  70. }
  71. return nil
  72. }
  73. func (g *DefaultGenerator) genLogicFunction(serviceName, goPackage string, rpc *parser.RPC) (string, error) {
  74. functions := make([]string, 0)
  75. text, err := util.LoadTemplate(category, logicFuncTemplateFileFile, logicFunctionTemplate)
  76. if err != nil {
  77. return "", err
  78. }
  79. logicName := stringx.From(rpc.Name + "_logic").ToCamel()
  80. comment := parser.GetComment(rpc.Doc())
  81. var streamServer string
  82. if rpc.StreamsRequest && rpc.StreamsReturns {
  83. streamServer = fmt.Sprintf("%s.%s", goPackage, parser.CamelCase(serviceName)+"_StreamServer")
  84. } else if rpc.StreamsRequest {
  85. streamServer = fmt.Sprintf("%s.%s", goPackage, parser.CamelCase(serviceName)+"_ClientStreamServer")
  86. } else {
  87. streamServer = fmt.Sprintf("%s.%s", goPackage, parser.CamelCase(serviceName)+"_ServerStreamServer")
  88. }
  89. buffer, err := util.With("fun").Parse(text).Execute(map[string]interface{}{
  90. "logicName": logicName,
  91. "method": parser.CamelCase(rpc.Name),
  92. "hasReq": !rpc.StreamsRequest,
  93. "request": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.RequestType)),
  94. "hasReply": !rpc.StreamsRequest && !rpc.StreamsReturns,
  95. "response": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.ReturnsType)),
  96. "responseType": fmt.Sprintf("%s.%s", goPackage, parser.CamelCase(rpc.ReturnsType)),
  97. "stream": rpc.StreamsRequest || rpc.StreamsReturns,
  98. "streamBody": streamServer,
  99. "hasComment": len(comment) > 0,
  100. "comment": comment,
  101. })
  102. if err != nil {
  103. return "", err
  104. }
  105. functions = append(functions, buffer.String())
  106. return strings.Join(functions, util.NL), nil
  107. }