-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprocess.go
267 lines (241 loc) · 6.42 KB
/
process.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package nrseg
import (
"bytes"
"errors"
"go/ast"
"go/format"
"go/parser"
"go/token"
"regexp"
"strconv"
"strings"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/imports"
)
func Process(filename string, src []byte) ([]byte, error) {
if len(src) != 0 && c.Match(src) {
return src, nil
}
fs := token.NewFileSet()
f, err := parser.ParseFile(fs, filename, src, parser.ParseComments)
if err != nil {
return nil, err
}
// import newrelic pkg
pkg := "newrelic"
name, err := addImport(fs, f) // importされたpkgの名前
if err != nil {
return nil, err
}
if len(name) != 0 {
// change name if named import.
pkg = name
}
ast.Inspect(f, func(n ast.Node) bool {
if fd, ok := n.(*ast.FuncDecl); ok {
if findIgnoreComment(fd.Doc) {
return false
}
if fd.Body != nil && len(fd.Body.List) > 0 {
sn := getSegName(fd)
vn, t := parseParams(f.Imports, fd.Type)
var ds ast.Stmt
switch t {
case TypeContext:
ds = buildDeferStmt(fd.Body.Lbrace, pkg, vn, sn)
case TypeHttpRequest:
ds = buildDeferStmtWithHttpRequest(fd.Body.Lbrace, pkg, vn, sn)
case TypeUnknown:
return false
}
if !existFromContext(pkg, fd.Body.List[0]) {
fd.Body.List = append([]ast.Stmt{ds}, fd.Body.List...)
}
return false
}
}
return true
})
// gofmt
var fmtedBuf bytes.Buffer
if err := format.Node(&fmtedBuf, fs, f); err != nil {
return nil, err
}
// goimports
igot, err := imports.Process(filename, fmtedBuf.Bytes(), nil)
if err != nil {
return nil, err
}
return igot, nil
}
const NewRelicV3Pkg = "github.com/newrelic/go-agent/v3/newrelic"
func addImport(fs *token.FileSet, f *ast.File) (string, error) {
pkg, err := findImport(fs, f)
if err == nil {
return pkg, nil
}
if errors.Is(err, ErrNoImportNrPkg) {
astutil.AddImport(fs, f, NewRelicV3Pkg)
return "", nil
}
return "", err
}
var ErrNoImportNrPkg = errors.New("not import newrelic pkg")
func findImport(fs *token.FileSet, f *ast.File) (string, error) {
for _, spec := range f.Imports {
path, err := strconv.Unquote(spec.Path.Value)
if err != nil {
return "", err
}
// import already.
if path == NewRelicV3Pkg {
if spec.Name != nil {
return spec.Name.Name, nil
}
return "", nil
}
}
return "", ErrNoImportNrPkg
}
var nrignoreReg = regexp.MustCompile("(?m)^// nrseg:ignore .*$")
func findIgnoreComment(cg *ast.CommentGroup) bool {
if cg == nil {
return false
}
for _, c := range cg.List {
if nrignoreReg.MatchString(c.Text) {
return true
}
}
return false
}
func existFromContext(pn string, s ast.Stmt) bool {
var result bool
ast.Inspect(s, func(n ast.Node) bool {
if se, ok := n.(*ast.SelectorExpr); ok {
if idt, ok := se.X.(*ast.Ident); ok && idt.Name == pn && se.Sel.Name == "FromContext" {
result = true
return false
}
}
return true
})
return result
}
// buildDeferStmt builds the defer statement with args.
// ex:
// defer newrelic.FromContext(ctx).StartSegment("slow").End()
func buildDeferStmt(pos token.Pos, pkgName, ctxName, segName string) *ast.DeferStmt {
arg := &ast.Ident{NamePos: pos, Name: ctxName}
return skeletonDeferStmt(pos, arg, pkgName, segName)
}
// buildDeferStmt builds the defer statement with *http.Request.
// ex:
// defer newrelic.FromContext(req.Context()).StartSegment("slow").End()
func buildDeferStmtWithHttpRequest(pos token.Pos, pkgName, reqName, segName string) *ast.DeferStmt {
arg := &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.Ident{NamePos: pos, Name: reqName},
Sel: &ast.Ident{NamePos: pos, Name: "Context"},
},
Rparen: pos,
}
return skeletonDeferStmt(pos, arg, pkgName, segName)
}
func skeletonDeferStmt(pos token.Pos, fcArg ast.Expr, pkgName, segName string) *ast.DeferStmt {
return &ast.DeferStmt{
Call: &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.Ident{NamePos: pos, Name: pkgName},
Sel: &ast.Ident{NamePos: pos, Name: "FromContext"},
},
Lparen: pos,
Args: []ast.Expr{fcArg},
Rparen: pos,
},
Sel: &ast.Ident{NamePos: pos, Name: "StartSegment"},
},
Args: []ast.Expr{&ast.BasicLit{
ValuePos: pos,
Kind: token.STRING,
Value: strconv.Quote(segName),
}},
},
Sel: &ast.Ident{NamePos: pos, Name: "End"},
},
Rparen: pos,
},
}
}
func getSegName(fd *ast.FuncDecl) string {
var prefix string
if fd.Recv != nil && len(fd.Recv.List) > 0 {
if rn, ok := fd.Recv.List[0].Type.(*ast.StarExpr); ok {
if idt, ok := rn.X.(*ast.Ident); ok {
prefix = toSnake(idt.Name)
}
} else if idt, ok := fd.Recv.List[0].Type.(*ast.Ident); ok {
prefix = toSnake(idt.Name)
}
}
sn := toSnake(fd.Name.Name)
if len(prefix) != 0 {
sn = prefix + "_" + sn
}
return sn
}
// https://www.golangprograms.com/golang-convert-string-into-snake-case.html
var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
func toSnake(n string) string {
snake := matchFirstCap.ReplaceAllString(n, "${1}_${2}")
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
return strings.ToLower(snake)
}
const (
TypeContext = "context.Context"
TypeHttpRequest = "*http.Request"
TypeUnknown = "Unknown"
)
var types = map[string]string{
TypeContext: "\"context\"",
TypeHttpRequest: "\"net/http\"",
}
func parseParams(is []*ast.ImportSpec, t *ast.FuncType) (string, string) {
var cname = getImportName(is, TypeContext)
var hname = getImportName(is, TypeHttpRequest)
n, typ := "", TypeUnknown
for _, f := range t.Params.List {
if se, ok := f.Type.(*ast.SelectorExpr); ok {
if idt, ok := se.X.(*ast.Ident); ok && idt.Name == cname && se.Sel.Name == "Context" {
if len(f.Names) > 0 && len(f.Names[0].Name) > 0 {
return f.Names[0].Name, TypeContext
}
}
}
if se, ok := f.Type.(*ast.StarExpr); ok {
if se, ok := se.X.(*ast.SelectorExpr); ok {
if idt, ok := se.X.(*ast.Ident); ok && idt.Name == hname && se.Sel.Name == "Request" {
if len(f.Names) > 0 && len(f.Names[0].Name) > 0 {
n = f.Names[0].Name
typ = TypeHttpRequest
}
}
}
}
}
return n, typ
}
func getImportName(is []*ast.ImportSpec, typ string) string {
var def = strings.Replace(strings.Split(typ, ".")[0], "*", "", 1)
for _, i := range is {
if i.Name != nil && i.Path != nil && i.Path.Value == types[typ] {
return i.Name.Name
}
}
return def
}