gencall.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. callTemplateText = `{{.head}}
  15. package {{.filePackage}}
  16. import (
  17. "context"
  18. {{.package}}
  19. "github.com/tal-tech/go-zero/zrpc"
  20. )
  21. type (
  22. {{.alias}}
  23. {{.serviceName}} interface {
  24. {{.interface}}
  25. }
  26. default{{.serviceName}} struct {
  27. cli zrpc.Client
  28. }
  29. )
  30. func New{{.serviceName}}(cli zrpc.Client) {{.serviceName}} {
  31. return &default{{.serviceName}}{
  32. cli: cli,
  33. }
  34. }
  35. {{.functions}}
  36. `
  37. callInterfaceFunctionTemplate = `{{if .hasComment}}{{.comment}}
  38. {{end}}{{.method}}(ctx context.Context,in *{{.pbRequest}}) (*{{.pbResponse}},error)`
  39. callFunctionTemplate = `
  40. {{if .hasComment}}{{.comment}}{{end}}
  41. func (m *default{{.serviceName}}) {{.method}}(ctx context.Context,in *{{.pbRequest}}) (*{{.pbResponse}}, error) {
  42. client := {{.package}}.New{{.rpcServiceName}}Client(m.cli.Conn())
  43. return client.{{.method}}(ctx, in)
  44. }
  45. `
  46. )
  47. // GenCall generates the rpc client code, which is the entry point for the rpc service call.
  48. // It is a layer of encapsulation for the rpc client and shields the details in the pb.
  49. func (g *DefaultGenerator) GenCall(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
  50. dir := ctx.GetCall()
  51. service := proto.Service
  52. head := util.GetHead(proto.Name)
  53. callFilename, err := format.FileNamingFormat(cfg.NamingFormat, service.Name)
  54. if err != nil {
  55. return err
  56. }
  57. filename := filepath.Join(dir.Filename, fmt.Sprintf("%s.go", callFilename))
  58. functions, err := g.genFunction(proto.PbPackage, service)
  59. if err != nil {
  60. return err
  61. }
  62. iFunctions, err := g.getInterfaceFuncs(service)
  63. if err != nil {
  64. return err
  65. }
  66. text, err := util.LoadTemplate(category, callTemplateFile, callTemplateText)
  67. if err != nil {
  68. return err
  69. }
  70. alias := collection.NewSet()
  71. for _, item := range proto.Message {
  72. alias.AddStr(fmt.Sprintf("%s = %s", parser.CamelCase(item.Name), fmt.Sprintf("%s.%s", proto.PbPackage, parser.CamelCase(item.Name))))
  73. }
  74. err = util.With("shared").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
  75. "name": callFilename,
  76. "alias": strings.Join(alias.KeysStr(), util.NL),
  77. "head": head,
  78. "filePackage": dir.Base,
  79. "package": fmt.Sprintf(`"%s"`, ctx.GetPb().Package),
  80. "serviceName": stringx.From(service.Name).ToCamel(),
  81. "functions": strings.Join(functions, util.NL),
  82. "interface": strings.Join(iFunctions, util.NL),
  83. }, filename, true)
  84. return err
  85. }
  86. func (g *DefaultGenerator) genFunction(goPackage string, service parser.Service) ([]string, error) {
  87. functions := make([]string, 0)
  88. for _, rpc := range service.RPC {
  89. text, err := util.LoadTemplate(category, callFunctionTemplateFile, callFunctionTemplate)
  90. if err != nil {
  91. return nil, err
  92. }
  93. comment := parser.GetComment(rpc.Doc())
  94. buffer, err := util.With("sharedFn").Parse(text).Execute(map[string]interface{}{
  95. "serviceName": stringx.From(service.Name).ToCamel(),
  96. "rpcServiceName": parser.CamelCase(service.Name),
  97. "method": parser.CamelCase(rpc.Name),
  98. "package": goPackage,
  99. "pbRequest": parser.CamelCase(rpc.RequestType),
  100. "pbResponse": parser.CamelCase(rpc.ReturnsType),
  101. "hasComment": len(comment) > 0,
  102. "comment": comment,
  103. })
  104. if err != nil {
  105. return nil, err
  106. }
  107. functions = append(functions, buffer.String())
  108. }
  109. return functions, nil
  110. }
  111. func (g *DefaultGenerator) getInterfaceFuncs(service parser.Service) ([]string, error) {
  112. functions := make([]string, 0)
  113. for _, rpc := range service.RPC {
  114. text, err := util.LoadTemplate(category, callInterfaceFunctionTemplateFile, callInterfaceFunctionTemplate)
  115. if err != nil {
  116. return nil, err
  117. }
  118. comment := parser.GetComment(rpc.Doc())
  119. buffer, err := util.With("interfaceFn").Parse(text).Execute(
  120. map[string]interface{}{
  121. "hasComment": len(comment) > 0,
  122. "comment": comment,
  123. "method": parser.CamelCase(rpc.Name),
  124. "pbRequest": parser.CamelCase(rpc.RequestType),
  125. "pbResponse": parser.CamelCase(rpc.ReturnsType),
  126. })
  127. if err != nil {
  128. return nil, err
  129. }
  130. functions = append(functions, buffer.String())
  131. }
  132. return functions, nil
  133. }