package_initialize_router.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package ast
  2. import (
  3. "fmt"
  4. "go/ast"
  5. "go/token"
  6. "io"
  7. )
  8. // PackageInitializeRouter 包初始化路由
  9. // ModuleName := PackageName.AppName.GroupName
  10. // ModuleName.FunctionName(RouterGroupName)
  11. type PackageInitializeRouter struct {
  12. Base
  13. Type Type // 类型
  14. Path string // 文件路径
  15. ImportPath string // 导包路径
  16. RelativePath string // 相对路径
  17. AppName string // 应用名称
  18. GroupName string // 分组名称
  19. ModuleName string // 模块名称
  20. PackageName string // 包名
  21. FunctionName string // 函数名
  22. RouterGroupName string // 路由分组名称
  23. LeftRouterGroupName string // 左路由分组名称
  24. RightRouterGroupName string // 右路由分组名称
  25. }
  26. func (a *PackageInitializeRouter) Parse(filename string, writer io.Writer) (file *ast.File, err error) {
  27. if filename == "" {
  28. if a.RelativePath == "" {
  29. filename = a.Path
  30. a.RelativePath = a.Base.RelativePath(a.Path)
  31. return a.Base.Parse(filename, writer)
  32. }
  33. a.Path = a.Base.AbsolutePath(a.RelativePath)
  34. filename = a.Path
  35. }
  36. return a.Base.Parse(filename, writer)
  37. }
  38. func (a *PackageInitializeRouter) Rollback(file *ast.File) error {
  39. funcDecl := FindFunction(file, "initBizRouter")
  40. exprNum := 0
  41. for i := range funcDecl.Body.List {
  42. if IsBlockStmt(funcDecl.Body.List[i]) {
  43. if VariableExistsInBlock(funcDecl.Body.List[i].(*ast.BlockStmt), a.ModuleName) {
  44. for ii, stmt := range funcDecl.Body.List[i].(*ast.BlockStmt).List {
  45. // 检查语句是否为 *ast.ExprStmt
  46. exprStmt, ok := stmt.(*ast.ExprStmt)
  47. if !ok {
  48. continue
  49. }
  50. // 检查表达式是否为 *ast.CallExpr
  51. callExpr, ok := exprStmt.X.(*ast.CallExpr)
  52. if !ok {
  53. continue
  54. }
  55. // 检查是否调用了我们正在寻找的函数
  56. selExpr, ok := callExpr.Fun.(*ast.SelectorExpr)
  57. if !ok {
  58. continue
  59. }
  60. // 检查调用的函数是否为 systemRouter.InitApiRouter
  61. ident, ok := selExpr.X.(*ast.Ident)
  62. //只要存在调用则+1
  63. if ok && ident.Name == a.ModuleName {
  64. exprNum++
  65. }
  66. //判断是否为目标结构
  67. if !ok || ident.Name != a.ModuleName || selExpr.Sel.Name != a.FunctionName {
  68. continue
  69. }
  70. exprNum--
  71. // 从语句列表中移除。
  72. funcDecl.Body.List[i].(*ast.BlockStmt).List = append(funcDecl.Body.List[i].(*ast.BlockStmt).List[:ii], funcDecl.Body.List[i].(*ast.BlockStmt).List[ii+1:]...)
  73. // 如果不再存在任何调用,则删除导入和变量。
  74. if exprNum == 0 {
  75. funcDecl.Body.List = append(funcDecl.Body.List[:i], funcDecl.Body.List[i+1:]...)
  76. }
  77. break
  78. }
  79. break
  80. }
  81. }
  82. }
  83. return nil
  84. }
  85. func (a *PackageInitializeRouter) Injection(file *ast.File) error {
  86. funcDecl := FindFunction(file, "initBizRouter")
  87. hasRouter := false
  88. var varBlock *ast.BlockStmt
  89. for i := range funcDecl.Body.List {
  90. if IsBlockStmt(funcDecl.Body.List[i]) {
  91. if VariableExistsInBlock(funcDecl.Body.List[i].(*ast.BlockStmt), a.ModuleName) {
  92. hasRouter = true
  93. varBlock = funcDecl.Body.List[i].(*ast.BlockStmt)
  94. break
  95. }
  96. }
  97. }
  98. if !hasRouter {
  99. stmt := a.CreateAssignStmt()
  100. varBlock = &ast.BlockStmt{
  101. List: []ast.Stmt{
  102. stmt,
  103. },
  104. }
  105. }
  106. routerStmt := CreateStmt(fmt.Sprintf("%s.%s(%s,%s)", a.ModuleName, a.FunctionName, a.LeftRouterGroupName, a.RightRouterGroupName))
  107. varBlock.List = append(varBlock.List, routerStmt)
  108. if !hasRouter {
  109. funcDecl.Body.List = append(funcDecl.Body.List, varBlock)
  110. }
  111. return nil
  112. }
  113. func (a *PackageInitializeRouter) Format(filename string, writer io.Writer, file *ast.File) error {
  114. if filename == "" {
  115. filename = a.Path
  116. }
  117. return a.Base.Format(filename, writer, file)
  118. }
  119. func (a *PackageInitializeRouter) CreateAssignStmt() *ast.AssignStmt {
  120. //创建左侧变量
  121. ident := &ast.Ident{
  122. Name: a.ModuleName,
  123. }
  124. //创建右侧的赋值语句
  125. selector := &ast.SelectorExpr{
  126. X: &ast.SelectorExpr{
  127. X: &ast.Ident{Name: a.PackageName},
  128. Sel: &ast.Ident{Name: a.AppName},
  129. },
  130. Sel: &ast.Ident{Name: a.GroupName},
  131. }
  132. // 创建一个组合的赋值语句
  133. stmt := &ast.AssignStmt{
  134. Lhs: []ast.Expr{ident},
  135. Tok: token.DEFINE,
  136. Rhs: []ast.Expr{selector},
  137. }
  138. return stmt
  139. }