Skip to content

Instantly share code, notes, and snippets.

@lcaballero
Created January 8, 2016 18:40
Show Gist options
  • Save lcaballero/fbfe2674c75370230d51 to your computer and use it in GitHub Desktop.
Save lcaballero/fbfe2674c75370230d51 to your computer and use it in GitHub Desktop.
Reflection that produces all related reflect package types.
package lookup
import "reflect"
type Lookup struct {
target interface{}
}
type Facet struct {
Method reflect.Method
Type reflect.Type
Value reflect.Value
ValueOf reflect.Value
}
func (f *Facet) Name() string {
names := []string{
f.Method.Name,
f.Type.Name(),
}
for _, name := range names {
if name != "" {
return name
}
}
return ""
}
func NewLookup(a interface{}) *Lookup {
return &Lookup{
target: a,
}
}
func (p *Lookup) Methods() []*Facet {
ty := reflect.TypeOf(p.target)
val := reflect.ValueOf(p.target)
methods := make([]*Facet, ty.NumMethod())
for i := range methods {
methods[i] = &Facet{
Method: ty.Method(i),
Type: ty,
Value: val.Method(i),
ValueOf: val,
}
}
return methods
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment