diff --git a/Swift/Class.swift.twig b/Swift/Class.swift.twig index 9105715..f8b9ad5 100644 --- a/Swift/Class.swift.twig +++ b/Swift/Class.swift.twig @@ -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 diff --git a/Swift/Enum.swift.twig b/Swift/Enum.swift.twig index 2593f0e..32a3095 100644 --- a/Swift/Enum.swift.twig +++ b/Swift/Enum.swift.twig @@ -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 } %} } diff --git a/Swift/blocks/class/classtype.twig b/Swift/blocks/class/classtype.twig deleted file mode 100644 index 285ae64..0000000 --- a/Swift/blocks/class/classtype.twig +++ /dev/null @@ -1,3 +0,0 @@ -{%- import '../../utils.twig' as utils -%} - -{{- type.baseTypeName -}}{%- include 'type-parameters.twig' with { typeParameters: type.typeParameters } -%} \ No newline at end of file diff --git a/Swift/blocks/class/date-transformers.twig b/Swift/blocks/class/date-transformers.twig index c56540e..e441fe3 100644 --- a/Swift/blocks/class/date-transformers.twig +++ b/Swift/blocks/class/date-transformers.twig @@ -1,4 +1,4 @@ -{%- import '../../utils.twig' as utils -%} +{%- import '../../macroses/common.utils.twig' as utils -%} {%- set hasDateFields = false -%} diff --git a/Swift/blocks/class/fields-initialization.twig b/Swift/blocks/class/fields-initialization.twig index 74ac950..098728a 100644 --- a/Swift/blocks/class/fields-initialization.twig +++ b/Swift/blocks/class/fields-initialization.twig @@ -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 -%} \ 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 6bc9093..e109ea5 100644 --- a/Swift/blocks/class/fields-mapping-from-map.twig +++ b/Swift/blocks/class/fields-mapping-from-map.twig @@ -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 -%} \ 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 b29a78b..98d9cc3 100644 --- a/Swift/blocks/class/fields-mapping-to-map.twig +++ b/Swift/blocks/class/fields-mapping-to-map.twig @@ -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 { diff --git a/Swift/blocks/class/fields-super-initialization.twig b/Swift/blocks/class/fields-super-initialization.twig index 88b762b..85c04c3 100644 --- a/Swift/blocks/class/fields-super-initialization.twig +++ b/Swift/blocks/class/fields-super-initialization.twig @@ -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 -%} \ No newline at end of file diff --git a/Swift/blocks/class/fields.twig b/Swift/blocks/class/fields.twig index d52adeb..65f95d9 100644 --- a/Swift/blocks/class/fields.twig +++ b/Swift/blocks/class/fields.twig @@ -1,4 +1,4 @@ -{%- import '../../utils.twig' as utils -%} +{%- import '../../macroses/common.utils.twig' as utils -%} {%- if fields is not empty -%} // MARK: - Fields diff --git a/Swift/blocks/class/init-parameters-fields.twig b/Swift/blocks/class/init-parameters-fields.twig index 23f4f0b..1d9e5c8 100644 --- a/Swift/blocks/class/init-parameters-fields.twig +++ b/Swift/blocks/class/init-parameters-fields.twig @@ -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 -%} diff --git a/Swift/blocks/class/supertype.twig b/Swift/blocks/class/supertype.twig deleted file mode 100644 index f38c9ec..0000000 --- a/Swift/blocks/class/supertype.twig +++ /dev/null @@ -1,3 +0,0 @@ -{%- import '../../utils.twig' as utils -%} - -{{- utils.parentClassType(parent) -}}{%- include 'type-parameters.twig' with { typeParameters: type.parentTypeParameters } -%} \ No newline at end of file diff --git a/Swift/blocks/class/type-parameters.twig b/Swift/blocks/class/type-parameters.twig deleted file mode 100644 index 1aea606..0000000 --- a/Swift/blocks/class/type-parameters.twig +++ /dev/null @@ -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 -%} \ No newline at end of file diff --git a/Swift/blocks/enum/cases.twig b/Swift/blocks/enum/cases.twig index 7758b30..9911919 100644 --- a/Swift/blocks/enum/cases.twig +++ b/Swift/blocks/enum/cases.twig @@ -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 %} diff --git a/Swift/blocks/method/method-func.twig b/Swift/blocks/method/method-func.twig index 85334cc..10a2de0 100644 --- a/Swift/blocks/method/method-func.twig +++ b/Swift/blocks/method/method-func.twig @@ -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) -%} diff --git a/Swift/macroses/class.utils.twig b/Swift/macroses/class.utils.twig new file mode 100644 index 0000000..4a36128 --- /dev/null +++ b/Swift/macroses/class.utils.twig @@ -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 %} diff --git a/Swift/macroses/common.utils.twig b/Swift/macroses/common.utils.twig new file mode 100644 index 0000000..3f2ade5 --- /dev/null +++ b/Swift/macroses/common.utils.twig @@ -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 %} diff --git a/Swift/macroses/enum.utils.twig b/Swift/macroses/enum.utils.twig new file mode 100644 index 0000000..b539f78 --- /dev/null +++ b/Swift/macroses/enum.utils.twig @@ -0,0 +1,7 @@ +{% macro enumType(valuesTypes) %} + {%- if valuesTypes == "STRING" -%} + String + {%- elseif valuesTypes == "INT" -%} + Int + {%- endif -%} +{% endmacro %} \ No newline at end of file diff --git a/Swift/utils.twig b/Swift/utils.twig deleted file mode 100644 index 3255629..0000000 --- a/Swift/utils.twig +++ /dev/null @@ -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 %}