genlogic.go 3.0 KB

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