gencall.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package generator
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strings"
  6. "github.com/emicklei/proto"
  7. "github.com/tal-tech/go-zero/core/collection"
  8. conf "github.com/tal-tech/go-zero/tools/goctl/config"
  9. "github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
  10. "github.com/tal-tech/go-zero/tools/goctl/util"
  11. "github.com/tal-tech/go-zero/tools/goctl/util/format"
  12. "github.com/tal-tech/go-zero/tools/goctl/util/stringx"
  13. )
  14. const (
  15. callTemplateText = `{{.head}}
  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{{if .hasReq}},in *{{.pbRequest}}{{end}}) ({{if .notStream}}*{{.pbResponse}}, {{else}}{{.streamBody}},{{end}} error)`
  40. callFunctionTemplate = `
  41. {{if .hasComment}}{{.comment}}{{end}}
  42. func (m *default{{.serviceName}}) {{.method}}(ctx context.Context{{if .hasReq}},in *{{.pbRequest}}{{end}}) ({{if .notStream}}*{{.pbResponse}}, {{else}}{{.streamBody}},{{end}} error) {
  43. client := {{.package}}.New{{.rpcServiceName}}Client(m.cli.Conn())
  44. return client.{{.method}}(ctx,{{if .hasReq}} in{{end}})
  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(proto.PbPackage, 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. alias := collection.NewSet()
  72. for _, item := range proto.Message {
  73. msgName := getMessageName(*item.Message)
  74. alias.AddStr(fmt.Sprintf("%s = %s", parser.CamelCase(msgName), fmt.Sprintf("%s.%s", proto.PbPackage, parser.CamelCase(msgName))))
  75. }
  76. err = util.With("shared").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
  77. "name": callFilename,
  78. "alias": strings.Join(alias.KeysStr(), util.NL),
  79. "head": head,
  80. "filePackage": dir.Base,
  81. "package": fmt.Sprintf(`"%s"`, ctx.GetPb().Package),
  82. "serviceName": stringx.From(service.Name).ToCamel(),
  83. "functions": strings.Join(functions, util.NL),
  84. "interface": strings.Join(iFunctions, util.NL),
  85. }, filename, true)
  86. return err
  87. }
  88. func getMessageName(msg proto.Message) string {
  89. list := []string{msg.Name}
  90. for {
  91. parent := msg.Parent
  92. if parent == nil {
  93. break
  94. }
  95. parentMsg, ok := parent.(*proto.Message)
  96. if !ok {
  97. break
  98. }
  99. tmp := []string{parentMsg.Name}
  100. list = append(tmp, list...)
  101. msg = *parentMsg
  102. }
  103. return strings.Join(list, "_")
  104. }
  105. func (g *DefaultGenerator) genFunction(goPackage string, service parser.Service) ([]string, error) {
  106. functions := make([]string, 0)
  107. for _, rpc := range service.RPC {
  108. text, err := util.LoadTemplate(category, callFunctionTemplateFile, callFunctionTemplate)
  109. if err != nil {
  110. return nil, err
  111. }
  112. comment := parser.GetComment(rpc.Doc())
  113. streamServer := fmt.Sprintf("%s.%s_%s%s", goPackage, parser.CamelCase(service.Name), parser.CamelCase(rpc.Name), "Client")
  114. buffer, err := util.With("sharedFn").Parse(text).Execute(map[string]interface{}{
  115. "serviceName": stringx.From(service.Name).ToCamel(),
  116. "rpcServiceName": parser.CamelCase(service.Name),
  117. "method": parser.CamelCase(rpc.Name),
  118. "package": goPackage,
  119. "pbRequest": parser.CamelCase(rpc.RequestType),
  120. "pbResponse": parser.CamelCase(rpc.ReturnsType),
  121. "hasComment": len(comment) > 0,
  122. "comment": comment,
  123. "hasReq": !rpc.StreamsRequest,
  124. "notStream": !rpc.StreamsRequest && !rpc.StreamsReturns,
  125. "streamBody": streamServer,
  126. })
  127. if err != nil {
  128. return nil, err
  129. }
  130. functions = append(functions, buffer.String())
  131. }
  132. return functions, nil
  133. }
  134. func (g *DefaultGenerator) getInterfaceFuncs(goPackage string, service parser.Service) ([]string, error) {
  135. functions := make([]string, 0)
  136. for _, rpc := range service.RPC {
  137. text, err := util.LoadTemplate(category, callInterfaceFunctionTemplateFile, callInterfaceFunctionTemplate)
  138. if err != nil {
  139. return nil, err
  140. }
  141. comment := parser.GetComment(rpc.Doc())
  142. streamServer := fmt.Sprintf("%s.%s_%s%s", goPackage, parser.CamelCase(service.Name), parser.CamelCase(rpc.Name), "Client")
  143. buffer, err := util.With("interfaceFn").Parse(text).Execute(
  144. map[string]interface{}{
  145. "hasComment": len(comment) > 0,
  146. "comment": comment,
  147. "method": parser.CamelCase(rpc.Name),
  148. "hasReq": !rpc.StreamsRequest,
  149. "pbRequest": parser.CamelCase(rpc.RequestType),
  150. "notStream": !rpc.StreamsRequest && !rpc.StreamsReturns,
  151. "pbResponse": parser.CamelCase(rpc.ReturnsType),
  152. "streamBody": streamServer,
  153. })
  154. if err != nil {
  155. return nil, err
  156. }
  157. functions = append(functions, buffer.String())
  158. }
  159. return functions, nil
  160. }