genlogic.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. streamServer := fmt.Sprintf("%s.%s_%s%s", goPackage, parser.CamelCase(serviceName), parser.CamelCase(rpc.Name), "Server")
  82. buffer, err := util.With("fun").Parse(text).Execute(map[string]interface{}{
  83. "logicName": logicName,
  84. "method": parser.CamelCase(rpc.Name),
  85. "hasReq": !rpc.StreamsRequest,
  86. "request": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.RequestType)),
  87. "hasReply": !rpc.StreamsRequest && !rpc.StreamsReturns,
  88. "response": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.ReturnsType)),
  89. "responseType": fmt.Sprintf("%s.%s", goPackage, parser.CamelCase(rpc.ReturnsType)),
  90. "stream": rpc.StreamsRequest || rpc.StreamsReturns,
  91. "streamBody": streamServer,
  92. "hasComment": len(comment) > 0,
  93. "comment": comment,
  94. })
  95. if err != nil {
  96. return "", err
  97. }
  98. functions = append(functions, buffer.String())
  99. return strings.Join(functions, util.NL), nil
  100. }