2
0

plugin_initialize_router.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package ast
  2. import (
  3. "fmt"
  4. "go/ast"
  5. "io"
  6. )
  7. // PluginInitializeRouter 插件初始化路由
  8. // PackageName.AppName.GroupName.FunctionName()
  9. type PluginInitializeRouter struct {
  10. Base
  11. Type Type // 类型
  12. Path string // 文件路径
  13. ImportPath string // 导包路径
  14. ImportGlobalPath string // 导包全局变量路径
  15. ImportMiddlewarePath string // 导包中间件路径
  16. RelativePath string // 相对路径
  17. AppName string // 应用名称
  18. GroupName string // 分组名称
  19. PackageName string // 包名
  20. FunctionName string // 函数名
  21. LeftRouterGroupName string // 左路由分组名称
  22. RightRouterGroupName string // 右路由分组名称
  23. }
  24. func (a *PluginInitializeRouter) Parse(filename string, writer io.Writer) (file *ast.File, err error) {
  25. if filename == "" {
  26. if a.RelativePath == "" {
  27. filename = a.Path
  28. a.RelativePath = a.Base.RelativePath(a.Path)
  29. return a.Base.Parse(filename, writer)
  30. }
  31. a.Path = a.Base.AbsolutePath(a.RelativePath)
  32. filename = a.Path
  33. }
  34. return a.Base.Parse(filename, writer)
  35. }
  36. func (a *PluginInitializeRouter) Rollback(file *ast.File) error {
  37. funcDecl := FindFunction(file, "Router")
  38. delI := 0
  39. routerNum := 0
  40. for i := len(funcDecl.Body.List) - 1; i >= 0; i-- {
  41. stmt, ok := funcDecl.Body.List[i].(*ast.ExprStmt)
  42. if !ok {
  43. continue
  44. }
  45. callExpr, ok := stmt.X.(*ast.CallExpr)
  46. if !ok {
  47. continue
  48. }
  49. selExpr, ok := callExpr.Fun.(*ast.SelectorExpr)
  50. if !ok {
  51. continue
  52. }
  53. ident, ok := selExpr.X.(*ast.SelectorExpr)
  54. if ok {
  55. if iExpr, ieok := ident.X.(*ast.SelectorExpr); ieok {
  56. if iden, idok := iExpr.X.(*ast.Ident); idok {
  57. if iden.Name == "router" {
  58. routerNum++
  59. }
  60. }
  61. }
  62. if ident.Sel.Name == a.GroupName && selExpr.Sel.Name == a.FunctionName {
  63. // 删除语句
  64. delI = i
  65. }
  66. }
  67. }
  68. funcDecl.Body.List = append(funcDecl.Body.List[:delI], funcDecl.Body.List[delI+1:]...)
  69. if routerNum <= 1 {
  70. _ = NewImport(a.ImportPath).Rollback(file)
  71. }
  72. return nil
  73. }
  74. func (a *PluginInitializeRouter) Injection(file *ast.File) error {
  75. _ = NewImport(a.ImportPath).Injection(file)
  76. funcDecl := FindFunction(file, "Router")
  77. var exists bool
  78. ast.Inspect(funcDecl, func(n ast.Node) bool {
  79. callExpr, ok := n.(*ast.CallExpr)
  80. if !ok {
  81. return true
  82. }
  83. selExpr, ok := callExpr.Fun.(*ast.SelectorExpr)
  84. if !ok {
  85. return true
  86. }
  87. ident, ok := selExpr.X.(*ast.SelectorExpr)
  88. if ok && ident.Sel.Name == a.GroupName && selExpr.Sel.Name == a.FunctionName {
  89. exists = true
  90. return false
  91. }
  92. return true
  93. })
  94. if !exists {
  95. stmtStr := fmt.Sprintf("%s.%s.%s.%s(%s, %s)", a.PackageName, a.AppName, a.GroupName, a.FunctionName, a.LeftRouterGroupName, a.RightRouterGroupName)
  96. stmt := CreateStmt(stmtStr)
  97. funcDecl.Body.List = append(funcDecl.Body.List, stmt)
  98. }
  99. return nil
  100. }
  101. func (a *PluginInitializeRouter) Format(filename string, writer io.Writer, file *ast.File) error {
  102. if filename == "" {
  103. filename = a.Path
  104. }
  105. return a.Base.Format(filename, writer, file)
  106. }