@@ -44,6 +44,7 @@ func PatchMethod(target, redirection interface{}) (*Patch, error) {
44
44
}
45
45
return patch , nil
46
46
}
47
+
47
48
// Patches an instance func by using two parameters, the target struct type and the method name inside that type,
48
49
//this func will be redirected to the "redirection" func. Note: The first parameter of the redirection func must be the object instance.
49
50
func PatchInstanceMethodByName (target reflect.Type , methodName string , redirection interface {}) (* Patch , error ) {
@@ -55,11 +56,23 @@ func PatchInstanceMethodByName(target reflect.Type, methodName string, redirecti
55
56
if ! ok {
56
57
return nil , errors .New (fmt .Sprintf ("Method '%v' not found" , methodName ))
57
58
}
58
- return PatchMethodByReflect (method .Func , redirection )
59
+ return PatchMethodByReflect (method , redirection )
60
+ }
61
+
62
+ // Patches a target func by passing the reflect.Method of the func. The target func will be redirected to the "redirection" func.
63
+ // Both function must have same arguments and return types.
64
+ func PatchMethodByReflect (target reflect.Method , redirection interface {}) (* Patch , error ) {
65
+ return PatchMethodByReflectValue (target .Func , redirection )
59
66
}
67
+
68
+ // Patches a target func with a "redirection" function created at runtime by using "reflect.MakeFunc".
69
+ func PatchMethodWithMakeFunc (target reflect.Method , fn func (args []reflect.Value ) (results []reflect.Value )) (* Patch , error ) {
70
+ return PatchMethodByReflect (target , reflect .MakeFunc (target .Type , fn ))
71
+ }
72
+
60
73
// Patches a target func by passing the reflect.ValueOf of the func. The target func will be redirected to the "redirection" func.
61
74
// Both function must have same arguments and return types.
62
- func PatchMethodByReflect (target reflect.Value , redirection interface {}) (* Patch , error ) {
75
+ func PatchMethodByReflectValue (target reflect.Value , redirection interface {}) (* Patch , error ) {
63
76
tValue := & target
64
77
rValue := getValueFrom (redirection )
65
78
if err := isPatchable (tValue , & rValue ); err != nil {
@@ -71,10 +84,12 @@ func PatchMethodByReflect(target reflect.Value, redirection interface{}) (*Patch
71
84
}
72
85
return patch , nil
73
86
}
87
+
74
88
// Patches a target func with a "redirection" function created at runtime by using "reflect.MakeFunc".
75
- func PatchMethodWithMakeFunc (target reflect.Value , fn func (args []reflect.Value ) (results []reflect.Value )) (* Patch , error ) {
76
- return PatchMethodByReflect (target , reflect .MakeFunc (target .Type (), fn ))
89
+ func PatchMethodWithMakeFuncValue (target reflect.Value , fn func (args []reflect.Value ) (results []reflect.Value )) (* Patch , error ) {
90
+ return PatchMethodByReflectValue (target , reflect .MakeFunc (target .Type (), fn ))
77
91
}
92
+
78
93
// Patch the target func with the redirection func.
79
94
func (p * Patch ) Patch () error {
80
95
if p == nil {
@@ -88,6 +103,7 @@ func (p *Patch) Patch() error {
88
103
}
89
104
return nil
90
105
}
106
+
91
107
// Unpatch the target func and recover the original func.
92
108
func (p * Patch ) Unpatch () error {
93
109
if p == nil {
0 commit comments