Created
January 8, 2016 18:40
-
-
Save lcaballero/fbfe2674c75370230d51 to your computer and use it in GitHub Desktop.
Reflection that produces all related reflect package types.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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