From 7b5a6dbcd6a8c283340c07d0bcdac116bb172d42 Mon Sep 17 00:00:00 2001 From: samanhappy Date: Tue, 7 May 2024 13:16:55 +0800 Subject: [PATCH 1/4] restore PatchInstanceMethod --- monkey.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/monkey.go b/monkey.go index 25e763b..02dd2db 100644 --- a/monkey.go +++ b/monkey.go @@ -56,6 +56,20 @@ func Patch(target, replacement interface{}, opts ...Option) *PatchGuard { return &PatchGuard{t, r, o} } +// PatchInstanceMethod replaces an instance method methodName for the type target with replacement +// Replacement should expect the receiver (of type target) as the first argument +func PatchInstanceMethod(target reflect.Type, methodName string, replacement interface{}) *PatchGuard { + m, ok := target.MethodByName(methodName) + if !ok { + panic(fmt.Sprintf("unknown method %s", methodName)) + } + + o := &opt{} + r := reflect.ValueOf(replacement) + patchValue(m.Func, r, o) + return &PatchGuard{m.Func, r, o} +} + // See reflect.Value type value struct { _ uintptr From e545191c68f05341f5f2cc42ba3087ab44dc71be Mon Sep 17 00:00:00 2001 From: samanhappy Date: Tue, 7 May 2024 14:06:37 +0800 Subject: [PATCH 2/4] fix global & restore test --- monkey.go | 2 +- monkey_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/monkey.go b/monkey.go index 02dd2db..fb34153 100644 --- a/monkey.go +++ b/monkey.go @@ -64,7 +64,7 @@ func PatchInstanceMethod(target reflect.Type, methodName string, replacement int panic(fmt.Sprintf("unknown method %s", methodName)) } - o := &opt{} + o := &opt{global: true} r := reflect.ValueOf(replacement) patchValue(m.Func, r, o) return &PatchGuard{m.Func, r, o} diff --git a/monkey_test.go b/monkey_test.go index dbb28dc..10bc8b5 100644 --- a/monkey_test.go +++ b/monkey_test.go @@ -90,7 +90,7 @@ func (f *f) No() bool { return false } func TestOnInstanceMethod(t *testing.T) { i := &f{} assert(t, !i.No()) - monkey.Patch((*f).No, func(_ *f) bool { return true }) + monkey.PatchInstanceMethod(reflect.TypeOf(i), "No", func(_ *f) bool { return true }) assert(t, i.No()) assert(t, monkey.UnpatchInstanceMethod(reflect.TypeOf(i), "No")) assert(t, !i.No()) From eca5adb5bbbf048b7e92d3462751fb5a026dbf09 Mon Sep 17 00:00:00 2001 From: samanhappy Date: Tue, 7 May 2024 14:41:52 +0800 Subject: [PATCH 3/4] unset global --- monkey.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monkey.go b/monkey.go index fb34153..02dd2db 100644 --- a/monkey.go +++ b/monkey.go @@ -64,7 +64,7 @@ func PatchInstanceMethod(target reflect.Type, methodName string, replacement int panic(fmt.Sprintf("unknown method %s", methodName)) } - o := &opt{global: true} + o := &opt{} r := reflect.ValueOf(replacement) patchValue(m.Func, r, o) return &PatchGuard{m.Func, r, o} From 573b46f7ec9c1b427918158a16c419b8b60734b7 Mon Sep 17 00:00:00 2001 From: samanhappy Date: Tue, 7 May 2024 15:33:37 +0800 Subject: [PATCH 4/4] fix test --- monkey.go | 2 +- monkey_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/monkey.go b/monkey.go index 02dd2db..fb34153 100644 --- a/monkey.go +++ b/monkey.go @@ -64,7 +64,7 @@ func PatchInstanceMethod(target reflect.Type, methodName string, replacement int panic(fmt.Sprintf("unknown method %s", methodName)) } - o := &opt{} + o := &opt{global: true} r := reflect.ValueOf(replacement) patchValue(m.Func, r, o) return &PatchGuard{m.Func, r, o} diff --git a/monkey_test.go b/monkey_test.go index 10bc8b5..f4c02cc 100644 --- a/monkey_test.go +++ b/monkey_test.go @@ -90,9 +90,9 @@ func (f *f) No() bool { return false } func TestOnInstanceMethod(t *testing.T) { i := &f{} assert(t, !i.No()) - monkey.PatchInstanceMethod(reflect.TypeOf(i), "No", func(_ *f) bool { return true }) + pg := monkey.PatchInstanceMethod(reflect.TypeOf(i), "No", func(_ *f) bool { return true }) assert(t, i.No()) - assert(t, monkey.UnpatchInstanceMethod(reflect.TypeOf(i), "No")) + pg.Unpatch() assert(t, !i.No()) }