(from any, field *Field)
| 107 | } |
| 108 | |
| 109 | func 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 | |
| 128 | func fieldByIndex(v reflect.Value, field *Field) reflect.Value { |
| 129 | if len(field.Index) == 1 { |
no test coverage detected
searching dependent graphs…