-
Notifications
You must be signed in to change notification settings - Fork 0
/
rewrite.go
213 lines (187 loc) · 4.69 KB
/
rewrite.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package astTool
import (
"errors"
"go/ast"
"go/token"
)
const (
HEAD = 0
TAIL = -1
)
/*
替换节点
*/
func (h *HappyAst) ReplaceNode(pos token.Pos, newNode ast.Node) error {
if pos == token.NoPos {
return errors.New("pos not exist")
}
ast.Inspect(h.Ast, func(node ast.Node) bool {
switch node.(type) {
case ast.Node:
if !node.Pos().IsValid() {
return true
}
if node.Pos() == pos {
node = newNode
return false
}
}
return true
})
return nil
}
/*
添加语句
@deprecated
*/
func (h *HappyAst) AddStmt(bmt *ast.BlockStmt, location int, stmt ast.Stmt) {
switch location {
case HEAD:
tempStmtList := make([]ast.Stmt, 0)
tempStmtList = append(tempStmtList, stmt)
for _, v := range bmt.List {
tempStmtList = append(tempStmtList, v)
}
bmt.List = tempStmtList
case TAIL:
tempStmtList := make([]ast.Stmt, 0)
for _, v := range bmt.List {
tempStmtList = append(tempStmtList, v)
}
tempStmtList = append(tempStmtList, stmt)
bmt.List = tempStmtList
default:
tempStmtList := make([]ast.Stmt, 0)
tempStmtList = append(tempStmtList, bmt.List[:location]...)
tempStmtList = append(tempStmtList, stmt)
tempStmtList = append(tempStmtList, bmt.List[location:]...)
bmt.List = tempStmtList
}
}
/*
添加赋值语句
bmt 语句块节点
location 在bmt.List数组中的位置
stmt 待放置的赋值语句节点
*/
func (h *HappyAst) AddAssignStmt(bmt *ast.BlockStmt, location int, stmt ast.Stmt) {
tempStmtList := make([]ast.Stmt, 0)
switch location {
case HEAD:
tempStmtList = append(tempStmtList, stmt)
tempStmtList = append(tempStmtList, bmt.List[:]...)
case TAIL:
tempStmtList = append(tempStmtList, bmt.List[:]...)
tempStmtList = append(tempStmtList, stmt)
default:
tempStmtList = append(tempStmtList, bmt.List[:location]...)
tempStmtList = append(tempStmtList, stmt)
tempStmtList = append(tempStmtList, bmt.List[location:]...)
}
bmt.List = tempStmtList
}
/*
添加函数声明
location 在root.Decls数组中的位置
stmt 待放置的赋值语句节点
*/
func (h *HappyAst) AppendFundDecl(location int, decl ast.Decl) {
tempDeclList := make([]ast.Decl, 0)
switch location {
case HEAD:
tempDeclList = append(tempDeclList, decl)
tempDeclList = append(tempDeclList, h.Ast.Decls[:]...)
case TAIL:
tempDeclList = append(tempDeclList, h.Ast.Decls[:]...)
tempDeclList = append(tempDeclList, decl)
default:
tempDeclList = append(tempDeclList, h.Ast.Decls[:location]...)
tempDeclList = append(tempDeclList, decl)
tempDeclList = append(tempDeclList, h.Ast.Decls[location:]...)
}
h.Ast.Decls = tempDeclList
}
/*
添加声明
@deprecated
*/
func (h *HappyAst) AddDecl(location int, appends ...ast.Decl) {
tempDecls := make([]ast.Decl, 0)
switch location {
case HEAD:
for _, v := range appends {
tempDecls = append(tempDecls, v)
}
for _, val := range h.Ast.Decls {
tempDecls = append(tempDecls, val)
}
case TAIL:
tempDecls = h.Ast.Decls
for _, v := range appends {
tempDecls = append(tempDecls, v)
}
default:
tempDecls = h.Ast.Decls
for _, v := range appends {
tempDecls = append(tempDecls, v)
}
}
h.Ast.Decls = tempDecls
}
/*
type svc interface {
UserGet()
}
=>
type svc interface {
UserGet()
RoleGet()
}
*/
func (h *HappyAst) AddFieldOfFuncType(bmt *ast.FieldList, location int, field *ast.Field) {
tempFieldList := make([]*ast.Field, 0)
switch location {
case HEAD:
tempFieldList = append(tempFieldList, field)
tempFieldList = append(tempFieldList, bmt.List[:]...)
case TAIL:
tempFieldList = append(tempFieldList, bmt.List[:]...)
tempFieldList = append(tempFieldList, field)
default:
tempFieldList = append(tempFieldList, bmt.List[:location]...)
tempFieldList = append(tempFieldList, field)
tempFieldList = append(tempFieldList, bmt.List[location:]...)
}
bmt.List = tempFieldList
}
/*
参数:
bmt,被插入的fieldList
location,在bmt中的顺序. -1尾部添加 0头部添加
field,插入的field
示例:
type PartnerSvcEndpoints struct {
modelFetchEndpoint kitendpoint.Endpoint
}
=>
type PartnerSvcEndpoints struct {
modelFetchEndpoint kitendpoint.Endpoint
gameFetchEndpoint kitendpoint.Endpoint
}
*/
func (h *HappyAst) AddField(bmt *ast.FieldList, location int, field *ast.Field) {
tempFieldList := make([]*ast.Field, 0)
switch location {
case HEAD:
tempFieldList = append(tempFieldList, field)
tempFieldList = append(tempFieldList, bmt.List[:]...)
case TAIL:
tempFieldList = append(tempFieldList, bmt.List[:]...)
tempFieldList = append(tempFieldList, field)
default:
tempFieldList = append(tempFieldList, bmt.List[:location]...)
tempFieldList = append(tempFieldList, field)
tempFieldList = append(tempFieldList, bmt.List[location:]...)
}
bmt.List = tempFieldList
}