From 7d658e8891c9502e5c021bc96b6157b8d937fe1f Mon Sep 17 00:00:00 2001 From: Ivan Smolin Date: Tue, 13 Jun 2017 12:21:28 +0300 Subject: [PATCH] add mapping to json --- Swift/Class.swift.twig | 13 +++++++++++-- ...ds-mapping.twig => fields-mapping-from-map.twig} | 2 +- Swift/blocks/class/fields-mapping-to-map.twig | 4 ++++ Swift/utils.twig | 10 +++++++++- 4 files changed, 25 insertions(+), 4 deletions(-) rename Swift/blocks/class/{fields-mapping.twig => fields-mapping-from-map.twig} (63%) create mode 100644 Swift/blocks/class/fields-mapping-to-map.twig diff --git a/Swift/Class.swift.twig b/Swift/Class.swift.twig index 0815494..503bb31 100644 --- a/Swift/Class.swift.twig +++ b/Swift/Class.swift.twig @@ -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 %} + } + } diff --git a/Swift/blocks/class/fields-mapping.twig b/Swift/blocks/class/fields-mapping-from-map.twig similarity index 63% rename from Swift/blocks/class/fields-mapping.twig rename to Swift/blocks/class/fields-mapping-from-map.twig index 3315eaa..81d11f8 100644 --- a/Swift/blocks/class/fields-mapping.twig +++ b/Swift/blocks/class/fields-mapping-from-map.twig @@ -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 -%} \ No newline at end of file diff --git a/Swift/blocks/class/fields-mapping-to-map.twig b/Swift/blocks/class/fields-mapping-to-map.twig new file mode 100644 index 0000000..92f37d5 --- /dev/null +++ b/Swift/blocks/class/fields-mapping-to-map.twig @@ -0,0 +1,4 @@ +{%- import '../../utils.twig' as utils -%} + {%- for field in fields %} + {{ field.name }} >>> {{ utils.mappingToMapForField(field, className) }} +{%- endfor -%} \ No newline at end of file diff --git a/Swift/utils.twig b/Swift/utils.twig index a30aaad..61a9caf 100644 --- a/Swift/utils.twig +++ b/Swift/utils.twig @@ -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 %}