gencall.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. func (g *defaultGenerator) GenCall(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
  49. dir := ctx.GetCall()
  50. service := proto.Service
  51. head := util.GetHead(proto.Name)
  52. callFilename, err := format.FileNamingFormat(cfg.NamingFormat, service.Name)
  53. if err != nil {
  54. return err
  55. }
  56. filename := filepath.Join(dir.Filename, fmt.Sprintf("%s.go", callFilename))
  57. functions, err := g.genFunction(proto.PbPackage, service)
  58. if err != nil {
  59. return err
  60. }
  61. iFunctions, err := g.getInterfaceFuncs(service)
  62. if err != nil {
  63. return err
  64. }
  65. text, err := util.LoadTemplate(category, callTemplateFile, callTemplateText)
  66. if err != nil {
  67. return err
  68. }
  69. var alias = collection.NewSet()
  70. for _, item := range proto.Message {
  71. alias.AddStr(fmt.Sprintf("%s = %s", parser.CamelCase(item.Name), fmt.Sprintf("%s.%s", proto.PbPackage, parser.CamelCase(item.Name))))
  72. }
  73. err = util.With("shared").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
  74. "name": callFilename,
  75. "alias": strings.Join(alias.KeysStr(), util.NL),
  76. "head": head,
  77. "filePackage": dir.Base,
  78. "package": fmt.Sprintf(`"%s"`, ctx.GetPb().Package),
  79. "serviceName": stringx.From(service.Name).ToCamel(),
  80. "functions": strings.Join(functions, util.NL),
  81. "interface": strings.Join(iFunctions, util.NL),
  82. }, filename, true)
  83. return err
  84. }
  85. func (g *defaultGenerator) genFunction(goPackage string, service parser.Service) ([]string, error) {
  86. functions := make([]string, 0)
  87. for _, rpc := range service.RPC {
  88. text, err := util.LoadTemplate(category, callFunctionTemplateFile, callFunctionTemplate)
  89. if err != nil {
  90. return nil, err
  91. }
  92. comment := parser.GetComment(rpc.Doc())
  93. buffer, err := util.With("sharedFn").Parse(text).Execute(map[string]interface{}{
  94. "serviceName": stringx.From(service.Name).ToCamel(),
  95. "rpcServiceName": parser.CamelCase(service.Name),
  96. "method": parser.CamelCase(rpc.Name),
  97. "package": goPackage,
  98. "pbRequest": parser.CamelCase(rpc.RequestType),
  99. "pbResponse": parser.CamelCase(rpc.ReturnsType),
  100. "hasComment": len(comment) > 0,
  101. "comment": comment,
  102. })
  103. if err != nil {
  104. return nil, err
  105. }
  106. functions = append(functions, buffer.String())
  107. }
  108. return functions, nil
  109. }
  110. func (g *defaultGenerator) getInterfaceFuncs(service parser.Service) ([]string, error) {
  111. functions := make([]string, 0)
  112. for _, rpc := range service.RPC {
  113. text, err := util.LoadTemplate(category, callInterfaceFunctionTemplateFile, callInterfaceFunctionTemplate)
  114. if err != nil {
  115. return nil, err
  116. }
  117. comment := parser.GetComment(rpc.Doc())
  118. buffer, err := util.With("interfaceFn").Parse(text).Execute(
  119. map[string]interface{}{
  120. "hasComment": len(comment) > 0,
  121. "comment": comment,
  122. "method": parser.CamelCase(rpc.Name),
  123. "pbRequest": parser.CamelCase(rpc.RequestType),
  124. "pbResponse": parser.CamelCase(rpc.ReturnsType),
  125. })
  126. if err != nil {
  127. return nil, err
  128. }
  129. functions = append(functions, buffer.String())
  130. }
  131. return functions, nil
  132. }