gencall.go 4.4 KB

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