-
-
Notifications
You must be signed in to change notification settings - Fork 437
/
Copy pathwith_context.go
45 lines (41 loc) · 923 Bytes
/
with_context.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
package patcher
import (
"reflect"
"github.com/expr-lang/expr/ast"
)
// WithContext adds WithContext.Name argument to all functions calls with a context.Context argument.
type WithContext struct {
Name string
}
// Visit adds WithContext.Name argument to all functions calls with a context.Context argument.
func (w WithContext) Visit(node *ast.Node) {
switch call := (*node).(type) {
case *ast.CallNode:
fn := call.Callee.Type()
if fn == nil {
return
}
if fn.Kind() != reflect.Func {
return
}
switch fn.NumIn() {
case 0:
return
case 1:
if fn.In(0).String() != "context.Context" {
return
}
default:
if fn.In(0).String() != "context.Context" &&
fn.In(1).String() != "context.Context" {
return
}
}
ast.Patch(node, &ast.CallNode{
Callee: call.Callee,
Arguments: append([]ast.Node{
&ast.IdentifierNode{Value: w.Name},
}, call.Arguments...),
})
}
}