Skip to content

Commit fcc2b74

Browse files
committed
refactor: add isAnyCodableArray filter
1 parent 761f789 commit fcc2b74

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/SDK/Language/Swift.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,9 @@ public function getFilters(): array
462462
new TwigFilter('propertyType', function (array $property, array $spec, string $generic = 'T') {
463463
return $this->getPropertyType($property, $spec, $generic);
464464
}),
465+
new TwigFilter('isAnyCodableArray', function (array $property, array $spec) {
466+
return $this->isAnyCodableArray($property, $spec);
467+
}),
465468
new TwigFilter('hasGenericType', function (string $model, array $spec) {
466469
return $this->hasGenericType($model, $spec);
467470
}),
@@ -533,6 +536,18 @@ protected function getPropertyType(array $property, array $spec, string $generic
533536
return $type;
534537
}
535538

539+
/**
540+
* Check if a property is an array that results in [AnyCodable] type
541+
*
542+
* @param array $property
543+
* @param array $spec
544+
* @return bool
545+
*/
546+
protected function isAnyCodableArray(array $property, array $spec): bool
547+
{
548+
return $property['type'] === 'array' && $this->getPropertyType($property, $spec, 'T') === '[AnyCodable]';
549+
}
550+
536551
protected function hasGenericType(?string $model, array $spec): string
537552
{
538553
if (empty($model) || $model === 'any') {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ 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-
{{ 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.type == 'array' and (property | propertyType(spec) | raw) == '[AnyCodable]' %}(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 %}
84+
{{ 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 %}
8585

8686
{%~ endfor %}
8787
{%~ if definition.additionalProperties %}

0 commit comments

Comments
 (0)