Skip to content

Instantly share code, notes, and snippets.

@imosquera
Last active December 24, 2015 00:29
Show Gist options
  • Select an option

  • Save imosquera/6716619 to your computer and use it in GitHub Desktop.

Select an option

Save imosquera/6716619 to your computer and use it in GitHub Desktop.
This gist helps re-assign functions during testing
package util
import (
"reflect"
)
// Restorer holds a function that can be used
// to restore some previous state.
type Restorer func()
// Restore restores some previous state.
func (r Restorer) Restore() {
r()
}
// Patch sets the value pointed to by the given destination to the given
// value, and returns a function to restore it to its original value. The
// value must be assignable to the element type of the destination.
func Patch(dest, value interface{}) Restorer {
destv := reflect.ValueOf(dest).Elem()
oldv := reflect.New(destv.Type()).Elem()
oldv.Set(destv)
valuev := reflect.ValueOf(value)
if !valuev.IsValid() {
// This isn't quite right when the destination type is not
// nilable, but it's better than the complex alternative.
valuev = reflect.Zero(destv.Type())
}
destv.Set(valuev)
return func() {
destv.Set(oldv)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment