| 36 | } |
| 37 | |
| 38 | func (a *PluginEnter) Rollback(file *ast.File) error { |
| 39 | //回滚结构体内内容 |
| 40 | var structType *ast.StructType |
| 41 | ast.Inspect(file, func(n ast.Node) bool { |
| 42 | switch x := n.(type) { |
| 43 | case *ast.TypeSpec: |
| 44 | if s, ok := x.Type.(*ast.StructType); ok { |
| 45 | structType = s |
| 46 | for i, field := range x.Type.(*ast.StructType).Fields.List { |
| 47 | if len(field.Names) > 0 && field.Names[0].Name == a.StructName { |
| 48 | s.Fields.List = append(s.Fields.List[:i], s.Fields.List[i+1:]...) |
| 49 | return false |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | return true |
| 55 | }) |
| 56 | |
| 57 | if len(structType.Fields.List) == 0 { |
| 58 | _ = NewImport(a.ImportPath).Rollback(file) |
| 59 | } |
| 60 | |
| 61 | if a.Type == TypePluginServiceEnter { |
| 62 | return nil |
| 63 | } |
| 64 | |
| 65 | //回滚变量内容 |
| 66 | ast.Inspect(file, func(n ast.Node) bool { |
| 67 | genDecl, ok := n.(*ast.GenDecl) |
| 68 | if ok && genDecl.Tok == token.VAR { |
| 69 | for i, spec := range genDecl.Specs { |
| 70 | valueSpec, vsok := spec.(*ast.ValueSpec) |
| 71 | if vsok { |
| 72 | for _, name := range valueSpec.Names { |
| 73 | if name.Name == a.ModuleName { |
| 74 | genDecl.Specs = append(genDecl.Specs[:i], genDecl.Specs[i+1:]...) |
| 75 | return false |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | return true |
| 82 | }) |
| 83 | |
| 84 | return nil |
| 85 | } |
| 86 | |
| 87 | func (a *PluginEnter) Injection(file *ast.File) error { |
| 88 | _ = NewImport(a.ImportPath).Injection(file) |