genserver.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package gen
  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. )
  10. const (
  11. serverTemplate = `{{.head}}
  12. package server
  13. import (
  14. "context"
  15. {{.imports}}
  16. )
  17. type {{.types}}
  18. func New{{.server}}Server(svcCtx *svc.ServiceContext) *{{.server}}Server {
  19. return &{{.server}}Server{
  20. svcCtx: svcCtx,
  21. }
  22. }
  23. {{.funcs}}
  24. `
  25. functionTemplate = `
  26. {{if .hasComment}}{{.comment}}{{end}}
  27. func (s *{{.server}}Server) {{.method}} (ctx context.Context, in {{.request}}) ({{.response}}, error) {
  28. l := logic.New{{.logicName}}(ctx,s.svcCtx)
  29. return l.{{.method}}(in)
  30. }
  31. `
  32. typeFmt = `%sServer struct {
  33. svcCtx *svc.ServiceContext
  34. }`
  35. )
  36. func (g *defaultRpcGenerator) genHandler() error {
  37. serverPath := g.dirM[dirServer]
  38. file := g.ast
  39. logicImport := fmt.Sprintf(`"%v"`, g.mustGetPackage(dirLogic))
  40. svcImport := fmt.Sprintf(`"%v"`, g.mustGetPackage(dirSvc))
  41. imports := collection.NewSet()
  42. imports.AddStr(logicImport, svcImport)
  43. head := util.GetHead(g.Ctx.ProtoSource)
  44. for _, service := range file.Service {
  45. filename := fmt.Sprintf("%vserver.go", service.Name.Lower())
  46. serverFile := filepath.Join(serverPath, filename)
  47. funcList, importList, err := g.genFunctions(service)
  48. if err != nil {
  49. return err
  50. }
  51. imports.AddStr(importList...)
  52. err = util.With("server").GoFmt(true).Parse(serverTemplate).SaveTo(map[string]interface{}{
  53. "head": head,
  54. "types": fmt.Sprintf(typeFmt, service.Name.Title()),
  55. "server": service.Name.Title(),
  56. "imports": strings.Join(imports.KeysStr(), util.NL),
  57. "funcs": strings.Join(funcList, util.NL),
  58. }, serverFile, true)
  59. if err != nil {
  60. return err
  61. }
  62. }
  63. return nil
  64. }
  65. func (g *defaultRpcGenerator) genFunctions(service *parser.RpcService) ([]string, []string, error) {
  66. file := g.ast
  67. pkg := file.Package
  68. var functionList []string
  69. imports := collection.NewSet()
  70. for _, method := range service.Funcs {
  71. if method.ParameterIn.Package == pkg || method.ParameterOut.Package == pkg {
  72. imports.AddStr(fmt.Sprintf(`%v "%v"`, pkg, g.mustGetPackage(dirPb)))
  73. }
  74. imports.AddStr(g.ast.Imports[method.ParameterIn.Package])
  75. imports.AddStr(g.ast.Imports[method.ParameterOut.Package])
  76. buffer, err := util.With("func").Parse(functionTemplate).Execute(map[string]interface{}{
  77. "server": service.Name.Title(),
  78. "logicName": fmt.Sprintf("%sLogic", method.Name.Title()),
  79. "method": method.Name.Title(),
  80. "package": pkg,
  81. "request": method.ParameterIn.StarExpression,
  82. "response": method.ParameterOut.StarExpression,
  83. "hasComment": method.HaveDoc(),
  84. "comment": method.GetDoc(),
  85. })
  86. if err != nil {
  87. return nil, nil, err
  88. }
  89. functionList = append(functionList, buffer.String())
  90. }
  91. return functionList, imports.KeysStr(), nil
  92. }