gencall.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package generator
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "sort"
  6. "strings"
  7. "github.com/emicklei/proto"
  8. "github.com/tal-tech/go-zero/core/collection"
  9. conf "github.com/tal-tech/go-zero/tools/goctl/config"
  10. "github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
  11. "github.com/tal-tech/go-zero/tools/goctl/util"
  12. "github.com/tal-tech/go-zero/tools/goctl/util/format"
  13. "github.com/tal-tech/go-zero/tools/goctl/util/pathx"
  14. "github.com/tal-tech/go-zero/tools/goctl/util/stringx"
  15. )
  16. const (
  17. callTemplateText = `{{.head}}
  18. package {{.filePackage}}
  19. import (
  20. "context"
  21. {{.package}}
  22. "github.com/zeromicro/go-zero/zrpc"
  23. "google.golang.org/grpc"
  24. )
  25. type (
  26. {{.alias}}
  27. {{.serviceName}} interface {
  28. {{.interface}}
  29. }
  30. default{{.serviceName}} struct {
  31. cli zrpc.Client
  32. }
  33. )
  34. func New{{.serviceName}}(cli zrpc.Client) {{.serviceName}} {
  35. return &default{{.serviceName}}{
  36. cli: cli,
  37. }
  38. }
  39. {{.functions}}
  40. `
  41. callInterfaceFunctionTemplate = `{{if .hasComment}}{{.comment}}
  42. {{end}}{{.method}}(ctx context.Context{{if .hasReq}}, in *{{.pbRequest}}{{end}}, opts ...grpc.CallOption) ({{if .notStream}}*{{.pbResponse}}, {{else}}{{.streamBody}},{{end}} error)`
  43. callFunctionTemplate = `
  44. {{if .hasComment}}{{.comment}}{{end}}
  45. func (m *default{{.serviceName}}) {{.method}}(ctx context.Context{{if .hasReq}}, in *{{.pbRequest}}{{end}}, opts ...grpc.CallOption) ({{if .notStream}}*{{.pbResponse}}, {{else}}{{.streamBody}},{{end}} error) {
  46. client := {{.package}}.New{{.rpcServiceName}}Client(m.cli.Conn())
  47. return client.{{.method}}(ctx{{if .hasReq}}, in{{end}}, opts...)
  48. }
  49. `
  50. )
  51. // GenCall generates the rpc client code, which is the entry point for the rpc service call.
  52. // It is a layer of encapsulation for the rpc client and shields the details in the pb.
  53. func (g *DefaultGenerator) GenCall(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
  54. dir := ctx.GetCall()
  55. service := proto.Service
  56. head := util.GetHead(proto.Name)
  57. callFilename, err := format.FileNamingFormat(cfg.NamingFormat, service.Name)
  58. if err != nil {
  59. return err
  60. }
  61. filename := filepath.Join(dir.Filename, fmt.Sprintf("%s.go", callFilename))
  62. functions, err := g.genFunction(proto.PbPackage, service)
  63. if err != nil {
  64. return err
  65. }
  66. iFunctions, err := g.getInterfaceFuncs(proto.PbPackage, service)
  67. if err != nil {
  68. return err
  69. }
  70. text, err := pathx.LoadTemplate(category, callTemplateFile, callTemplateText)
  71. if err != nil {
  72. return err
  73. }
  74. alias := collection.NewSet()
  75. for _, item := range proto.Message {
  76. msgName := getMessageName(*item.Message)
  77. alias.AddStr(fmt.Sprintf("%s = %s", parser.CamelCase(msgName), fmt.Sprintf("%s.%s", proto.PbPackage, parser.CamelCase(msgName))))
  78. }
  79. aliasKeys := alias.KeysStr()
  80. sort.Strings(aliasKeys)
  81. err = util.With("shared").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
  82. "name": callFilename,
  83. "alias": strings.Join(aliasKeys, pathx.NL),
  84. "head": head,
  85. "filePackage": dir.Base,
  86. "package": fmt.Sprintf(`"%s"`, ctx.GetPb().Package),
  87. "serviceName": stringx.From(service.Name).ToCamel(),
  88. "functions": strings.Join(functions, pathx.NL),
  89. "interface": strings.Join(iFunctions, pathx.NL),
  90. }, filename, true)
  91. return err
  92. }
  93. func getMessageName(msg proto.Message) string {
  94. list := []string{msg.Name}
  95. for {
  96. parent := msg.Parent
  97. if parent == nil {
  98. break
  99. }
  100. parentMsg, ok := parent.(*proto.Message)
  101. if !ok {
  102. break
  103. }
  104. tmp := []string{parentMsg.Name}
  105. list = append(tmp, list...)
  106. msg = *parentMsg
  107. }
  108. return strings.Join(list, "_")
  109. }
  110. func (g *DefaultGenerator) genFunction(goPackage string, service parser.Service) ([]string, error) {
  111. functions := make([]string, 0)
  112. for _, rpc := range service.RPC {
  113. text, err := pathx.LoadTemplate(category, callFunctionTemplateFile, callFunctionTemplate)
  114. if err != nil {
  115. return nil, err
  116. }
  117. comment := parser.GetComment(rpc.Doc())
  118. streamServer := fmt.Sprintf("%s.%s_%s%s", goPackage, parser.CamelCase(service.Name), parser.CamelCase(rpc.Name), "Client")
  119. buffer, err := util.With("sharedFn").Parse(text).Execute(map[string]interface{}{
  120. "serviceName": stringx.From(service.Name).ToCamel(),
  121. "rpcServiceName": parser.CamelCase(service.Name),
  122. "method": parser.CamelCase(rpc.Name),
  123. "package": goPackage,
  124. "pbRequest": parser.CamelCase(rpc.RequestType),
  125. "pbResponse": parser.CamelCase(rpc.ReturnsType),
  126. "hasComment": len(comment) > 0,
  127. "comment": comment,
  128. "hasReq": !rpc.StreamsRequest,
  129. "notStream": !rpc.StreamsRequest && !rpc.StreamsReturns,
  130. "streamBody": streamServer,
  131. })
  132. if err != nil {
  133. return nil, err
  134. }
  135. functions = append(functions, buffer.String())
  136. }
  137. return functions, nil
  138. }
  139. func (g *DefaultGenerator) getInterfaceFuncs(goPackage string, service parser.Service) ([]string, error) {
  140. functions := make([]string, 0)
  141. for _, rpc := range service.RPC {
  142. text, err := pathx.LoadTemplate(category, callInterfaceFunctionTemplateFile, callInterfaceFunctionTemplate)
  143. if err != nil {
  144. return nil, err
  145. }
  146. comment := parser.GetComment(rpc.Doc())
  147. streamServer := fmt.Sprintf("%s.%s_%s%s", goPackage, parser.CamelCase(service.Name), parser.CamelCase(rpc.Name), "Client")
  148. buffer, err := util.With("interfaceFn").Parse(text).Execute(
  149. map[string]interface{}{
  150. "hasComment": len(comment) > 0,
  151. "comment": comment,
  152. "method": parser.CamelCase(rpc.Name),
  153. "hasReq": !rpc.StreamsRequest,
  154. "pbRequest": parser.CamelCase(rpc.RequestType),
  155. "notStream": !rpc.StreamsRequest && !rpc.StreamsReturns,
  156. "pbResponse": parser.CamelCase(rpc.ReturnsType),
  157. "streamBody": streamServer,
  158. })
  159. if err != nil {
  160. return nil, err
  161. }
  162. functions = append(functions, buffer.String())
  163. }
  164. return functions, nil
  165. }