genserver.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. func (g *defaultGenerator) GenServer(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
  39. dir := ctx.GetServer()
  40. logicImport := fmt.Sprintf(`"%v"`, ctx.GetLogic().Package)
  41. svcImport := fmt.Sprintf(`"%v"`, ctx.GetSvc().Package)
  42. pbImport := fmt.Sprintf(`"%v"`, ctx.GetPb().Package)
  43. imports := collection.NewSet()
  44. imports.AddStr(logicImport, svcImport, pbImport)
  45. head := util.GetHead(proto.Name)
  46. service := proto.Service
  47. serverFilename, err := format.FileNamingFormat(cfg.NamingFormat, service.Name+"_server")
  48. if err != nil {
  49. return err
  50. }
  51. serverFile := filepath.Join(dir.Filename, serverFilename+".go")
  52. funcList, err := g.genFunctions(proto.PbPackage, service)
  53. if err != nil {
  54. return err
  55. }
  56. text, err := util.LoadTemplate(category, serverTemplateFile, serverTemplate)
  57. if err != nil {
  58. return err
  59. }
  60. err = util.With("server").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
  61. "head": head,
  62. "server": stringx.From(service.Name).ToCamel(),
  63. "imports": strings.Join(imports.KeysStr(), util.NL),
  64. "funcs": strings.Join(funcList, util.NL),
  65. }, serverFile, true)
  66. return err
  67. }
  68. func (g *defaultGenerator) genFunctions(goPackage string, service parser.Service) ([]string, error) {
  69. var functionList []string
  70. for _, rpc := range service.RPC {
  71. text, err := util.LoadTemplate(category, serverFuncTemplateFile, functionTemplate)
  72. if err != nil {
  73. return nil, err
  74. }
  75. comment := parser.GetComment(rpc.Doc())
  76. buffer, err := util.With("func").Parse(text).Execute(map[string]interface{}{
  77. "server": stringx.From(service.Name).ToCamel(),
  78. "logicName": fmt.Sprintf("%sLogic", stringx.From(rpc.Name).ToCamel()),
  79. "method": parser.CamelCase(rpc.Name),
  80. "request": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.RequestType)),
  81. "response": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.ReturnsType)),
  82. "hasComment": len(comment) > 0,
  83. "comment": comment,
  84. })
  85. if err != nil {
  86. return nil, err
  87. }
  88. functionList = append(functionList, buffer.String())
  89. }
  90. return functionList, nil
  91. }