MCPcopy
hub / github.com/flipped-aurora/gin-vue-admin / Rollback

Method Rollback

server/utils/ast/plugin_enter.go:38–85  ·  view source on GitHub ↗
(file *ast.File)

Source from the content-addressed store, hash-verified

36}
37
38func (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
87func (a *PluginEnter) Injection(file *ast.File) error {
88 _ = NewImport(a.ImportPath).Injection(file)

Callers 1

TestPluginEnter_RollbackFunction · 0.95

Calls 2

NewImportFunction · 0.85
RollbackMethod · 0.65

Tested by 1

TestPluginEnter_RollbackFunction · 0.76