Skip to content

Commit 1fbc9a2

Browse files
committed
chore: reduce code duplication
1 parent ac12d94 commit 1fbc9a2

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

templates/swift/Sources/Models/Model.swift.twig

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,8 @@ open class {{ definition | modelType(spec) | raw }}: Codable {
8181
public static func from(map: [String: Any] ) -> {{ definition.name | caseUcfirst }} {
8282
return {{ definition.name | caseUcfirst }}(
8383
{%~ for property in definition.properties %}
84-
{%~ if definition.name | lower == 'document' %}
85-
{{ property.name | escapeSwiftKeyword | removeDollarSign }}: {% if property.sub_schema %}{% if property.type == 'array' %}(map["{{property.name }}"] as? [[String: Any]] ?? []).map { {{property.sub_schema | caseUcfirst}}.from(map: $0) }{% else %}map["{{property.name }}"] as? [String: Any] != nil ? {{property.sub_schema | caseUcfirst}}.from(map: map["{{property.name }}"] as! [String: Any]) : nil{% endif %}{% else %}{% if property | isAnyCodableArray(spec) %}(map["{{property.name }}"] as? [Any] ?? []).map { AnyCodable($0) }{% else %}map["{{property.name }}"] as? {{ property | propertyType(spec) | raw }}{% if property.required and property.type == 'string' %} ?? ""{% elseif property.required and property.type == 'integer' %} ?? 0{% elseif property.required and property.type == 'number' %} ?? 0.0{% elseif property.required and property.type == 'boolean' %} ?? false{% elseif property.required and property.type == 'array' %} ?? []{% endif %}{% endif %}{% endif %}{% if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %}
86-
{%~ else %}
87-
{{ property.name | escapeSwiftKeyword | removeDollarSign }}: {% if property.sub_schema %}{% if property.type == 'array' %}(map["{{property.name }}"] as! [[String: Any]]).map { {{property.sub_schema | caseUcfirst}}.from(map: $0) }{% else %}{{property.sub_schema | caseUcfirst}}.from(map: map["{{property.name }}"] as! [String: Any]){% endif %}{% else %}{% if property | isAnyCodableArray(spec) %}(map["{{property.name }}"] as{% if property.required %}!{% else %}?{% endif %} [Any]{% if not property.required %} ?? []{% endif %}).map { AnyCodable($0) }{% else %}map["{{property.name }}"] as{% if property.required %}!{% else %}?{% endif %} {{ property | propertyType(spec) | raw }}{% endif %}{% endif %}{% if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %}
88-
{%~ endif %}
84+
{%~ set isDocument = definition.name | lower == 'document' %}
85+
{{ property.name | escapeSwiftKeyword | removeDollarSign }}: {% if property.sub_schema %}{% if property.type == 'array' %}(map["{{property.name }}"] as{% if isDocument %}?{% else %}{% if property.required %}!{% else %}?{% endif %}{% endif %} [[String: Any]]{% if isDocument %} ?? []{% elseif not property.required %} ?? []{% endif %}).map { {{property.sub_schema | caseUcfirst}}.from(map: $0) }{% else %}{% if isDocument %}map["{{property.name }}"] as? [String: Any] != nil ? {{property.sub_schema | caseUcfirst}}.from(map: map["{{property.name }}"] as! [String: Any]) : nil{% else %}{{property.sub_schema | caseUcfirst}}.from(map: map["{{property.name }}"] as! [String: Any]){% endif %}{% endif %}{% else %}{% if property | isAnyCodableArray(spec) %}(map["{{property.name }}"] as{% if isDocument %}?{% else %}{% if property.required %}!{% else %}?{% endif %}{% endif %} [Any]{% if isDocument or not property.required %} ?? []{% endif %}).map { AnyCodable($0) }{% else %}map["{{property.name }}"] as{% if isDocument %}?{% else %}{% if property.required %}!{% else %}?{% endif %}{% endif %} {{ property | propertyType(spec) | raw }}{% if isDocument and property.required %}{% if property.type == 'string' %} ?? ""{% elseif property.type == 'integer' %} ?? 0{% elseif property.type == 'number' %} ?? 0.0{% elseif property.type == 'boolean' %} ?? false{% elseif property.type == 'array' %} ?? []{% endif %}{% endif %}{% endif %}{% endif %}{% if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %}
8986

9087
{%~ endfor %}
9188
{%~ if definition.additionalProperties %}

0 commit comments

Comments
 (0)