{% macro construct_function(property, source) %}
{{ property.class_info.name }}.from_dict({{ source }})
{% endmacro %}

{% from "property_templates/property_macros.py.jinja" import construct_template %}

{% macro construct(property, source) %}
{{ construct_template(construct_function, property, source) }}
{% endmacro %}

{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, dict){% endmacro %}

{% macro transform(property, source, destination, declare_type=True, multipart=False, transform_method="to_dict") %}
{% set transformed = source + "." + transform_method + "()" %}
{% if multipart %}
    {% set transformed = "(None, json.dumps(" + transformed + ").encode(), 'text/plain')" %}
    {% set type_string = property.get_type_string(multipart=True) %}
{% else %}
    {% set type_string = property.get_type_string(json=True) %}
{% endif %}
{% if property.required %}
{{ destination }} = {{ transformed }}
{%- else %}
{{ destination }}{% if declare_type %}: {{ type_string }}{% endif %} = UNSET
if not isinstance({{ source }}, Unset):
    {{ destination }} = {{ transformed }}
{%- endif %}
{% endmacro %}

{% macro transform_multipart(property, source, destination) %}
{{ transform(property, source, destination, transform_method="to_multipart") }}
{% endmacro %}
