(
obj: object,
path: Sequence[str],
*,
index: int,
flattened_key: str | None,
array_format: ArrayFormat,
)
| 77 | |
| 78 | |
| 79 | def _extract_items( |
| 80 | obj: object, |
| 81 | path: Sequence[str], |
| 82 | *, |
| 83 | index: int, |
| 84 | flattened_key: str | None, |
| 85 | array_format: ArrayFormat, |
| 86 | ) -> list[tuple[str, FileTypes]]: |
| 87 | try: |
| 88 | key = path[index] |
| 89 | except IndexError: |
| 90 | if not is_given(obj): |
| 91 | # no value was provided - we can safely ignore |
| 92 | return [] |
| 93 | |
| 94 | # cyclical import |
| 95 | from .._files import assert_is_file_content |
| 96 | |
| 97 | # We have exhausted the path, return the entry we found. |
| 98 | assert flattened_key is not None |
| 99 | |
| 100 | if is_list(obj): |
| 101 | files: list[tuple[str, FileTypes]] = [] |
| 102 | for array_index, entry in enumerate(obj): |
| 103 | suffix = _array_suffix(array_format, array_index) |
| 104 | emitted_key = (flattened_key + suffix) if flattened_key else suffix |
| 105 | assert_is_file_content(entry, key=emitted_key) |
| 106 | files.append((emitted_key, cast(FileTypes, entry))) |
| 107 | return files |
| 108 | |
| 109 | assert_is_file_content(obj, key=flattened_key) |
| 110 | return [(flattened_key, cast(FileTypes, obj))] |
| 111 | |
| 112 | index += 1 |
| 113 | if is_dict(obj): |
| 114 | try: |
| 115 | # Remove the field if there are no more dict keys in the path, |
| 116 | # only "<array>" traversal markers or end. |
| 117 | if all(p == "<array>" for p in path[index:]): |
| 118 | item = obj.pop(key) |
| 119 | else: |
| 120 | item = obj[key] |
| 121 | except KeyError: |
| 122 | # Key was not present in the dictionary, this is not indicative of an error |
| 123 | # as the given path may not point to a required field. We also do not want |
| 124 | # to enforce required fields as the API may differ from the spec in some cases. |
| 125 | return [] |
| 126 | if flattened_key is None: |
| 127 | flattened_key = key |
| 128 | else: |
| 129 | flattened_key += f"[{key}]" |
| 130 | return _extract_items( |
| 131 | item, |
| 132 | path, |
| 133 | index=index, |
| 134 | flattened_key=flattened_key, |
| 135 | array_format=array_format, |
| 136 | ) |
no test coverage detected