-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: model generator refactored (#569)
- Loading branch information
1 parent
a5fa3c1
commit c9c31e8
Showing
18 changed files
with
826 additions
and
674 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from .schema_generator import AvroModel # noqa: F401 I001 | ||
from .types import * # noqa: F401 F403 | ||
from .model_generator.generator import BaseClassEnum, ModelGenerator # noqa: F401 | ||
from .model_generator.generator import BaseClassEnum, ModelType, ModelGenerator # noqa: F401 | ||
from .fields.field_utils import * # noqa: F401 F403 | ||
from .fields.fields import * # noqa: F401 F403 |
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Empty file.
File renamed without changes.
Empty file.
51 changes: 51 additions & 0 deletions
51
dataclasses_avroschema/model_generator/lang/python/avrodantic/avrodantic_model_generator.py
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import typing | ||
from dataclasses import dataclass | ||
|
||
from dataclasses_avroschema.model_generator.lang.python import templates | ||
from dataclasses_avroschema.model_generator.lang.python.base import BaseGenerator | ||
from dataclasses_avroschema.types import JsonDict | ||
|
||
|
||
@dataclass | ||
class AvroDanticModelGenerator(BaseGenerator): | ||
def __post_init__(self) -> None: | ||
super().__post_init__() | ||
|
||
self.base_class = "AvroBaseModel" | ||
self.imports.add("from dataclasses_avroschema.pydantic import AvroBaseModel") | ||
self.imports_dict = { | ||
"dataclass_field": "from pydantic import Field", | ||
} | ||
|
||
# Templates | ||
self.field_template = templates.pydantic_field_template | ||
|
||
def _resolve_type_from_metadata(self, *, field: JsonDict) -> typing.Optional[str]: | ||
""" | ||
Check if the language type must be replaced with any extra class which | ||
was specified in the field metadata. This method should be only called | ||
after the native type was resolved properly. | ||
An example of this if when a pydantic field was used in the model: | ||
class MyModel(AvroBaseModel): | ||
email: pydantic.EmailStr | ||
then the email field is represented as: | ||
{"name": "email", "type": {"type": "string", "pydantic-class": "EmailStr"}} | ||
For now we only recognize the attribute `pydantic-class` but in the future | ||
new way might be added, for example: `java-class`. | ||
""" | ||
pydantic_class = field.get("pydantic-class") | ||
|
||
if pydantic_class is not None: | ||
self.imports.add("import pydantic") | ||
return f"pydantic.{pydantic_class}" | ||
return None | ||
|
||
def render_dataclass_field(self, properties: str) -> str: | ||
self.imports.add("from pydantic import Field") | ||
return super().render_dataclass_field(properties=properties) |
Oops, something went wrong.