api-generator-templates/Swift/Class.swift.twig

95 lines
3.5 KiB
Twig

{%- 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) -%}
{%- set hasGenerics = utils.valueTypeHasGenerics(type) != null %}
{%- for field in allFieldsOrdered -%}
{%- set fieldsHasGenericsOrNonEqutableCollections = default(fieldsHasGenericsOrNonEqutableCollections, false) or (utils.hasGenericsOrNonEqutableCollections(field.type) != null) -%}
{%- endfor -%}
import ObjectMapper
import SwiftDate
import LeadKit
/// {{ description }}
{% if (not hasChilds) -%}final {% endif %}class {{ classType }}: {{ parentClassType }} {
{% include 'blocks/class/fields.twig' with { fields: fields } %}
// MARK: - Initializers
{% if (hasParent and (fields is empty)) %} override {% endif %}init({%- include 'blocks/class/init-parameters-fields.twig' with { fields: allFieldsOrdered } -%}) {
{%- include 'blocks/class/fields-initialization.twig' with { fields: fields } -%}
{% if hasParent %}
super.init({%- include 'blocks/class/fields-super-initialization.twig' with { fields: superclassesFields } -%})
{%- endif %}
}
required init(map: Map) throws {
{%- include 'blocks/class/fields-mapping-from-map.twig' with {
fields: fields,
classType: classType,
typeParameters: type.typeParameters
} -%}
{% 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,
classType: classType,
typeParameters: type.typeParameters
} -%}
{% if hasParent %}
super.mapping(map: map)
{%- endif %}
}
{% 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 %}{%- if (fields is not empty) and hasParent %} &&{% endif %} {% include 'blocks/class/fields-equal.twig' with { fields: fields } %}
}{{ "\n" }}
{%- endif %}
}
{{ "\n" }}
{%- if not hasChilds -%}
extension {{ type.baseTypeName }}: UniversalMappable {
static func decode(from map: Map, key: String) throws -> {{ type.baseTypeName }} {
return try map.value(key)
}
}
{{ "\n" }}
{%- endif -%}
{%- if (not hasChilds) and (not hasGenerics) and (not fieldsHasGenericsOrNonEqutableCollections) -%}
extension {{ type.baseTypeName }}: Equatable {
static func ==(lhs: {{ classType }}, rhs: {{ classType }}) -> Bool {
return lhs.isEqual(to: rhs)
}
}
{{ "\n" }}
{%- endif -%}
{%- if not hasGenerics -%}
extension {{ type.baseTypeName }} {
static let new{{ type.baseTypeName }} = {{ type.baseTypeName }}({%- include 'blocks/class/fields-initialization-default-values.twig' with { fields: allFieldsOrdered } -%})
func copy{%- if hasChilds -%}{{ type.baseTypeName }}{%- endif -%}With({%- include 'blocks/class/nullable-parameters-fields.twig' with { fields: allFieldsOrdered } -%}) -> {{ type.baseTypeName }} {
return {{ type.baseTypeName }}({%- include 'blocks/class/fields-optional-initialization.twig' with { fields: allFieldsOrdered } -%})
}
}
{{ "\n" }}
{%- endif -%}