genlogic.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package generator
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strings"
  6. "github.com/tal-tech/go-zero/core/collection"
  7. "github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
  8. "github.com/tal-tech/go-zero/tools/goctl/util"
  9. "github.com/tal-tech/go-zero/tools/goctl/util/stringx"
  10. )
  11. const (
  12. logicTemplate = `package logic
  13. import (
  14. "context"
  15. {{.imports}}
  16. "github.com/tal-tech/go-zero/core/logx"
  17. )
  18. type {{.logicName}} struct {
  19. ctx context.Context
  20. svcCtx *svc.ServiceContext
  21. logx.Logger
  22. }
  23. func New{{.logicName}}(ctx context.Context,svcCtx *svc.ServiceContext) *{{.logicName}} {
  24. return &{{.logicName}}{
  25. ctx: ctx,
  26. svcCtx: svcCtx,
  27. Logger: logx.WithContext(ctx),
  28. }
  29. }
  30. {{.functions}}
  31. `
  32. logicFunctionTemplate = `{{if .hasComment}}{{.comment}}{{end}}
  33. func (l *{{.logicName}}) {{.method}} (in {{.request}}) ({{.response}}, error) {
  34. // todo: add your logic here and delete this line
  35. return &{{.responseType}}{}, nil
  36. }
  37. `
  38. )
  39. func (g *defaultGenerator) GenLogic(ctx DirContext, proto parser.Proto, namingStyle NamingStyle) error {
  40. dir := ctx.GetLogic()
  41. for _, rpc := range proto.Service.RPC {
  42. filename := filepath.Join(dir.Filename, formatFilename(rpc.Name+"_logic", namingStyle)+".go")
  43. functions, err := g.genLogicFunction(proto.PbPackage, rpc)
  44. if err != nil {
  45. return err
  46. }
  47. imports := collection.NewSet()
  48. imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetSvc().Package))
  49. imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetPb().Package))
  50. text, err := util.LoadTemplate(category, logicTemplateFileFile, logicTemplate)
  51. if err != nil {
  52. return err
  53. }
  54. err = util.With("logic").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
  55. "logicName": fmt.Sprintf("%sLogic", stringx.From(rpc.Name).ToCamel()),
  56. "functions": functions,
  57. "imports": strings.Join(imports.KeysStr(), util.NL),
  58. }, filename, false)
  59. if err != nil {
  60. return err
  61. }
  62. }
  63. return nil
  64. }
  65. func (g *defaultGenerator) genLogicFunction(goPackage string, rpc *parser.RPC) (string, error) {
  66. var functions = make([]string, 0)
  67. text, err := util.LoadTemplate(category, logicFuncTemplateFileFile, logicFunctionTemplate)
  68. if err != nil {
  69. return "", err
  70. }
  71. logicName := stringx.From(rpc.Name + "_logic").ToCamel()
  72. comment := parser.GetComment(rpc.Doc())
  73. buffer, err := util.With("fun").Parse(text).Execute(map[string]interface{}{
  74. "logicName": logicName,
  75. "method": parser.CamelCase(rpc.Name),
  76. "request": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.RequestType)),
  77. "response": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.ReturnsType)),
  78. "responseType": fmt.Sprintf("%s.%s", goPackage, parser.CamelCase(rpc.ReturnsType)),
  79. "hasComment": len(comment) > 0,
  80. "comment": comment,
  81. })
  82. if err != nil {
  83. return "", err
  84. }
  85. functions = append(functions, buffer.String())
  86. return strings.Join(functions, util.NL), nil
  87. }