|
| 1 | +from typing import TYPE_CHECKING, Dict, cast |
1 | 2 | from mindee.parsing.common.string_dict import StringDict |
2 | 3 | from mindee.parsing.v2.field.base_field import BaseField |
3 | 4 | from mindee.parsing.v2.field.dynamic_field import FieldType |
4 | 5 | from mindee.parsing.v2.field.inference_fields import InferenceFields |
5 | 6 |
|
| 7 | +if TYPE_CHECKING: |
| 8 | + from mindee.parsing.v2.field.list_field import ListField |
| 9 | + from mindee.parsing.v2.field.simple_field import SimpleField |
| 10 | + |
6 | 11 |
|
7 | 12 | class ObjectField(BaseField): |
8 | 13 | """Object field containing multiple fields.""" |
@@ -37,5 +42,86 @@ def multi_str(self) -> str: |
37 | 42 | first = False |
38 | 43 | return out_str |
39 | 44 |
|
| 45 | + @property |
| 46 | + def simple_fields(self) -> Dict[str, "SimpleField"]: |
| 47 | + """ |
| 48 | + Extract and return all SimpleField fields from the `fields` attribute. |
| 49 | +
|
| 50 | + :return: A dictionary containing all fields that have a type of `FieldType.SIMPLE`. |
| 51 | + """ |
| 52 | + simple_fields = {} |
| 53 | + for field_key, field_value in self.fields.items(): |
| 54 | + if field_value.field_type == FieldType.SIMPLE: |
| 55 | + simple_fields[field_key] = cast("SimpleField", field_value) |
| 56 | + return simple_fields |
| 57 | + |
| 58 | + @property |
| 59 | + def list_fields(self) -> Dict[str, "ListField"]: |
| 60 | + """ |
| 61 | + Retrieves all ListField fields from the `fields` attribute. |
| 62 | +
|
| 63 | + :return: A dictionary containing all fields of type `LIST`, with keys |
| 64 | + representing field keys and values being the corresponding field |
| 65 | + objects. |
| 66 | + """ |
| 67 | + list_fields = {} |
| 68 | + for field_key, field_value in self.fields.items(): |
| 69 | + if field_value.field_type == FieldType.LIST: |
| 70 | + list_fields[field_key] = cast("ListField", field_value) |
| 71 | + return list_fields |
| 72 | + |
| 73 | + @property |
| 74 | + def object_fields(self) -> Dict[str, "ObjectField"]: |
| 75 | + """ |
| 76 | + Retrieves all ObjectField fields from the `fields` attribute of the instance. |
| 77 | +
|
| 78 | + :returns: A dictionary containing fields of type `FieldType.OBJECT`. The keys represent |
| 79 | + the field names, and the values are corresponding ObjectField objects. |
| 80 | + """ |
| 81 | + object_fields = {} |
| 82 | + for field_key, field_value in self.fields.items(): |
| 83 | + if field_value.field_type == FieldType.OBJECT: |
| 84 | + object_fields[field_key] = cast("ObjectField", field_value) |
| 85 | + return object_fields |
| 86 | + |
| 87 | + def get_simple_field(self, field_name: str) -> "SimpleField": |
| 88 | + """ |
| 89 | + Retrieves a SimpleField from the provided field name. |
| 90 | +
|
| 91 | + :param field_name: The name of the field to retrieve. |
| 92 | + :type field_name: str |
| 93 | + :return: The SimpleField object corresponding to the given field name. |
| 94 | + :raises ValueError: If the specified field is not of type SimpleField. |
| 95 | + """ |
| 96 | + if self.fields[field_name].field_type != FieldType.SIMPLE: |
| 97 | + raise ValueError(f"Field {field_name} is not a SimpleField.") |
| 98 | + return cast("SimpleField", self.fields[field_name]) |
| 99 | + |
| 100 | + def get_list_field(self, field_name: str) -> "ListField": |
| 101 | + """ |
| 102 | + Retrieves the ``ListField`` for the specified field name. |
| 103 | +
|
| 104 | + :param field_name: The name of the field to retrieve. |
| 105 | + :type field_name: str |
| 106 | + :return: The corresponding ``ListField`` for the given field name. |
| 107 | + :raises ValueError: If the field is not of type ``ListField``. |
| 108 | + """ |
| 109 | + if self.fields[field_name].field_type != FieldType.LIST: |
| 110 | + raise ValueError(f"Field {field_name} is not a ListField.") |
| 111 | + return cast("ListField", self.fields[field_name]) |
| 112 | + |
| 113 | + def get_object_field(self, field_name: str) -> "ObjectField": |
| 114 | + """ |
| 115 | + Retrieves the `ObjectField` associated with the specified field name. |
| 116 | +
|
| 117 | + :param field_name: The name of the field to retrieve. |
| 118 | + :type field_name: str |
| 119 | + :return: The `ObjectField` associated with the given field name. |
| 120 | + :raises ValueError: If the field specified by `field_name` is not an `ObjectField`. |
| 121 | + """ |
| 122 | + if self.fields[field_name].field_type != FieldType.OBJECT: |
| 123 | + raise ValueError(f"Field {field_name} is not an ObjectField.") |
| 124 | + return cast("ObjectField", self.fields[field_name]) |
| 125 | + |
40 | 126 | def __str__(self) -> str: |
41 | 127 | return self.single_str() |
0 commit comments