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

83 lines
3.4 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) -%}
{%- set classAndFieldsHaveNotGenericsOrNonEqutableCollections = (not hasGenerics) and (not fieldsHasGenericsOrNonEqutableCollections) -%}
{%- endfor -%}
import SwiftDate
import LeadKit
/// {{ description }}
public {% if (not hasChilds) -%}final {% endif %}class {{ classType }}: {{ parentClassType }} {
{% include 'blocks/class/coding-keys.twig' with { fields: fields } %}
{% include 'blocks/class/fields.twig' with { fields: fields } %}
// MARK: - Initializers
public {% 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 %}
}
{% if hasParent or fields is not empty %}
public required init(from decoder: Decoder) throws {
{%- if fields is not empty %}
{% include 'blocks/class/fields-initialization-from-decoder.twig' with { fields: fields} %}
{%- endif -%}
{% if hasParent %}
try super.init(from: decoder)
{%- endif %}
}
{% endif %}
public {% if hasParent -%}override {% endif %}func encode(to encoder: Encoder) throws {
{%- if fields is not empty %}
{% include 'blocks/class/fields-encode-to-encoder.twig' with { fields: fields} %}
{%- endif -%}
{% if hasParent %}
try super.encode(to: encoder)
{%- endif %}
}
{% if classAndFieldsHaveNotGenericsOrNonEqutableCollections -%}
public func isEqual(to other: {{ classType }}?) -> Bool {
{% if (fields is empty) and (not hasParent) %}
return false
{% else %}
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 } %} {% endif %}
}
{%- endif %}
}
{{ "\n" }}
{%- if (not hasChilds) and classAndFieldsHaveNotGenericsOrNonEqutableCollections -%}
extension {{ type.baseTypeName }}: Equatable {
public static func ==(lhs: {{ classType }}, rhs: {{ classType }}) -> Bool {
return lhs.isEqual(to: rhs)
}
}
{{ "\n" }}
{%- endif -%}
{%- if classAndFieldsHaveNotGenericsOrNonEqutableCollections -%}
extension {{ type.baseTypeName }} {
public static let new{{ type.baseTypeName }} = {{ type.baseTypeName }}({%- include 'blocks/class/fields-initialization-default-values.twig' with { fields: allFieldsOrdered } -%})
{% include 'blocks/class/copy-declaration.twig' with { hasChilds: hasChilds, type: type, fields: allFieldsOrdered } %}
}
{{ "\n" }}
{%- endif -%}