add mapping to json

This commit is contained in:
Ivan Smolin 2017-06-13 12:21:28 +03:00
parent 1cd38f1973
commit 7d658e8891
4 changed files with 25 additions and 4 deletions

View File

@ -2,6 +2,7 @@
{%- set className = utils.formatClassType(type.baseTypeName, type.typeParameters) -%}
{%- set parentClassName = utils.formatClassType(utils.parentClassType(parent), type.parentTypeParameters) -%}
{%- set hasParent = parent is not null -%}
import ObjectMapper
@ -13,9 +14,17 @@ import ObjectMapper
// MARK: - Initializer
required init(map: Map) throws {
{%- include 'blocks/class/fields-mapping.twig' with { fields: fields } -%}
{% if parent is not null %}
{%- include 'blocks/class/fields-mapping-from-map.twig' with { fields: fields } -%}
{% if hasParent %}
try super.init(map: map)
{%- endif %}
}
{% if hasParent %} override {% endif %}func mapping(map: Map) {
{%- include 'blocks/class/fields-mapping-to-map.twig' with { fields: fields } -%}
{% if hasParent %}
super.mapping(map: map)
{%- endif %}
}
}

View File

@ -1,4 +1,4 @@
{%- import '../../utils.twig' as utils -%}
{%- for field in fields %}
{{ field.name }} = {{ utils.formatNullable('try', field.nullable) }} {{ utils.mappingForField(field, className) }}
{{ field.name }} = {{ utils.formatNullable('try', field.nullable) }} {{ utils.mappingFromMapForField(field, className) }}
{%- endfor -%}

View File

@ -0,0 +1,4 @@
{%- import '../../utils.twig' as utils -%}
{%- for field in fields %}
{{ field.name }} >>> {{ utils.mappingToMapForField(field, className) }}
{%- endfor -%}

View File

@ -41,10 +41,18 @@ Date
{%- endif -%}
{% endmacro %}
{% macro mappingForField(field, className) %}
{% macro mappingFromMapForField(field, className) %}
{%- if field.type.type.baseTypeName == "DateTime" -%}
map.value("{{ field.jsonName }}", using: {{ className -}}.{{- field.name -}}Transform)
{%- else -%}
map.value("{{ field.jsonName }}")
{%- endif -%}
{% endmacro %}
{% macro mappingToMapForField(field, className) %}
{%- if field.type.type.baseTypeName == "DateTime" -%}
(map["{{ field.jsonName }}"], {{ className -}}.{{- field.name -}}Transform)
{%- else -%}
map["{{ field.jsonName }}"]
{%- endif -%}
{% endmacro %}