Skip to content

Commit

Permalink
fix: pass to fastavro all schemas in a single call (#553)
Browse files Browse the repository at this point in the history
* Pass to fastavro all schemas in a single call

* fix formatting
  • Loading branch information
omer-shtivi authored Feb 22, 2024
1 parent 260e350 commit df42186
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions dataclasses_avroschema/model_generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ def __post_init__(self) -> None:
self.imports_dict = {"dataclass_field": "from pydantic import Field"}

@staticmethod
def validate_schema(*, schema: JsonDict) -> None:
def validate_schema(*, schemas: typing.List[JsonDict]) -> None:
"""
Validate that the schema is a valid avro schema
Validate that the schemas are valid avro schemas
"""
fastavro.parse_schema(schema)
fastavro.parse_schema(schemas)

def render_imports(self) -> str:
"""
Expand All @@ -106,7 +106,7 @@ def render_extras(self) -> str:
return "".join([extra for extra in self.extras])

def render_metaclass(
self, *, schema: JsonDict, field_order: typing.Optional[typing.List[str]] = None
self, *, schema: JsonDict, field_order: typing.Optional[typing.List[str]] = None
) -> typing.Optional[str]:
"""
Render Class Meta that contains the schema matadata
Expand All @@ -120,7 +120,7 @@ def render_metaclass(
# Parse the entire schema string into the Meta class field "original_schema" for each class.
# If self.include_original_schema is set to True.
if self.include_original_schema:
metadata.append(self._add_schema_to_metaclass(self.metadata_field_templates['original_schema'], schema))
metadata.append(self._add_schema_to_metaclass(self.metadata_field_templates["original_schema"], schema))

if field_order is not None:
metadata.append(
Expand All @@ -135,11 +135,10 @@ def render_metaclass(
if properties:
# some formating to remove identation at the end of the Class Meta to make it more compatible with black
return (
self.field_identation.join(
[line for line in
templates.metaclass_template.safe_substitute(properties=properties).split("\n")]
).rstrip(self.field_identation)
+ "\n"
self.field_identation.join(
[line for line in templates.metaclass_template.safe_substitute(properties=properties).split("\n")]
).rstrip(self.field_identation)
+ "\n"
)
return None

Expand Down Expand Up @@ -207,8 +206,8 @@ def render_module(self, *, schemas: typing.List[JsonDict]) -> str:
"""
Render the module with the classes generated from the schemas
"""
for schema in schemas:
self.validate_schema(schema=schema)

self.validate_schema(schemas=schemas)

classes = "\n".join(self.render_class(schema=schema) for schema in schemas)
imports = self.render_imports()
Expand Down Expand Up @@ -478,11 +477,11 @@ def _get_complex_langauge_type(self, *, type: typing.Any, model_name: str) -> st
return language_type

def get_language_type(
self,
*,
type: str,
default: typing.Optional[str] = None,
model_name: typing.Optional[str] = None,
self,
*,
type: str,
default: typing.Optional[str] = None,
model_name: typing.Optional[str] = None,
) -> str:
if type in (field_utils.INT, field_utils.FLOAT):
self.imports.add("from dataclasses_avroschema import types")
Expand Down Expand Up @@ -549,8 +548,8 @@ class MyModel(AvroBaseModel):
"""
if self.base_class in (
BaseClassEnum.AVRO_DANTIC_MODEL.value,
BaseClassEnum.PYDANTIC_MODEL.value,
BaseClassEnum.AVRO_DANTIC_MODEL.value,
BaseClassEnum.PYDANTIC_MODEL.value,
):
pydantic_class = field.get("pydantic-class")

Expand All @@ -560,12 +559,12 @@ class MyModel(AvroBaseModel):
return None

def get_field_default(
self,
*,
field_type: AvroTypeRepr,
default: typing.Any,
name: str,
field_metadata: typing.Optional[JsonDict] = None,
self,
*,
field_type: AvroTypeRepr,
default: typing.Any,
name: str,
field_metadata: typing.Optional[JsonDict] = None,
) -> typing.Any:
"""
Returns the default value according to the field type
Expand Down

0 comments on commit df42186

Please sign in to comment.