Skip to content

Commit

Permalink
revert breaking api changes (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyredondo authored Jun 16, 2020
1 parent b295ad1 commit e07aeae
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func methodB() int { return 2 }

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

func TestPatcherUsingMakeFunc(t *testing.T) {
reflectA := reflect.ValueOf(methodA)
patch, err := PatchMethodWithMakeFunc(reflectA,
patch, err := PatchMethodWithMakeFuncValue(reflectA,
func(args []reflect.Value) (results []reflect.Value) {
return []reflect.Value{reflect.ValueOf(42)}
})
Expand Down
24 changes: 20 additions & 4 deletions patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func PatchMethod(target, redirection interface{}) (*Patch, error) {
}
return patch, nil
}

// Patches an instance func by using two parameters, the target struct type and the method name inside that type,
//this func will be redirected to the "redirection" func. Note: The first parameter of the redirection func must be the object instance.
func PatchInstanceMethodByName(target reflect.Type, methodName string, redirection interface{}) (*Patch, error) {
Expand All @@ -55,11 +56,23 @@ func PatchInstanceMethodByName(target reflect.Type, methodName string, redirecti
if !ok {
return nil, errors.New(fmt.Sprintf("Method '%v' not found", methodName))
}
return PatchMethodByReflect(method.Func, redirection)
return PatchMethodByReflect(method, redirection)
}

// Patches a target func by passing the reflect.Method of the func. The target func will be redirected to the "redirection" func.
// Both function must have same arguments and return types.
func PatchMethodByReflect(target reflect.Method, redirection interface{}) (*Patch, error) {
return PatchMethodByReflectValue(target.Func, redirection)
}

// Patches a target func with a "redirection" function created at runtime by using "reflect.MakeFunc".
func PatchMethodWithMakeFunc(target reflect.Method, fn func(args []reflect.Value) (results []reflect.Value)) (*Patch, error) {
return PatchMethodByReflect(target, reflect.MakeFunc(target.Type, fn))
}

// Patches a target func by passing the reflect.ValueOf of the func. The target func will be redirected to the "redirection" func.
// Both function must have same arguments and return types.
func PatchMethodByReflect(target reflect.Value, redirection interface{}) (*Patch, error) {
func PatchMethodByReflectValue(target reflect.Value, redirection interface{}) (*Patch, error) {
tValue := &target
rValue := getValueFrom(redirection)
if err := isPatchable(tValue, &rValue); err != nil {
Expand All @@ -71,10 +84,12 @@ func PatchMethodByReflect(target reflect.Value, redirection interface{}) (*Patch
}
return patch, nil
}

// Patches a target func with a "redirection" function created at runtime by using "reflect.MakeFunc".
func PatchMethodWithMakeFunc(target reflect.Value, fn func(args []reflect.Value) (results []reflect.Value)) (*Patch, error) {
return PatchMethodByReflect(target, reflect.MakeFunc(target.Type(), fn))
func PatchMethodWithMakeFuncValue(target reflect.Value, fn func(args []reflect.Value) (results []reflect.Value)) (*Patch, error) {
return PatchMethodByReflectValue(target, reflect.MakeFunc(target.Type(), fn))
}

// Patch the target func with the redirection func.
func (p *Patch) Patch() error {
if p == nil {
Expand All @@ -88,6 +103,7 @@ func (p *Patch) Patch() error {
}
return nil
}

// Unpatch the target func and recover the original func.
func (p *Patch) Unpatch() error {
if p == nil {
Expand Down
4 changes: 2 additions & 2 deletions patcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestPatcher(t *testing.T) {

func TestPatcherUsingReflect(t *testing.T) {
reflectA := reflect.ValueOf(methodA)
patch, err := PatchMethodByReflect(reflectA, methodB)
patch, err := PatchMethodByReflectValue(reflectA, methodB)
if err != nil {
t.Fatal(err)
}
Expand All @@ -65,7 +65,7 @@ func TestPatcherUsingReflect(t *testing.T) {

func TestPatcherUsingMakeFunc(t *testing.T) {
reflectA := reflect.ValueOf(methodA)
patch, err := PatchMethodWithMakeFunc(reflectA,
patch, err := PatchMethodWithMakeFuncValue(reflectA,
func(args []reflect.Value) (results []reflect.Value) {
return []reflect.Value{reflect.ValueOf(42)}
})
Expand Down

0 comments on commit e07aeae

Please sign in to comment.