Converts a FieldPath to a FieldRef consisting exclusively of field names. The resulting FieldRef can be used to lookup the corresponding field in any schema regardless of missing/unordered fields. The input path is assumed to be valid for the given schema.
| 164 | // regardless of missing/unordered fields. The input path is assumed to be valid for the |
| 165 | // given schema. |
| 166 | FieldRef ToUniversalRef(const FieldPath& path, const Schema& schema) { |
| 167 | std::vector<FieldRef> refs; |
| 168 | refs.reserve(path.indices().size()); |
| 169 | |
| 170 | const FieldVector* fields = &schema.fields(); |
| 171 | for (auto it = path.begin(); it != path.end(); ++it) { |
| 172 | DCHECK_LT(*it, static_cast<int>(fields->size())); |
| 173 | const auto& child_field = *(*fields)[*it]; |
| 174 | refs.emplace_back(child_field.name()); |
| 175 | if (it + 1 != path.end()) { |
| 176 | auto&& child_type = checked_cast<const StructType&>(*child_field.type()); |
| 177 | fields = &child_type.fields(); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return refs.empty() ? FieldRef() |
| 182 | : refs.size() == 1 ? refs[0] |
| 183 | : FieldRef(std::move(refs)); |
| 184 | } |
| 185 | |
| 186 | int TopLevelIndex(const FieldRef& ref, const Schema& schema) { |
| 187 | if (const auto* name = ref.name()) { |