genserver.go 3.7 KB

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