Skip to content

Commit

Permalink
feature: 插件enter回滚当数据为空时候删除import
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelmaxQm committed Jul 23, 2024
1 parent a561e7d commit ca4118c
Showing 1 changed file with 41 additions and 41 deletions.
82 changes: 41 additions & 41 deletions server/utils/ast/plugin_enter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"go/ast"
"go/token"
"io"
"log"
)

// PluginEnter 插件化入口
Expand Down Expand Up @@ -37,51 +36,51 @@ func (a *PluginEnter) Parse(filename string, writer io.Writer) (file *ast.File,
}

func (a *PluginEnter) Rollback(file *ast.File) error {
for i := 0; i < len(file.Decls); i++ {
v1, o1 := file.Decls[i].(*ast.GenDecl)
if o1 {
for j := 0; j < len(v1.Specs); j++ {
v2, o2 := v1.Specs[j].(*ast.ValueSpec)
if o2 {
for k := 0; k < len(v2.Values); k++ {
v3, o3 := v2.Values[k].(*ast.CallExpr)
if o3 {
for l := 0; l < len(v3.Args); l++ {
v4, o4 := v3.Args[l].(*ast.Ident)
if o4 {
v5, o5 := v4.Obj.Decl.(*ast.TypeSpec)
if o5 {
if v5.Name.Name != a.Type.Group() {
break
}
v6, o6 := v5.Type.(*ast.StructType)
if o6 {
for m := 0; m < len(v6.Fields.List); m++ {
v7, o7 := v6.Fields.List[m].Type.(*ast.Ident)
if len(v6.Fields.List[m].Names) >= 1 && v6.Fields.List[m].Names[0].Name == a.StructName && o7 && v7.Name == a.StructCamelName {
v6.Fields.List = append(v6.Fields.List[:m], v6.Fields.List[m+1:]...)
break
}
}
}
}
}
}
}
}
if a.Type == TypePluginServiceEnter {
continue
//回滚结构体内内容
var structType *ast.StructType
ast.Inspect(file, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.TypeSpec:
if s, ok := x.Type.(*ast.StructType); ok {
structType = s
for i, field := range x.Type.(*ast.StructType).Fields.List {
if len(field.Names) > 0 && field.Names[0].Name == a.StructName {
s.Fields.List = append(s.Fields.List[:i], s.Fields.List[i+1:]...)
return false
}
if len(v2.Names) >= 1 && v2.Names[0].Name == a.ModuleName {
v1.Specs = append(v1.Specs[:j], v1.Specs[j+1:]...)
if len(v1.Specs) <= 1 {
_ = NewImport(a.ImportPath).Rollback(file)
}
}
}
return true
})

if len(structType.Fields.List) == 0 {
_ = NewImport(a.ImportPath).Rollback(file)
}

if a.Type == TypePluginServiceEnter {
return nil
}

//回滚变量内容
ast.Inspect(file, func(n ast.Node) bool {
genDecl, ok := n.(*ast.GenDecl)
if ok && genDecl.Tok == token.VAR {
for i, spec := range genDecl.Specs {
valueSpec, vsok := spec.(*ast.ValueSpec)
if vsok {
for _, name := range valueSpec.Names {
if name.Name == a.ModuleName {
genDecl.Specs = append(genDecl.Specs[:i], genDecl.Specs[i+1:]...)
return false
}
}
}
}
}
}
return true
})

return nil
}

Expand All @@ -92,6 +91,7 @@ func (a *PluginEnter) Injection(file *ast.File) error {
hasVar := false
var firstStruct *ast.StructType
var varSpec *ast.GenDecl
//寻找是否存在结构且定位
ast.Inspect(file, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.TypeSpec:
Expand Down Expand Up @@ -120,10 +120,10 @@ func (a *PluginEnter) Injection(file *ast.File) error {
return nil
}

//寻找是否存在变量且定位
ast.Inspect(file, func(n ast.Node) bool {
genDecl, ok := n.(*ast.GenDecl)
if ok && genDecl.Tok == token.VAR {
log.Println("Found var declaration block")
for _, spec := range genDecl.Specs {
valueSpec, vsok := spec.(*ast.ValueSpec)
if vsok {
Expand Down

0 comments on commit ca4118c

Please sign in to comment.