swift templates refactoring

This commit is contained in:
Ivan Smolin 2017-06-30 17:02:31 +03:00
parent 1914d6f163
commit 67b5fe0809
18 changed files with 96 additions and 86 deletions

View File

@ -1,13 +1,16 @@
{%- import 'utils.twig' as utils -%}
{%- import 'macroses/common.utils.twig' as utils -%}
{%- import 'macroses/class.utils.twig' as classUtils -%}
{%- set hasParent = parent is not null -%}
{%- set classType = classUtils.classType(type) -%}
{%- set parentClassType = classUtils.parentClassType(parent, type.parentTypeParameters) -%}
import ObjectMapper
{%- include 'blocks/class/date-transformers.twig' with { fields: fields } %}
/// {{ description }}
{% if (not hasChilds) -%}final {% endif %}class {% include 'blocks/class/classtype.twig' with { type: type } %}: {% include 'blocks/class/supertype.twig' with { type: type, parent: parent } %} {
{% if (not hasChilds) -%}final {% endif %}class {{ classType }}: {{ parentClassType }} {
{% include 'blocks/class/fields.twig' with { fields: fields } %}
// MARK: - Initializers

View File

@ -1,4 +1,5 @@
{%- import 'utils.twig' as utils -%}
{%- import 'macroses/common.utils.twig' as utils -%}
{%- import 'macroses/enum.utils.twig' as enumutils -%}
import Foundation
@ -7,7 +8,7 @@ import Foundation
{% for value in values -%}
/// - {{ utils.decapitalize(value.name) }}: {{ value.description }}
{% endfor -%}
enum {{ name }}: {{ utils.enumType(valuesTypes) }} {
enum {{ name }}: {{ enumutils.enumType(valuesTypes) }} {
{% include 'blocks/enum/cases.twig' with { values: values } %}
}

View File

@ -1,3 +0,0 @@
{%- import '../../utils.twig' as utils -%}
{{- type.baseTypeName -}}{%- include 'type-parameters.twig' with { typeParameters: type.typeParameters } -%}

View File

@ -1,4 +1,4 @@
{%- import '../../utils.twig' as utils -%}
{%- import '../../macroses/common.utils.twig' as utils -%}
{%- set hasDateFields = false -%}

View File

@ -1,4 +1,4 @@
{%- import '../../utils.twig' as utils -%}
{%- import '../../macroses/common.utils.twig' as utils -%}
{%- for field in fields %}
self.{{ field.name }} = {{ field.name }}
{%- endfor -%}

View File

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

View File

@ -1,4 +1,4 @@
{%- import '../../utils.twig' as utils -%}
{%- import '../../macroses/common.utils.twig' as utils -%}
{%- for field in fields %}
{%- if field.optional %}
if {{ field.name }} != nil {

View File

@ -1,4 +1,4 @@
{%- import '../../utils.twig' as utils -%}
{%- import '../../macroses/common.utils.twig' as utils -%}
{%- for field in fields -%}
{{ field.name }}: {{ field.name }}{%- if not (loop.last) %}, {% endif %}
{%- endfor -%}

View File

@ -1,4 +1,4 @@
{%- import '../../utils.twig' as utils -%}
{%- import '../../macroses/common.utils.twig' as utils -%}
{%- if fields is not empty -%}
// MARK: - Fields

View File

@ -1,4 +1,4 @@
{%- import '../../utils.twig' as utils -%}
{%- import '../../macroses/common.utils.twig' as utils -%}
{%- if fields is not empty -%}
{%- for field in fields -%}

View File

@ -1,3 +0,0 @@
{%- import '../../utils.twig' as utils -%}
{{- utils.parentClassType(parent) -}}{%- include 'type-parameters.twig' with { typeParameters: type.parentTypeParameters } -%}

View File

@ -1,8 +0,0 @@
{%- import '../../utils.twig' as utils -%}
{%- if typeParameters is not empty -%}
<
{%- for typeParameter in typeParameters %}
{{- utils.formatValueType(typeParameter) -}}{%- if not (loop.last) %}, {% endif %}
{%- endfor -%}
>
{%- endif -%}

View File

@ -1,4 +1,4 @@
{%- import '../../utils.twig' as utils -%}
{%- import '../../macroses/common.utils.twig' as utils -%}
{%- for value in values -%}
{%- if valuesTypes == "STRING" %}
{%- if value.name != value.value %}

View File

@ -1,4 +1,4 @@
{%- import '../../utils.twig' as utils -%}
{%- import '../../macroses/common.utils.twig' as utils -%}
{%- set bodyParamName = utils.decapitalize(method.bodyType.type.typeName) -%}
{%- set funcName = utils.decapitalize(method.name) -%}

View File

@ -0,0 +1,29 @@
{% macro typeParameters(parameters) %}
{%- import '../macroses/common.utils.twig' as utils -%}
{%- if parameters is not empty -%}
<
{%- for typeParameter in parameters %}
{{- utils.formatValueType(typeParameter) -}}{%- if not (loop.last) %}, {% endif %}
{%- endfor -%}
>
{%- endif -%}
{% endmacro %}
{% macro classType(type) %}
{%- import _self as self -%}
{{- type.baseTypeName -}}{{- self.typeParameters(type.typeParameters) -}}
{% endmacro %}
{% macro parentClassType(parent, parentTypeParameters) %}
{%- import _self as self -%}
{%- if parent is not null %}
{{- parent.type.baseTypeName -}}
{%- else -%}
ImmutableMappable
{%- endif -%}
{{- self.typeParameters(parentTypeParameters) -}}
{% endmacro %}

View File

@ -0,0 +1,43 @@
{% macro decapitalize(text) %}
{{- concat(slice(text, 0, 1) | lower, slice(text, 1, text | length)) -}}
{% endmacro %}
{% macro formatNullableOrOptional(expr, nullable, optional) %}
{{- expr -}}{%- if nullable or optional -%}?{%- endif -%}
{% endmacro %}
{% macro formatValueType(valueType) %}
{%- if valueType.baseTypeName == "Array" -%}
[{{ valueType.itemsType.baseTypeName }}]
{%- elseif valueType.baseTypeName == "Map" -%}
[{{ valueType.keysType.baseTypeName }}: {{ valueType.valuesType.baseTypeName }}]
{%- elseif valueType.baseTypeName == "DateTime" -%}
Date
{%- else -%}
{{ valueType.baseTypeName }}
{%- endif -%}
{% endmacro %}
{% macro escapeIfNeeded(expr) %}
{%- if expr == "default" -%}
`{{ expr }}`
{%- else -%}
{{ expr }}
{%- endif -%}
{% endmacro %}
{% macro mappingFromMapForField(field) %}
{%- if field.type.type.baseTypeName == "DateTime" -%}
map.value("{{ field.jsonName }}", using: {{ field.name -}}Transform)
{%- else -%}
map.value("{{ field.jsonName }}")
{%- endif -%}
{% endmacro %}
{% macro mappingToMapForField(field) %}
{%- if field.type.type.baseTypeName == "DateTime" -%}
(map["{{ field.jsonName }}"], {{ field.name -}}Transform)
{%- else -%}
map["{{ field.jsonName }}"]
{%- endif -%}
{% endmacro %}

View File

@ -0,0 +1,7 @@
{% macro enumType(valuesTypes) %}
{%- if valuesTypes == "STRING" -%}
String
{%- elseif valuesTypes == "INT" -%}
Int
{%- endif -%}
{% endmacro %}

View File

@ -1,59 +0,0 @@
{% macro decapitalize(text) %}
{{- concat(slice(text, 0, 1) | lower, slice(text, 1, text | length)) -}}
{% endmacro %}
{% macro enumType(valuesTypes) %}
{%- if valuesTypes == "STRING" -%}
String
{%- elseif valuesTypes == "INT" -%}
Int
{%- endif -%}
{% endmacro %}
{% macro parentClassType(parent) %}
{%- if parent is not null %}
{{- parent.type.baseTypeName -}}
{%- else -%}
ImmutableMappable
{%- endif -%}
{% endmacro %}
{% macro formatNullableOrOptional(expr, nullable, optional) %}
{{- expr -}}{%- if nullable or optional -%}?{%- endif -%}
{% endmacro %}
{% macro formatValueType(valueType) %}
{%- if valueType.baseTypeName == "Array" -%}
[{{ valueType.itemsType.baseTypeName }}]
{%- elseif valueType.baseTypeName == "Map" -%}
[{{ valueType.keysType.baseTypeName }}: {{ valueType.valuesType.baseTypeName }}]
{%- elseif valueType.baseTypeName == "DateTime" -%}
Date
{%- else -%}
{{ valueType.baseTypeName }}
{%- endif -%}
{% endmacro %}
{% macro escapeIfNeeded(expr) %}
{%- if expr == "default" -%}
`{{ expr }}`
{%- else -%}
{{ expr }}
{%- endif -%}
{% endmacro %}
{% macro mappingFromMapForField(field) %}
{%- if field.type.type.baseTypeName == "DateTime" -%}
map.value("{{ field.jsonName }}", using: {{ field.name -}}Transform)
{%- else -%}
map.value("{{ field.jsonName }}")
{%- endif -%}
{% endmacro %}
{% macro mappingToMapForField(field) %}
{%- if field.type.type.baseTypeName == "DateTime" -%}
(map["{{ field.jsonName }}"], {{ field.name -}}Transform)
{%- else -%}
map["{{ field.jsonName }}"]
{%- endif -%}
{% endmacro %}