|
17 | 17 | class BaseSchema(ABC): |
18 | 18 | """Abstract class for schema wrapper""" |
19 | 19 |
|
20 | | - def __init__(self, schema: typing.Union[str, typing.Dict[str, typing.Any]]) -> None: |
| 20 | + def __init__(self, schema: typing.Union[str, typing.Dict[str, typing.Any]], ignore_default_error: bool = False) -> None: |
21 | 21 | if isinstance(schema, str): |
22 | 22 | schema = json.loads(schema) |
23 | 23 | self.raw_schema = typing.cast(typing.Dict, schema) |
| 24 | + self.ignore_default_error = ignore_default_error |
24 | 25 | self.schema = self.parse_schema(self.raw_schema) |
25 | 26 | self.generate_hash() |
26 | 27 |
|
@@ -66,10 +67,10 @@ def __eq__(self, other: typing.Any) -> bool: |
66 | 67 | class AvroSchema(BaseSchema): |
67 | 68 | """Integrate BaseSchema for Avro schema.""" |
68 | 69 |
|
69 | | - def __init__(self, *args: typing.Any, **kwargs: typing.Any) -> None: |
| 70 | + def __init__(self, *args: typing.Any, ignore_default_error: bool = False, **kwargs: typing.Any) -> None: |
70 | 71 | self._expanded_schema: typing.Optional[typing.Dict] = None |
71 | 72 | self._flat_schema: typing.Optional[typing.Dict] = None |
72 | | - |
| 73 | + self.ignore_default_error: typing.Optional[bool] = ignore_default_error |
73 | 74 | super().__init__(*args, **kwargs) |
74 | 75 |
|
75 | 76 | @property |
@@ -103,28 +104,28 @@ def flat_schema(self) -> typing.Dict: |
103 | 104 | # NOTE: Dict expected when we pass a dict |
104 | 105 | self._flat_schema = typing.cast( |
105 | 106 | typing.Dict, |
106 | | - fastavro.parse_schema(self.raw_schema, _write_hint=False, _force=True), |
| 107 | + fastavro.parse_schema(self.raw_schema, _write_hint=False, _force=True, _ignore_default_error=self.ignore_default_error), |
107 | 108 | ) |
108 | 109 |
|
109 | 110 | return self._flat_schema |
110 | 111 |
|
111 | 112 | def parse_schema(self, schema: typing.Dict) -> typing.Dict: |
112 | 113 | # NOTE: Dict expected when we pass a dict |
113 | | - return typing.cast(typing.Dict, fastavro.parse_schema(schema, _force=True)) |
| 114 | + return typing.cast(typing.Dict, fastavro.parse_schema(schema, _force=True, _ignore_default_error=self.ignore_default_error)) |
114 | 115 |
|
115 | 116 | @staticmethod |
116 | | - def load(fp: str) -> AvroSchema: |
| 117 | + def load(fp: str, ignore_default_error=False) -> AvroSchema: |
117 | 118 | """Parse an avro schema from a file path.""" |
118 | 119 | with open(fp, mode="r") as f: |
119 | 120 | content = f.read() |
120 | | - return AvroSchema(content) |
| 121 | + return AvroSchema(content, ignore_default_error) |
121 | 122 |
|
122 | 123 | @staticmethod |
123 | | - async def async_load(fp: str) -> AvroSchema: |
| 124 | + async def async_load(fp: str, ignore_default_error=False) -> AvroSchema: |
124 | 125 | """Parse an avro schema from a file path.""" |
125 | 126 | async with aiofiles.open(fp, mode="r") as f: |
126 | 127 | content = await f.read() |
127 | | - return AvroSchema(content) |
| 128 | + return AvroSchema(content, ignore_default_error) |
128 | 129 |
|
129 | 130 |
|
130 | 131 | class JsonSchema(BaseSchema): |
|
0 commit comments