MCPcopy
hub / github.com/expr-lang/expr / FetchField

Function FetchField

vm/runtime/runtime.go:109–126  ·  view source on GitHub ↗
(from any, field *Field)

Source from the content-addressed store, hash-verified

107}
108
109func FetchField(from any, field *Field) any {
110 v := reflect.ValueOf(from)
111 if v.Kind() != reflect.Invalid {
112 v = reflect.Indirect(v)
113
114 // We can use v.FieldByIndex here, but it will panic if the field
115 // is not exists. And we need to recover() to generate a more
116 // user-friendly error message.
117 // Also, our fieldByIndex() function is slightly faster than the
118 // v.FieldByIndex() function as we don't need to verify what a field
119 // is a struct as we already did it on compilation step.
120 value := fieldByIndex(v, field)
121 if value.IsValid() {
122 return value.Interface()
123 }
124 }
125 panic(fmt.Sprintf("cannot get %v from %T", field.Path[0], from))
126}
127
128func fieldByIndex(v reflect.Value, field *Field) reflect.Value {
129 if len(field.Index) == 1 {

Callers 1

RunMethod · 0.92

Calls 2

fieldByIndexFunction · 0.85
SprintfMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…