2
0

plugin_initialize_gorm.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package ast
  2. import (
  3. "go/ast"
  4. "io"
  5. )
  6. type PluginInitializeGorm struct {
  7. Base
  8. Type Type // 类型
  9. Path string // 文件路径
  10. ImportPath string // 导包路径
  11. RelativePath string // 相对路径
  12. StructName string // 结构体名称
  13. PackageName string // 包名
  14. IsNew bool // 是否使用new关键字 true: new(PackageName.StructName) false: &PackageName.StructName{}
  15. }
  16. func (a *PluginInitializeGorm) Parse(filename string, writer io.Writer) (file *ast.File, err error) {
  17. if filename == "" {
  18. if a.RelativePath == "" {
  19. filename = a.Path
  20. a.RelativePath = a.Base.RelativePath(a.Path)
  21. return a.Base.Parse(filename, writer)
  22. }
  23. a.Path = a.Base.AbsolutePath(a.RelativePath)
  24. filename = a.Path
  25. }
  26. return a.Base.Parse(filename, writer)
  27. }
  28. func (a *PluginInitializeGorm) Rollback(file *ast.File) error {
  29. var needRollBackImport bool
  30. ast.Inspect(file, func(n ast.Node) bool {
  31. callExpr, ok := n.(*ast.CallExpr)
  32. if !ok {
  33. return true
  34. }
  35. selExpr, seok := callExpr.Fun.(*ast.SelectorExpr)
  36. if !seok || selExpr.Sel.Name != "AutoMigrate" {
  37. return true
  38. }
  39. if len(callExpr.Args) <= 1 {
  40. needRollBackImport = true
  41. }
  42. // 删除指定的参数
  43. for i, arg := range callExpr.Args {
  44. compLit, cok := arg.(*ast.CompositeLit)
  45. if !cok {
  46. continue
  47. }
  48. cselExpr, sok := compLit.Type.(*ast.SelectorExpr)
  49. if !sok {
  50. continue
  51. }
  52. ident, idok := cselExpr.X.(*ast.Ident)
  53. if idok && ident.Name == a.PackageName && cselExpr.Sel.Name == a.StructName {
  54. // 删除参数
  55. callExpr.Args = append(callExpr.Args[:i], callExpr.Args[i+1:]...)
  56. break
  57. }
  58. }
  59. return true
  60. })
  61. if needRollBackImport {
  62. _ = NewImport(a.ImportPath).Rollback(file)
  63. }
  64. return nil
  65. }
  66. func (a *PluginInitializeGorm) Injection(file *ast.File) error {
  67. _ = NewImport(a.ImportPath).Injection(file)
  68. var call *ast.CallExpr
  69. ast.Inspect(file, func(n ast.Node) bool {
  70. callExpr, ok := n.(*ast.CallExpr)
  71. if !ok {
  72. return true
  73. }
  74. selExpr, ok := callExpr.Fun.(*ast.SelectorExpr)
  75. if ok && selExpr.Sel.Name == "AutoMigrate" {
  76. call = callExpr
  77. return false
  78. }
  79. return true
  80. })
  81. arg := &ast.CompositeLit{
  82. Type: &ast.SelectorExpr{
  83. X: &ast.Ident{Name: a.PackageName},
  84. Sel: &ast.Ident{Name: a.StructName},
  85. },
  86. }
  87. call.Args = append(call.Args, arg)
  88. return nil
  89. }
  90. func (a *PluginInitializeGorm) Format(filename string, writer io.Writer, file *ast.File) error {
  91. if filename == "" {
  92. filename = a.Path
  93. }
  94. return a.Base.Format(filename, writer, file)
  95. }