gencall.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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{{if .hasReq}},in *{{.pbRequest}}{{end}}) ({{if .notStream}}*{{.pbResponse}}, {{else}}{{.streamBody}},{{end}} error)`
  39. callFunctionTemplate = `
  40. {{if .hasComment}}{{.comment}}{{end}}
  41. func (m *default{{.serviceName}}) {{.method}}(ctx context.Context{{if .hasReq}},in *{{.pbRequest}}{{end}}) ({{if .notStream}}*{{.pbResponse}}, {{else}}{{.streamBody}},{{end}} error) {
  42. client := {{.package}}.New{{.rpcServiceName}}Client(m.cli.Conn())
  43. return client.{{.method}}(ctx,{{if .hasReq}} in{{end}})
  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(proto.PbPackage, 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. streamServer := fmt.Sprintf("%s.%s_%s%s", goPackage, parser.CamelCase(service.Name), parser.CamelCase(rpc.Name), "Client")
  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. "hasReq": !rpc.StreamsRequest,
  105. "notStream": !rpc.StreamsRequest && !rpc.StreamsReturns,
  106. "streamBody": streamServer,
  107. })
  108. if err != nil {
  109. return nil, err
  110. }
  111. functions = append(functions, buffer.String())
  112. }
  113. return functions, nil
  114. }
  115. func (g *DefaultGenerator) getInterfaceFuncs(goPackage string, service parser.Service) ([]string, error) {
  116. functions := make([]string, 0)
  117. for _, rpc := range service.RPC {
  118. text, err := util.LoadTemplate(category, callInterfaceFunctionTemplateFile, callInterfaceFunctionTemplate)
  119. if err != nil {
  120. return nil, err
  121. }
  122. comment := parser.GetComment(rpc.Doc())
  123. streamServer := fmt.Sprintf("%s.%s_%s%s", goPackage, parser.CamelCase(service.Name), parser.CamelCase(rpc.Name), "Client")
  124. buffer, err := util.With("interfaceFn").Parse(text).Execute(
  125. map[string]interface{}{
  126. "hasComment": len(comment) > 0,
  127. "comment": comment,
  128. "method": parser.CamelCase(rpc.Name),
  129. "hasReq": !rpc.StreamsRequest,
  130. "pbRequest": parser.CamelCase(rpc.RequestType),
  131. "notStream": !rpc.StreamsRequest && !rpc.StreamsReturns,
  132. "pbResponse": parser.CamelCase(rpc.ReturnsType),
  133. "streamBody": streamServer,
  134. })
  135. if err != nil {
  136. return nil, err
  137. }
  138. functions = append(functions, buffer.String())
  139. }
  140. return functions, nil
  141. }