fix duplicate date transform in single file mode

This commit is contained in:
Ivan Smolin 2018-01-10 22:25:28 +03:00
parent 026184d552
commit 76bdacb777
5 changed files with 31 additions and 11 deletions

View File

@ -7,7 +7,7 @@
import ObjectMapper
{%- include 'blocks/class/date-transformers.twig' with { fields: fields } %}
{%- include 'blocks/class/date-transformers.twig' with { fields: fields, classType: classType } %}
/// {{ description }}
{% if (not hasChilds) -%}final {% endif %}class {{ classType }}: {{ parentClassType }} {
@ -23,14 +23,20 @@ import ObjectMapper
}
required init(map: Map) throws {
{%- include 'blocks/class/fields-mapping-from-map.twig' with { fields: fields } -%}
{%- include 'blocks/class/fields-mapping-from-map.twig' with {
fields: fields,
classType: classType
} -%}
{% 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 } -%}
{%- include 'blocks/class/fields-mapping-to-map.twig' with {
fields: fields,
classType: classType
} -%}
{% if hasParent %}
super.mapping(map: map)
{%- endif %}

View File

@ -15,7 +15,7 @@
{%- for field in fields -%}
{% if field.type.type.baseTypeName == "DateTime" -%}
private let {{ field.name }}Transform = ApiDateFormattingService.shared.mappingTransform(for: "{{ field.type.dateFormat }}")
private let {{ utils.dateTransformName(field.name, classType) }} = ApiDateFormattingService.shared.mappingTransform(for: "{{ field.type.dateFormat }}")
{% endif -%}
{%- endfor -%}
{%- endif -%}

View File

@ -1,4 +1,4 @@
{%- import '../../macroses/common.utils.twig' as utils -%}
{%- for field in fields %}
{{ field.name }} = {{ utils.formatNullableOrOptional('try', field.nullable, field.optional) }} {{ utils.mappingFromMapForField(field) }}
{{ field.name }} = {{ utils.formatNullableOrOptional('try', field.nullable, field.optional) }} {{ utils.mappingFromMapForField(field, classType) }}
{%- endfor -%}

View File

@ -2,9 +2,9 @@
{%- for field in fields %}
{%- if field.optional %}
if {{ field.name }} != nil {
{{ field.name }} >>> {{ utils.mappingToMapForField(field) }}
{{ field.name }} >>> {{ utils.mappingToMapForField(field, classType) }}
}
{%- else %}
{{ field.name }} >>> {{ utils.mappingToMapForField(field) }}
{{ field.name }} >>> {{ utils.mappingToMapForField(field, classType) }}
{%- endif -%}
{%- endfor -%}

View File

@ -2,6 +2,10 @@
{{- concat(slice(text, 0, 1) | lower, slice(text, 1, text | length)) -}}
{% endmacro %}
{% macro capitalize(text) %}
{{- concat(slice(text, 0, 1) | upper, slice(text, 1, text | length)) -}}
{% endmacro %}
{% macro formatNullableOrOptional(expr, nullable, optional) %}
{{- expr -}}{%- if nullable or optional -%}?{%- endif -%}
{% endmacro %}
@ -32,9 +36,11 @@
{%- endif -%}
{% endmacro %}
{% macro mappingFromMapForField(field) %}
{% macro mappingFromMapForField(field, className) %}
{%- import _self as self -%}
{%- if field.type.type.baseTypeName == "DateTime" -%}
map.value("{{ field.jsonName }}", using: {{ field.name -}}Transform)
map.value("{{ field.jsonName }}", using: {{ self.dateTransformName(field.name, className) -}})
{%- elseif field.type.type.baseTypeName == "Decimal" -%}
map.value("{{ field.jsonName }}", using: NSDecimalNumberTransform())
{%- else -%}
@ -42,10 +48,18 @@
{%- endif -%}
{% endmacro %}
{% macro mappingToMapForField(field) %}
{% macro mappingToMapForField(field, className) %}
{%- import _self as self -%}
{%- if field.type.type.baseTypeName == "DateTime" -%}
(map["{{ field.jsonName }}"], {{ field.name -}}Transform)
(map["{{ field.jsonName }}"], {{ self.dateTransformName(field.name, className) -}})
{%- else -%}
map["{{ field.jsonName }}"]
{%- endif -%}
{% endmacro %}
{% macro dateTransformName(fieldName, className) %}
{%- import _self as self -%}
{{ self.decapitalize(className) }}{{ self.capitalize(fieldName) -}}Transform
{%- endmacro %}