forked from jpmorganchase/py-avro-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement make_default for PydanticSchema (jpmorganchase#64)
- Loading branch information
1 parent
57df5ff
commit 28a2133
Showing
2 changed files
with
10 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -567,6 +567,9 @@ def data(self, names: NamesType) -> JSONObj: | |
"items": self.items_schema.data(names=names), | ||
} | ||
|
||
def make_default(self, py_default: Any) -> Any: | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
dada-engineer
Author
Owner
|
||
return [_schema_obj(default.__class__).make_default(default) for default in py_default] | ||
|
||
|
||
class DictSchema(Schema): | ||
"""An Avro map schema for a given Python mapping""" | ||
|
@@ -807,10 +810,6 @@ def __init__( | |
if isinstance(self.schema, UnionSchema): | ||
self.schema.sort_item_schemas(self.default) | ||
typeguard.check_type(self.default, self.py_type) | ||
|
||
# allow to use pydantic models as default values | ||
if PydanticSchema.handles_type(default.__class__): | ||
self.default = default.model_dump(mode="json") # type: ignore | ||
else: | ||
if Option.DEFAULTS_MANDATORY in self.options: | ||
raise TypeError(f"Default value for field {self} is missing") | ||
|
@@ -906,6 +905,10 @@ def _record_field(self, name: str, py_field: pydantic.fields.FieldInfo) -> Recor | |
) | ||
return field_obj | ||
|
||
def make_default(self, py_default: Any) -> Any: | ||
"""Return an Avro schema compliant default value for a given Python value""" | ||
return {key: _schema_obj(value.__class__).make_default(value) for key, value in py_default} | ||
|
||
def _annotation(self, field_name: str) -> Type: | ||
""" | ||
Fetch the raw annotation for a given field name | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Could I propose we do this in a separate PR?