fieldsByName fills a values interface with fields from the passed value based on the traversals in int. If ptrs is true, return addresses instead of values. We write this instead of using FieldsByName to save allocations and map lookups when iterating over many rows. Empty traversals will get an i
(v reflect.Value, traversals [][]int, values []interface{}, ptrs bool)
| 1024 | // Because of the necessity of requesting ptrs or values, it's considered a bit too |
| 1025 | // specialized for inclusion in reflectx itself. |
| 1026 | func fieldsByTraversal(v reflect.Value, traversals [][]int, values []interface{}, ptrs bool) error { |
| 1027 | v = reflect.Indirect(v) |
| 1028 | if v.Kind() != reflect.Struct { |
| 1029 | return errors.New("argument not a struct") |
| 1030 | } |
| 1031 | |
| 1032 | for i, traversal := range traversals { |
| 1033 | if len(traversal) == 0 { |
| 1034 | values[i] = new(interface{}) |
| 1035 | continue |
| 1036 | } |
| 1037 | f := reflectx.FieldByIndexes(v, traversal) |
| 1038 | if ptrs { |
| 1039 | values[i] = f.Addr().Interface() |
| 1040 | } else { |
| 1041 | values[i] = f.Interface() |
| 1042 | } |
| 1043 | } |
| 1044 | return nil |
| 1045 | } |
| 1046 | |
| 1047 | func missingFields(transversals [][]int) (field int, err error) { |
| 1048 | for i, t := range transversals { |
no test coverage detected