Skip to content

Commit e07aeae

Browse files
authored
revert breaking api changes (#9)
1 parent b295ad1 commit e07aeae

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func methodB() int { return 2 }
5959

6060
func TestPatcherUsingReflect(t *testing.T) {
6161
reflectA := reflect.ValueOf(methodA)
62-
patch, err := mPatch.PatchMethodByReflect(reflectA, methodB)
62+
patch, err := mPatch.PatchMethodByReflectValue(reflectA, methodB)
6363
if err != nil {
6464
t.Fatal(err)
6565
}
@@ -84,7 +84,7 @@ func methodA() int { return 1 }
8484

8585
func TestPatcherUsingMakeFunc(t *testing.T) {
8686
reflectA := reflect.ValueOf(methodA)
87-
patch, err := PatchMethodWithMakeFunc(reflectA,
87+
patch, err := PatchMethodWithMakeFuncValue(reflectA,
8888
func(args []reflect.Value) (results []reflect.Value) {
8989
return []reflect.Value{reflect.ValueOf(42)}
9090
})

patcher.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func PatchMethod(target, redirection interface{}) (*Patch, error) {
4444
}
4545
return patch, nil
4646
}
47+
4748
// Patches an instance func by using two parameters, the target struct type and the method name inside that type,
4849
//this func will be redirected to the "redirection" func. Note: The first parameter of the redirection func must be the object instance.
4950
func PatchInstanceMethodByName(target reflect.Type, methodName string, redirection interface{}) (*Patch, error) {
@@ -55,11 +56,23 @@ func PatchInstanceMethodByName(target reflect.Type, methodName string, redirecti
5556
if !ok {
5657
return nil, errors.New(fmt.Sprintf("Method '%v' not found", methodName))
5758
}
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)
5966
}
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+
6073
// Patches a target func by passing the reflect.ValueOf of the func. The target func will be redirected to the "redirection" func.
6174
// 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) {
6376
tValue := &target
6477
rValue := getValueFrom(redirection)
6578
if err := isPatchable(tValue, &rValue); err != nil {
@@ -71,10 +84,12 @@ func PatchMethodByReflect(target reflect.Value, redirection interface{}) (*Patch
7184
}
7285
return patch, nil
7386
}
87+
7488
// 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))
7791
}
92+
7893
// Patch the target func with the redirection func.
7994
func (p *Patch) Patch() error {
8095
if p == nil {
@@ -88,6 +103,7 @@ func (p *Patch) Patch() error {
88103
}
89104
return nil
90105
}
106+
91107
// Unpatch the target func and recover the original func.
92108
func (p *Patch) Unpatch() error {
93109
if p == nil {

patcher_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestPatcher(t *testing.T) {
4646

4747
func TestPatcherUsingReflect(t *testing.T) {
4848
reflectA := reflect.ValueOf(methodA)
49-
patch, err := PatchMethodByReflect(reflectA, methodB)
49+
patch, err := PatchMethodByReflectValue(reflectA, methodB)
5050
if err != nil {
5151
t.Fatal(err)
5252
}
@@ -65,7 +65,7 @@ func TestPatcherUsingReflect(t *testing.T) {
6565

6666
func TestPatcherUsingMakeFunc(t *testing.T) {
6767
reflectA := reflect.ValueOf(methodA)
68-
patch, err := PatchMethodWithMakeFunc(reflectA,
68+
patch, err := PatchMethodWithMakeFuncValue(reflectA,
6969
func(args []reflect.Value) (results []reflect.Value) {
7070
return []reflect.Value{reflect.ValueOf(42)}
7171
})

0 commit comments

Comments
 (0)