-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconf_nodes.go
114 lines (101 loc) · 2.57 KB
/
conf_nodes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import "go/ast"
// setObjNil looks for the elements that can't be serialized and set it to nil.
// It has the properly signature to be a parameter of ast.Inspect function.
func setObjNil(node ast.Node) bool {
switch node.(type) {
case *ast.BranchStmt:
n := node.(*ast.BranchStmt)
safeIdent(n.Label)
case *ast.File:
n := node.(*ast.File)
safeImportSpecList(n.Imports)
safeIdent(n.Name)
safeScope(n.Scope)
safeIdentList(n.Unresolved)
case *ast.FuncDecl:
n := node.(*ast.FuncDecl)
safeIdent(n.Name)
safeFieldList(n.Recv)
safeFuncTye(n.Type)
case *ast.FuncLit:
n := node.(*ast.FuncLit)
safeFuncTye(n.Type)
case *ast.FuncType:
n := node.(*ast.FuncType)
safeFuncTye(n)
case *ast.Ident:
n := node.(*ast.Ident)
safeIdent(n)
case *ast.ImportSpec:
n := node.(*ast.ImportSpec)
safeImportSpec(n)
case *ast.InterfaceType:
n := node.(*ast.InterfaceType)
safeFieldList(n.Methods)
case *ast.LabeledStmt:
n := node.(*ast.LabeledStmt)
safeIdent(n.Label)
case *ast.Package:
n := node.(*ast.Package)
n.Files = nil
n.Imports = nil
safeScope(n.Scope)
case *ast.SelectorExpr:
n := node.(*ast.SelectorExpr)
safeIdent(n.Sel)
case *ast.StructType:
n := node.(*ast.StructType)
safeFieldList(n.Fields)
case *ast.TypeSpec:
n := node.(*ast.TypeSpec)
safeIdent(n.Name)
}
return true
}
// safeIdent set to nil conflictives fields of a ast.Ident.
func safeIdent(node *ast.Ident) {
if node == nil {
return
}
node.Obj = nil
}
// safeIdentList iterates over a slice of ast.Ident and calls safeIdent.
func safeIdentList(list []*ast.Ident) {
for i := range list {
safeIdent(list[i])
}
}
// safeImportSpec set to nil conflictives fields of a ast.ImportSpect.
func safeImportSpec(is *ast.ImportSpec) {
safeIdent(is.Name)
}
// safeImportSpecList iterates over a slice of ast.ImportSpec and calls safeImportSpec.
func safeImportSpecList(list []*ast.ImportSpec) {
for i := range list {
safeImportSpec(list[i])
}
}
// safeField set to nil conflictives fields of a ast.Field.
func safeField(field *ast.Field) {
safeIdentList(field.Names)
}
// safeFieldList iterates over a slice of ast.Field and calls safeField.
func safeFieldList(flist *ast.FieldList) {
if flist == nil {
return
}
for i := range flist.List {
safeField(flist.List[i])
}
}
// safeFuncTye set to nil conflictives fields of a ast.FuncType.
func safeFuncTye(ftype *ast.FuncType) {
safeFieldList(ftype.Params)
safeFieldList(ftype.Results)
}
// safeScope set to nil conflictives fields of a ast.Scope.
func safeScope(scope *ast.Scope) {
scope.Objects = nil
scope.Outer = nil
}