genserver.go 3.0 KB

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