-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunc.go
53 lines (48 loc) · 1.24 KB
/
func.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
package gop
import (
"fmt"
"reflect"
"bou.ke/monkey"
)
const funcLocation = "%s.%s"
// register func point
func RegisterFuncPoint(fn interface{}) {
var guard *monkey.PatchGuard
pointType := reflect.TypeOf(fn)
pkgPth := pointType.PkgPath()
location := fmt.Sprintf(funcLocation, pkgPth, pointType.Name())
putMatchAspect(location)
if len(getAspect(location)) < 1 {
return
}
var proxy = func(in []reflect.Value) []reflect.Value {
guard.Unpatch()
defer guard.Restore()
point := newFuncJoinPoint(reflect.ValueOf(fn), in)
defer finallyProcessed(point, location)
if !beforeProcessed(point, location) {
return point.Result
}
point.Result = point.Func.Call(in)
afterProcessed(point, location)
return point.Result
}
// dynamic create proxy function
proxyFn := reflect.MakeFunc(pointType, proxy)
// change original function call to proxy call
guard = monkey.Patch(fn, proxyFn.Interface())
}
// new func join point
func newFuncJoinPoint(fn reflect.Value, params []reflect.Value) *JoinPoint {
point := &JoinPoint{
Params: params,
Func: fn,
}
fnType := fn.Type()
nout := fnType.NumOut()
point.Result = make([]reflect.Value, nout)
for i := 0; i < nout; i++ {
point.Result[i] = reflect.Zero(fnType.Out(i))
}
return point
}