From 21b64a3500010605b54ae1c64ada47a1c2ec4eff Mon Sep 17 00:00:00 2001 From: Semprini Date: Sun, 19 May 2024 11:31:24 +1200 Subject: [PATCH] Fixes #78 --- mdg/templates/Python/dataclass_avro.py.jinja | 10 +++++----- sample_recipes/sparxdb/config-sparxdb-schema.yaml | 3 ++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/mdg/templates/Python/dataclass_avro.py.jinja b/mdg/templates/Python/dataclass_avro.py.jinja index cab8a60..f8077b0 100644 --- a/mdg/templates/Python/dataclass_avro.py.jinja +++ b/mdg/templates/Python/dataclass_avro.py.jinja @@ -4,13 +4,13 @@ import typing from datetime import date from dataclasses_avroschema import AvroModel - -{% macro do_enum(current_enum) -%} +{% set enums_done = [] %}{% set classes_done = [] %} +{% macro do_enum(current_enum) -%}{% if current_enum.id not in enums_done %}{{ enums_done.append( current_enum.id ) | default("", True) }} class {{ current_enum.name | case_class }}(enum.Enum):{% for attr in current_enum.values %} {{ attr | case_class | replace("/", "_") | replace("-", "_")}} = '{{ attr }}'{% endfor -%} -{% endmacro -%} +{% endif %}{% endmacro -%} -{% macro do_class(current_class, root=False) -%} +{% macro do_class(current_class, root=False) -%}{% if current_class.id not in classes_done %}{{ classes_done.append( current_class.id ) | default("", True) }} @dataclass class {{ current_class.name | case_class }}({% if current_class.generalization %}{{ current_class.generalization.name | case_class }}{% else %}AvroModel{% endif %}):{% if current_class.id_attribute.parent == current_class %} {{ current_class.id_attribute.name | snakecase }}: {{current_class.id_attribute.dest_type}}{% endif %} @@ -24,7 +24,7 @@ class {{ current_class.name | case_class }}({% if current_class.generalization % class Meta: namespace = "{{ current_class.package.name | snakecase }}" -{% endmacro -%} +{% endif %}{% endmacro -%} {% for cls in package.classes %}{% for attr in cls.attributes %}{% if attr.classification %} {{ do_enum(attr.classification) }} diff --git a/sample_recipes/sparxdb/config-sparxdb-schema.yaml b/sample_recipes/sparxdb/config-sparxdb-schema.yaml index 5f4ff3b..c5b81ca 100644 --- a/sample_recipes/sparxdb/config-sparxdb-schema.yaml +++ b/sample_recipes/sparxdb/config-sparxdb-schema.yaml @@ -5,7 +5,7 @@ source: sqlite:///./sample_recipes/sparxdb/sample.qea parser: sparxdb dest_root: ./build/sample_sparxdb_schema templates_folder: ./mdg/templates -default_dialect: default +default_dialect: python generation_artifacts: # Avro Schema - dest: "avro/{{cls.package.name}}.{{ cls.name }}.avsc" @@ -20,6 +20,7 @@ generation_artifacts: - dest: "python/{{package.name}}_dataclasses.py" level: package source: "Python/dataclass_avro.py.jinja" + filter: "{% if package.classes %}True{% else %}False{% endif %}" test_templates: - dest: ./build/json{{ins.package.path}}/{{ins.stereotype}}.json format: json