Merge pull request #3 from TouchInstinct/map-escaping

escape < and > in Map type formatting
This commit is contained in:
Gavriil 2017-08-08 13:37:36 +03:00 committed by GitHub
commit 0832578bad
20 changed files with 130 additions and 89 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
@ -34,3 +37,13 @@ import ObjectMapper
}
}
{% if not hasChilds %}
extension {{ type.baseTypeName }}: Equatable {
static func ==(lhs: {{ classType }}, rhs: {{ classType }}) -> Bool {
return {%- for field in allFieldsOrdered %} lhs.{{ field.name }} == rhs.{{ field.name }} {% if not (loop.last) -%} && {%- endif %}
{% endfor %}
}
}
{% endif -%}

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

@ -0,0 +1,21 @@
{%- import '../../macroses/common.utils.twig' as utils -%}
{%- macro hashOf(name, type, optional, nullable, ) -%}
{%- if type.baseTypeName == "Array" -%}
{#- implement hash for Array -#}
{%- elseif type.baseTypeName == "Map" -%}
{#- implement hash for Map -#}
{%- else -%}
{#- implement hash for regular value -#}
{%- endif -%}
{%- endmacro -%}
{%- for field in fields %}
{#- FIXME: Value of type '[T]' has no member 'hashValue'} -#}
{#- FIXME: The same for [TKey: TValue] -#}
{%- if field.optional or field.nullable %}
({{ field.name }}?.hashValue ?? 0)
{%- else %}
{{ field.name }}.hashValue
{%- endif -%}
{% if not (loop.last) %} ^ {%- endif %}
{%- endfor -%}

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,45 @@
{% 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
{%- elseif valueType.baseTypeName == "Long" -%}
Int64
{%- 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,61 +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
{%- elseif valueType.baseTypeName == "Long" -%}
Int64
{%- 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

@ -3,7 +3,7 @@
{%- import '../utils.twig' as utils -%}
{%- if valueType.baseTypeName == "Map" -%}
Map<{{- self.formatValueType(valueType.keysType, false, objectsLinks) -}}, {{- self.formatValueType(valueType.valuesType, false, objectsLinks) -}}>
Map&lt;{{- self.formatValueType(valueType.keysType, false, objectsLinks) -}}, {{- self.formatValueType(valueType.valuesType, false, objectsLinks) -}}&gt;
{%- elseif valueType.baseTypeName == "Array" -%}
{{- self.formatValueType(valueType.itemsType, false, objectsLinks) -}}[]
{%- else -%}