Universal mappable
This commit is contained in:
parent
d883cdd431
commit
d686c41cab
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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 -%}
|
||||
|
|
@ -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 -%}
|
||||
|
|
@ -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) -%}
|
||||
{{ field.name }}.encode(to: map, key: "{{ field.jsonName }}")
|
||||
{%- else -%}
|
||||
map["{{ field.jsonName }}"]
|
||||
{{ field.name }} >>> map["{{ field.jsonName }}"]
|
||||
{%- endif -%}
|
||||
{% endmacro %}
|
||||
|
||||
|
|
@ -131,3 +135,11 @@
|
|||
{%- endfor -%}
|
||||
{%- endif -%}
|
||||
{%- endmacro %}
|
||||
|
||||
{% macro fieldIsGeneric(field, typeParameters) %}
|
||||
{%- for typeParameter in typeParameters -%}
|
||||
{%- if typeParameter.baseTypeName == field.type.type.baseTypeName -%}
|
||||
{{ true }}
|
||||
{%- endif -%}
|
||||
{%- endfor -%}
|
||||
{% endmacro %}
|
||||
|
|
|
|||
Loading…
Reference in New Issue