gencall.go 4.1 KB

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