diff --git a/Swift/Class.swift.twig b/Swift/Class.swift.twig index b9f29a7..42f0a76 100644 --- a/Swift/Class.swift.twig +++ b/Swift/Class.swift.twig @@ -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 %} diff --git a/Swift/blocks/class/date-transformers.twig b/Swift/blocks/class/date-transformers.twig index e441fe3..f9e4203 100644 --- a/Swift/blocks/class/date-transformers.twig +++ b/Swift/blocks/class/date-transformers.twig @@ -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 -%} \ No newline at end of file diff --git a/Swift/blocks/class/fields-mapping-from-map.twig b/Swift/blocks/class/fields-mapping-from-map.twig index e109ea5..6b2b9dd 100644 --- a/Swift/blocks/class/fields-mapping-from-map.twig +++ b/Swift/blocks/class/fields-mapping-from-map.twig @@ -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 -%} \ 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 index 98d9cc3..253edae 100644 --- a/Swift/blocks/class/fields-mapping-to-map.twig +++ b/Swift/blocks/class/fields-mapping-to-map.twig @@ -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 -%} \ No newline at end of file diff --git a/Swift/macroses/common.utils.twig b/Swift/macroses/common.utils.twig index 86ef83f..220b5e0 100644 --- a/Swift/macroses/common.utils.twig +++ b/Swift/macroses/common.utils.twig @@ -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 %}