From 4969111f8c2b2c9b5736260832829fca8728d565 Mon Sep 17 00:00:00 2001 From: himanshu230987 <57001655+himanshu230987@users.noreply.github.com> Date: Fri, 2 Dec 2022 16:17:25 +0530 Subject: [PATCH 1/2] to allow creation of empty object sometimes it is possible that user doesn't have the dictionary ready. In such cases, they should be able to create empty object and user setters to set required values. --- avro_to_python/templates/files/record.j2 | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/avro_to_python/templates/files/record.j2 b/avro_to_python/templates/files/record.j2 index 7ab9a61..df8585d 100644 --- a/avro_to_python/templates/files/record.j2 +++ b/avro_to_python/templates/files/record.j2 @@ -2,8 +2,11 @@ class {{file.name}}(object): {% include 'partials/avroSchema.j2'|indent(-4) %} - def __init__(self, obj: Union[str, dict, '{{file.name}}']) -> None: - if isinstance(obj, str): + def __init__(self, obj: Union[str, dict, '{{file.name}}'] = None) -> None: + if obj is None: + return + + elif isinstance(obj, str): obj = json.loads(obj) elif isinstance(obj, type(self)): @@ -22,4 +25,4 @@ class {{file.name}}(object): {% for name, field in file.fields.items() -%} {% include 'fields/fieldFactory.j2' %} {% endfor -%} -{%- endblock -%} \ No newline at end of file +{%- endblock -%} From 155d4be402ec4db5f566d316ed7429544004ced9 Mon Sep 17 00:00:00 2001 From: himanshu230987 <57001655+himanshu230987@users.noreply.github.com> Date: Mon, 5 Dec 2022 18:26:09 +0530 Subject: [PATCH 2/2] Map field comes as dictionary not list --- avro_to_python/templates/fields/mapField.j2 | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/avro_to_python/templates/fields/mapField.j2 b/avro_to_python/templates/fields/mapField.j2 index 1826ecf..a62fc9f 100644 --- a/avro_to_python/templates/fields/mapField.j2 +++ b/avro_to_python/templates/fields/mapField.j2 @@ -1,6 +1,9 @@ {%- block map %} - def set_{{name}}(self, values: list) -> None: - + def set_{{name}}(self, values) -> None: + if isinstance(values, dict): + self.{{name}} = values + return + self.{{name}} = [] if isinstance(values, list): for item in values: @@ -60,6 +63,6 @@ else: raise TypeError("Field '{{name}}' should be type list") - def get_{{name}}(self) -> list: + def get_{{name}}(self): return self.{{name}} -{%- endblock -%} \ No newline at end of file +{%- endblock -%}