add mapping to json
This commit is contained in:
parent
1cd38f1973
commit
7d658e8891
|
|
@ -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 %}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 -%}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
{%- import '../../utils.twig' as utils -%}
|
||||
{%- for field in fields %}
|
||||
{{ field.name }} >>> {{ utils.mappingToMapForField(field, className) }}
|
||||
{%- endfor -%}
|
||||
|
|
@ -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 %}
|
||||
|
|
|
|||
Loading…
Reference in New Issue