From 3640a5477f3b75971373c72feaed28458ef7b1ff Mon Sep 17 00:00:00 2001 From: Ivan Smolin Date: Wed, 4 Apr 2018 13:32:45 +0300 Subject: [PATCH] fix equtable, date formatting service and methods without body --- Swift/ApiDateFormattingService.swift.twig | 4 +-- Swift/Class.swift.twig | 16 +++++------ Swift/Enum.swift.twig | 1 + Swift/Methods.swift.twig | 1 + Swift/NetworkService.swift.twig | 1 + Swift/blocks/class/date-transformers.twig | 21 -------------- Swift/blocks/method/method-func.twig | 15 +++++++--- Swift/macroses/common.utils.twig | 34 ++++++++++++----------- 8 files changed, 42 insertions(+), 51 deletions(-) delete mode 100644 Swift/blocks/class/date-transformers.twig diff --git a/Swift/ApiDateFormattingService.swift.twig b/Swift/ApiDateFormattingService.swift.twig index a77bee5..8cf443b 100644 --- a/Swift/ApiDateFormattingService.swift.twig +++ b/Swift/ApiDateFormattingService.swift.twig @@ -10,8 +10,7 @@ import SwiftDate enum ApiDateFormat: String, LeadKit.DateFormat { {% for format in dateFormats -%} - {%- set classType = classUtils.classType(format.type) -%} - case {{ utils.dateFormatName(format.fieldName, classType) }} = "{{ format.dateFormat }}" + case {{ format.name }} = "{{ format.format }}" {% endfor %} var swiftDateFormat: SwiftDate.DateFormat { return SwiftDate.DateFormat.custom(rawValue) @@ -30,4 +29,5 @@ final class ApiDateFormattingService: DateFormattingService, Singleton { private init() {} } +{{ "\n" }} {%- endif -%} \ No newline at end of file diff --git a/Swift/Class.swift.twig b/Swift/Class.swift.twig index df170f8..01f4723 100644 --- a/Swift/Class.swift.twig +++ b/Swift/Class.swift.twig @@ -4,7 +4,7 @@ {%- set hasParent = parent is not null -%} {%- set classType = classUtils.classType(type) -%} {%- set parentClassType = classUtils.parentClassType(parent, type.parentTypeParameters) -%} -{%- set hasGenerics = (type.typeParameters is not empty) %} +{%- set hasGenerics = utils.valueTypeHasGenerics(type) != null %} {%- for field in allFieldsOrdered -%} {%- set fieldsHasGenericsOrNonEqutableCollections = default(fieldsHasGenericsOrNonEqutableCollections, false) or (utils.hasGenericsOrNonEqutableCollections(field.type) != null) -%} {%- endfor -%} @@ -13,8 +13,6 @@ import ObjectMapper import SwiftDate import LeadKit -{%- include 'blocks/class/date-transformers.twig' with { fields: fields, classType: classType } %} - /// {{ description }} {% if (not hasChilds) -%}final {% endif %}class {{ classType }}: {{ parentClassType }} { @@ -48,13 +46,13 @@ import LeadKit {%- endif %} } - {% if not fieldsHasGenericsOrNonEqutableCollections -%} + {% if (not hasGenerics) and (not fieldsHasGenericsOrNonEqutableCollections) -%} func isEqual(to other: {{ classType }}?) -> Bool { guard let other = other else { return false } - return{%- if hasParent %} super.isEqual(to: other) &&{%- endif %} {% include 'blocks/class/fields-equal.twig' with { fields: fields } %} + return{%- if hasParent %} super.isEqual(to: other){%- endif %}{%- if (fields is not empty) and hasParent %} &&{% endif %} {% include 'blocks/class/fields-equal.twig' with { fields: fields } %} } {%- endif %} @@ -68,9 +66,10 @@ extension {{ type.baseTypeName }}: Equatable { } } -{%- endif %} +{{ "\n" }} +{%- endif -%} -{% if not hasGenerics -%} +{%- if not hasGenerics -%} extension {{ type.baseTypeName }} { static let new{{ type.baseTypeName }} = {{ type.baseTypeName }}({%- include 'blocks/class/fields-initialization-default-values.twig' with { fields: allFieldsOrdered } -%}) @@ -80,4 +79,5 @@ extension {{ type.baseTypeName }} { } } -{%- endif %} +{{ "\n" }} +{%- endif -%} \ No newline at end of file diff --git a/Swift/Enum.swift.twig b/Swift/Enum.swift.twig index e868936..b8aed11 100644 --- a/Swift/Enum.swift.twig +++ b/Swift/Enum.swift.twig @@ -12,3 +12,4 @@ enum {{ name }}: {{ enumutils.enumType(valuesTypes) }}, RawRepresentable { {% include 'blocks/enum/cases.twig' with { values: values } %} } +{{ "\n" }} \ No newline at end of file diff --git a/Swift/Methods.swift.twig b/Swift/Methods.swift.twig index 7b84752..fb9472f 100644 --- a/Swift/Methods.swift.twig +++ b/Swift/Methods.swift.twig @@ -9,3 +9,4 @@ extension NetworkService { {% endfor %} } +{{ "\n" }} \ No newline at end of file diff --git a/Swift/NetworkService.swift.twig b/Swift/NetworkService.swift.twig index 0b74994..3938085 100644 --- a/Swift/NetworkService.swift.twig +++ b/Swift/NetworkService.swift.twig @@ -12,3 +12,4 @@ class {{ serviceName }}: NetworkService, ConfigurableNetworkService { } } +{{ "\n" }} \ No newline at end of file diff --git a/Swift/blocks/class/date-transformers.twig b/Swift/blocks/class/date-transformers.twig deleted file mode 100644 index 25d93de..0000000 --- a/Swift/blocks/class/date-transformers.twig +++ /dev/null @@ -1,21 +0,0 @@ -{%- import '../../macroses/common.utils.twig' as utils -%} - -{%- set hasDateFields = false -%} - -{%- for field in fields -%} - {%- if field.type.type.baseTypeName == "DateTime" -%} - {%- set hasDateFields = true -%} - {%- endif -%} -{%- endfor -%} - -{%- if hasDateFields -%} -{{ "\n" }} -// MARK: - Date transformers -{{ "\n" }} - -{%- for field in fields -%} -{% if field.type.type.baseTypeName == "DateTime" -%} -private let {{ utils.dateTransformName(field.name, classType) }} = ApiDateFormattingService.mappingTransform(with: .{{ utils.dateFormatName(field.name, classType) }}) -{% endif -%} -{%- endfor -%} -{%- endif -%} \ No newline at end of file diff --git a/Swift/blocks/method/method-func.twig b/Swift/blocks/method/method-func.twig index 10a2de0..075ffee 100644 --- a/Swift/blocks/method/method-func.twig +++ b/Swift/blocks/method/method-func.twig @@ -1,16 +1,23 @@ {%- import '../../macroses/common.utils.twig' as utils -%} -{%- set bodyParamName = utils.decapitalize(method.bodyType.type.typeName) -%} +{%- if (method.bodyType is not null) -%} + {%- set bodyParamName = utils.decapitalize(method.bodyType.type.typeName) -%} + {%- set bodyTypeName = method.bodyType.type.typeName -%} + + {%- set hasBody = true -%} +{%- endif -%} + +{%- set methodType = default(method.type, "POST") | lower -%} {%- set funcName = utils.decapitalize(method.name) -%} /// {{ method.description }} - func {{ funcName }}({{ bodyParamName }}: {{ method.bodyType.type.typeName }}, + func {{ funcName }}({%- if hasBody -%}{{ bodyParamName }}: {{ bodyTypeName }},{{ "\n " }}{%- endif -%} encoding: ParameterEncoding = URLEncoding.default, headers: HTTPHeaders? = nil) -> Single<{{ method.responseType.type.typeName }}> { let parameters = ApiRequestParameters(url: "{{ method.url }}", - method: .post, - parameters: {{ bodyParamName }}.toJSON(), + method: .{{ methodType }}, + parameters: {% if hasBody -%}{{ bodyParamName }}.toJSON(){%- else -%}nil{%- endif -%}, encoding: encoding, headers: headers) diff --git a/Swift/macroses/common.utils.twig b/Swift/macroses/common.utils.twig index 1541a9d..58d197e 100644 --- a/Swift/macroses/common.utils.twig +++ b/Swift/macroses/common.utils.twig @@ -40,7 +40,7 @@ {%- import _self as self -%} {%- if field.type.type.baseTypeName == "DateTime" -%} - map.value("{{ field.jsonName }}", using: {{ self.dateTransformName(field.name, className) -}}) + map.value("{{ field.jsonName }}", using: {{ self.mappingTransformForField(field) -}}) {%- elseif field.type.type.baseTypeName == "Decimal" -%} map.value("{{ field.jsonName }}", using: NSDecimalNumberTransform()) {%- else -%} @@ -52,23 +52,15 @@ {%- import _self as self -%} {%- if field.type.type.baseTypeName == "DateTime" -%} - (map["{{ field.jsonName }}"], {{ self.dateTransformName(field.name, className) -}}) + (map["{{ field.jsonName }}"], {{ self.mappingTransformForField(field) -}}) {%- else -%} map["{{ field.jsonName }}"] {%- endif -%} {% endmacro %} -{% macro dateTransformName(fieldName, className) %} - {%- import _self as self -%} - - {{ self.decapitalize(className) }}{{ self.capitalize(fieldName) -}}Transform -{%- endmacro %} - -{% macro dateFormatName(fieldName, className) %} - {%- import _self as self -%} - - {{ self.decapitalize(className) }}{{ self.capitalize(fieldName) -}}Format -{%- endmacro %} +{% macro mappingTransformForField(field) %} + ApiDateFormattingService.mappingTransform(with: .{{ dateFormatToName(field.type.dateFormat) }}) +{% endmacro %} {% macro enumValueName(value) %} {%- import _self as self -%} @@ -108,10 +100,22 @@ {%- endif -%} {%- endmacro %} +{% macro valueTypeHasGenerics(valueType) %} + {%- if valueType.typeParameters is not empty -%} + {{ true }} + {%- elseif (valueType.parent is not null) -%} + {%- import _self as self -%} + + self.valueTypeHasGenerics(valueType.parent) + {%- endif -%} +{% endmacro %} + {% macro hasGenericsOrNonEqutableCollections(type) %} {%- set valueType = type.type -%} - {%- if (valueType.typeParameters is not empty) -%} + {%- import _self as self -%} + + {%- if self.valueTypeHasGenerics(valueType) != null -%} {{ true }} {%- elseif valueType.baseTypeName == "Array" -%} {%- if valueType.itemsType.parent is not null -%} @@ -122,8 +126,6 @@ {%- elseif valueType.baseTypeName == "Map" -%} {{ true }} {%- elseif type.allFieldsOrdered is not null -%} - {%- import _self as self -%} - {%- for field in type.allFieldsOrdered -%} {{ self.hasGenericsOrNonEqutableCollections(field.type) }} {%- endfor -%}