genserver.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package generator
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strings"
  6. "github.com/zeromicro/go-zero/core/collection"
  7. conf "github.com/zeromicro/go-zero/tools/goctl/config"
  8. "github.com/zeromicro/go-zero/tools/goctl/rpc/parser"
  9. "github.com/zeromicro/go-zero/tools/goctl/util"
  10. "github.com/zeromicro/go-zero/tools/goctl/util/format"
  11. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  12. "github.com/zeromicro/go-zero/tools/goctl/util/stringx"
  13. )
  14. const (
  15. serverTemplate = `{{.head}}
  16. package server
  17. import (
  18. {{if .notStream}}"context"{{end}}
  19. {{.imports}}
  20. )
  21. type {{.server}}Server struct {
  22. svcCtx *svc.ServiceContext
  23. {{.unimplementedServer}}
  24. }
  25. func New{{.server}}Server(svcCtx *svc.ServiceContext) *{{.server}}Server {
  26. return &{{.server}}Server{
  27. svcCtx: svcCtx,
  28. }
  29. }
  30. {{.funcs}}
  31. `
  32. functionTemplate = `
  33. {{if .hasComment}}{{.comment}}{{end}}
  34. func (s *{{.server}}Server) {{.method}} ({{if .notStream}}ctx context.Context,{{if .hasReq}} in {{.request}}{{end}}{{else}}{{if .hasReq}} in {{.request}},{{end}}stream {{.streamBody}}{{end}}) ({{if .notStream}}{{.response}},{{end}}error) {
  35. l := logic.New{{.logicName}}({{if .notStream}}ctx,{{else}}stream.Context(),{{end}}s.svcCtx)
  36. return l.{{.method}}({{if .hasReq}}in{{if .stream}} ,stream{{end}}{{else}}{{if .stream}}stream{{end}}{{end}})
  37. }
  38. `
  39. )
  40. // GenServer generates rpc server file, which is an implementation of rpc server
  41. func (g *Generator) GenServer(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
  42. dir := ctx.GetServer()
  43. logicImport := fmt.Sprintf(`"%v"`, ctx.GetLogic().Package)
  44. svcImport := fmt.Sprintf(`"%v"`, ctx.GetSvc().Package)
  45. pbImport := fmt.Sprintf(`"%v"`, ctx.GetPb().Package)
  46. imports := collection.NewSet()
  47. imports.AddStr(logicImport, svcImport, pbImport)
  48. head := util.GetHead(proto.Name)
  49. service := proto.Service
  50. serverFilename, err := format.FileNamingFormat(cfg.NamingFormat, service.Name+"_server")
  51. if err != nil {
  52. return err
  53. }
  54. serverFile := filepath.Join(dir.Filename, serverFilename+".go")
  55. funcList, err := g.genFunctions(proto.PbPackage, service)
  56. if err != nil {
  57. return err
  58. }
  59. text, err := pathx.LoadTemplate(category, serverTemplateFile, serverTemplate)
  60. if err != nil {
  61. return err
  62. }
  63. notStream := false
  64. for _, rpc := range service.RPC {
  65. if !rpc.StreamsRequest && !rpc.StreamsReturns {
  66. notStream = true
  67. break
  68. }
  69. }
  70. err = util.With("server").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
  71. "head": head,
  72. "unimplementedServer": fmt.Sprintf("%s.Unimplemented%sServer", proto.PbPackage, stringx.From(service.Name).ToCamel()),
  73. "server": stringx.From(service.Name).ToCamel(),
  74. "imports": strings.Join(imports.KeysStr(), pathx.NL),
  75. "funcs": strings.Join(funcList, pathx.NL),
  76. "notStream": notStream,
  77. }, serverFile, true)
  78. return err
  79. }
  80. func (g *Generator) genFunctions(goPackage string, service parser.Service) ([]string, error) {
  81. var functionList []string
  82. for _, rpc := range service.RPC {
  83. text, err := pathx.LoadTemplate(category, serverFuncTemplateFile, functionTemplate)
  84. if err != nil {
  85. return nil, err
  86. }
  87. comment := parser.GetComment(rpc.Doc())
  88. streamServer := fmt.Sprintf("%s.%s_%s%s", goPackage, parser.CamelCase(service.Name), parser.CamelCase(rpc.Name), "Server")
  89. buffer, err := util.With("func").Parse(text).Execute(map[string]interface{}{
  90. "server": stringx.From(service.Name).ToCamel(),
  91. "logicName": fmt.Sprintf("%sLogic", stringx.From(rpc.Name).ToCamel()),
  92. "method": parser.CamelCase(rpc.Name),
  93. "request": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.RequestType)),
  94. "response": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.ReturnsType)),
  95. "hasComment": len(comment) > 0,
  96. "comment": comment,
  97. "hasReq": !rpc.StreamsRequest,
  98. "stream": rpc.StreamsRequest || rpc.StreamsReturns,
  99. "notStream": !rpc.StreamsRequest && !rpc.StreamsReturns,
  100. "streamBody": streamServer,
  101. })
  102. if err != nil {
  103. return nil, err
  104. }
  105. functionList = append(functionList, buffer.String())
  106. }
  107. return functionList, nil
  108. }