diff --git a/Swift/Class.swift.twig b/Swift/Class.swift.twig index 01f4723..1082178 100644 --- a/Swift/Class.swift.twig +++ b/Swift/Class.swift.twig @@ -29,7 +29,8 @@ import LeadKit required init(map: Map) throws { {%- include 'blocks/class/fields-mapping-from-map.twig' with { fields: fields, - classType: classType + classType: classType, + typeParameters: type.typeParameters } -%} {% if hasParent %} try super.init(map: map) @@ -39,7 +40,8 @@ import LeadKit {% if hasParent %} override {% endif %}func mapping(map: Map) { {%- include 'blocks/class/fields-mapping-to-map.twig' with { fields: fields, - classType: classType + classType: classType, + typeParameters: type.typeParameters } -%} {% if hasParent %} super.mapping(map: map) @@ -53,12 +55,22 @@ import LeadKit } return{%- if hasParent %} super.isEqual(to: other){%- endif %}{%- if (fields is not empty) and hasParent %} &&{% endif %} {% include 'blocks/class/fields-equal.twig' with { fields: fields } %} - } + }{{ "\n" }} {%- endif %} +} +{{ "\n" }} +{%- if not hasChilds -%} +extension {{ type.baseTypeName }}: UniversalMappable { + + static func decode(from map: Map, key: String) throws -> {{ type.baseTypeName }} { + return try map.value(key) + } } +{{ "\n" }} +{%- endif -%} -{% if (not hasChilds) and (not hasGenerics) and (not fieldsHasGenericsOrNonEqutableCollections) -%} +{%- if (not hasChilds) and (not hasGenerics) and (not fieldsHasGenericsOrNonEqutableCollections) -%} extension {{ type.baseTypeName }}: Equatable { static func ==(lhs: {{ classType }}, rhs: {{ classType }}) -> Bool { diff --git a/Swift/blocks/class/fields-mapping-from-map.twig b/Swift/blocks/class/fields-mapping-from-map.twig index 6b2b9dd..4289fbd 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, classType) }} + {{ field.name }} = {{ utils.formatNullableOrOptional('try', field.nullable, field.optional) }} {{ utils.mappingFromMapForField(field, classType, typeParameters) }} {%- 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 253edae..7a85af4 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, classType) }} + {{ utils.mappingToMapForField(field, classType, typeParameters) }} } {%- else %} - {{ field.name }} >>> {{ utils.mappingToMapForField(field, classType) }} + {{ utils.mappingToMapForField(field, classType, typeParameters) }} {%- endif -%} {%- endfor -%} \ No newline at end of file diff --git a/Swift/macroses/class.utils.twig b/Swift/macroses/class.utils.twig index 91c81cb..ac14d4e 100644 --- a/Swift/macroses/class.utils.twig +++ b/Swift/macroses/class.utils.twig @@ -4,7 +4,7 @@ {%- if parameters is not empty -%} < {%- for typeParameter in parameters %} - {{- utils.formatValueType(typeParameter) -}}{{- withConstraint ? ": ImmutableMappable" : ""-}}{%- if not (loop.last) %}, {% endif %} + {{- utils.formatValueType(typeParameter) -}}{{- withConstraint ? ": UniversalMappable" : ""-}}{%- if not (loop.last) %}, {% endif %} {%- endfor -%} > {%- endif -%} diff --git a/Swift/macroses/common.utils.twig b/Swift/macroses/common.utils.twig index cd55fd8..6f27a7f 100644 --- a/Swift/macroses/common.utils.twig +++ b/Swift/macroses/common.utils.twig @@ -36,25 +36,29 @@ {%- endif -%} {% endmacro %} -{% macro mappingFromMapForField(field, className) %} +{% macro mappingFromMapForField(field, className, typeParameters) %} {%- import _self as self -%} {%- if field.type.type.baseTypeName == "DateTime" -%} map.value("{{ field.jsonName }}", using: {{ self.mappingTransformForField(field) -}}) {%- elseif field.type.type.baseTypeName == "Decimal" -%} map.value("{{ field.jsonName }}", using: NSDecimalNumberTransform()) + {%- elseif (self.fieldIsGeneric(field, typeParameters) != null) -%} + {{ field.type.type.baseTypeName }}.decode(from: map, key: "{{ field.jsonName }}") {%- else -%} map.value("{{ field.jsonName }}") {%- endif -%} {% endmacro %} -{% macro mappingToMapForField(field, className) %} +{% macro mappingToMapForField(field, className, typeParameters) %} {%- import _self as self -%} {%- if field.type.type.baseTypeName == "DateTime" -%} - (map["{{ field.jsonName }}"], {{ self.mappingTransformForField(field) -}}) + {{ field.name }} >>> (map["{{ field.jsonName }}"], {{ self.mappingTransformForField(field) -}}) + {%- elseif (self.fieldIsGeneric(field, typeParameters) != null) -%} + {{ self.formatNullableOrOptional(field.name, field.nullable, field.optional) }}.encode(to: map, key: "{{ field.jsonName }}") {%- else -%} - map["{{ field.jsonName }}"] + {{ field.name }} >>> map["{{ field.jsonName }}"] {%- endif -%} {% endmacro %} @@ -106,7 +110,7 @@ {%- elseif (valueType.parent is not null) -%} {%- import _self as self -%} - {{ self.valueTypeHasGenerics(valueType.parent) }} + {{- self.valueTypeHasGenerics(valueType.parent) -}} {%- endif -%} {% endmacro %} @@ -121,13 +125,21 @@ {%- if (valueType.itemsType.typeParameters is not null) -%} {{ true }} {%- else -%} - {{ self.hasGenericsOrNonEqutableCollections(valueType.itemsType) }} + {{- self.hasGenericsOrNonEqutableCollections(valueType.itemsType) -}} {%- endif -%} {%- elseif valueType.baseTypeName == "Map" -%} {{ true }} {%- elseif type.allFieldsOrdered is not null -%} {%- for field in type.allFieldsOrdered -%} - {{ self.hasGenericsOrNonEqutableCollections(field.type) }} + {{- self.hasGenericsOrNonEqutableCollections(field.type) -}} {%- endfor -%} {%- endif -%} {%- endmacro %} + +{% macro fieldIsGeneric(field, typeParameters) %} + {%- for typeParameter in typeParameters -%} + {%- if typeParameter.baseTypeName == field.type.type.baseTypeName -%} + {{ true }} + {%- endif -%} + {%- endfor -%} +{% endmacro %}