genlogic.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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}} (in {{.request}}) ({{.response}}, error) {
  36. // todo: add your logic here and delete this line
  37. return &{{.responseType}}{}, 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. for _, rpc := range proto.Service.RPC {
  45. logicFilename, err := format.FileNamingFormat(cfg.NamingFormat, rpc.Name+"_logic")
  46. if err != nil {
  47. return err
  48. }
  49. filename := filepath.Join(dir.Filename, logicFilename+".go")
  50. functions, err := g.genLogicFunction(proto.PbPackage, rpc)
  51. if err != nil {
  52. return err
  53. }
  54. imports := collection.NewSet()
  55. imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetSvc().Package))
  56. imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetPb().Package))
  57. text, err := util.LoadTemplate(category, logicTemplateFileFile, logicTemplate)
  58. if err != nil {
  59. return err
  60. }
  61. err = util.With("logic").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
  62. "logicName": fmt.Sprintf("%sLogic", stringx.From(rpc.Name).ToCamel()),
  63. "functions": functions,
  64. "imports": strings.Join(imports.KeysStr(), util.NL),
  65. }, filename, false)
  66. if err != nil {
  67. return err
  68. }
  69. }
  70. return nil
  71. }
  72. func (g *DefaultGenerator) genLogicFunction(goPackage string, rpc *parser.RPC) (string, error) {
  73. var functions = make([]string, 0)
  74. text, err := util.LoadTemplate(category, logicFuncTemplateFileFile, logicFunctionTemplate)
  75. if err != nil {
  76. return "", err
  77. }
  78. logicName := stringx.From(rpc.Name + "_logic").ToCamel()
  79. comment := parser.GetComment(rpc.Doc())
  80. buffer, err := util.With("fun").Parse(text).Execute(map[string]interface{}{
  81. "logicName": logicName,
  82. "method": parser.CamelCase(rpc.Name),
  83. "request": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.RequestType)),
  84. "response": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.ReturnsType)),
  85. "responseType": fmt.Sprintf("%s.%s", goPackage, parser.CamelCase(rpc.ReturnsType)),
  86. "hasComment": len(comment) > 0,
  87. "comment": comment,
  88. })
  89. if err != nil {
  90. return "", err
  91. }
  92. functions = append(functions, buffer.String())
  93. return strings.Join(functions, util.NL), nil
  94. }