Skip to content

Commit

Permalink
Improve the reflect of getFunc(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
szyhf authored and antonmedv committed Jan 19, 2019
1 parent c807621 commit dc7e436
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func extract(val interface{}, i interface{}) (interface{}, bool) {
return nil, false
}

func getFunc(val interface{}, i interface{}) (interface{}, bool) {
func getFunc(val interface{}, name string) (interface{}, bool) {
v := reflect.ValueOf(val)
d := v
if v.Kind() == reflect.Ptr {
Expand All @@ -92,20 +92,18 @@ func getFunc(val interface{}, i interface{}) (interface{}, bool) {

switch d.Kind() {
case reflect.Map:
value := d.MapIndex(reflect.ValueOf(i))
value := d.MapIndex(reflect.ValueOf(name))
if value.IsValid() && value.CanInterface() {
return value.Interface(), true
}
// A map may have method too.
if v.NumMethod() > 0 {
name := reflect.ValueOf(i).String()
method := v.MethodByName(name)
if method.IsValid() && method.CanInterface() {
return method.Interface(), true
}
}
case reflect.Struct:
name := reflect.ValueOf(i).String()
method := v.MethodByName(name)
if method.IsValid() && method.CanInterface() {
return method.Interface(), true
Expand Down

0 comments on commit dc7e436

Please sign in to comment.