MCPcopy
hub / github.com/lxn/walk / reflectValueFromPath

Function reflectValueFromPath

databinding.go:449–531  ·  view source on GitHub ↗
(root reflect.Value, path string)

Source from the content-addressed store, hash-verified

447}
448
449func reflectValueFromPath(root reflect.Value, path string) (parent, value reflect.Value, err error) {
450 fullPath := path
451 value = root
452
453 for path != "" {
454 var name string
455 name, path = nextPathPart(path)
456
457 var p reflect.Value
458 for value.Kind() == reflect.Interface || value.Kind() == reflect.Ptr {
459 p = value
460 value = value.Elem()
461 }
462
463 switch value.Kind() {
464 case reflect.Map:
465 parent = value
466 value = value.MapIndex(reflect.ValueOf(name))
467
468 case reflect.Struct:
469 parent = value
470
471 var fun reflect.Value
472
473 // Try as field first.
474 if f := value.FieldByName(name); f.IsValid() {
475 switch f.Kind() {
476 case reflect.Func:
477 fun = f
478
479 case reflect.Interface:
480 if fn := f.Elem(); fn.Kind() == reflect.Func {
481 fun = fn
482 } else {
483 value = f
484 }
485
486 default:
487 value = f
488 }
489 } else {
490 // No field, so let's see if we got a method.
491 if p.IsValid() {
492 // Try pointer receiver first.
493 fun = p.MethodByName(name)
494 }
495
496 if !fun.IsValid() {
497 // No pointer, try directly.
498 fun = value.MethodByName(name)
499 }
500 if !fun.IsValid() {
501 return parent, value, fmt.Errorf("bad member: '%s', path: '%s'", path, fullPath)
502 }
503 }
504
505 if fun.IsValid() {
506 // We assume it takes no args and returns one mandatory value plus

Callers 3

ValueMethod · 0.85
valueFromSliceFunction · 0.85
dataFieldFromPathFunction · 0.85

Calls 1

nextPathPartFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…