Skip to content

Commit f2a58a0

Browse files
committed
Make swift models codable
1 parent f5f7a48 commit f2a58a0

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/SDK/Language/Swift.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@ public function getTypeName(array $parameter, array $spec = []): string
319319
self::TYPE_BOOLEAN => 'Bool',
320320
self::TYPE_ARRAY => (!empty(($parameter['array'] ?? [])['type']) && !\is_array($parameter['array']['type']))
321321
? '[' . $this->getTypeName($parameter['array']) . ']'
322-
: '[Any]',
323-
self::TYPE_OBJECT => 'Any',
322+
: '[AnyCodable]',
323+
self::TYPE_OBJECT => '[String: AnyCodable]',
324324
default => $parameter['type'],
325325
};
326326
}

templates/apple/Sources/Client.swift.twig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,6 @@ open class Client {
288288
case 0..<400:
289289
if response.headers["Set-Cookie"].count > 0 {
290290
let domain = URL(string: request.url)!.host!
291-
let existing = UserDefaults.standard.stringArray(forKey: domain)
292291
let new = response.headers["Set-Cookie"]
293292

294293
UserDefaults.standard.set(new, forKey: domain)

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ public class {{ definition | modelType(spec) | raw }} {}
77
{% else %}
88
public class {{ definition | modelType(spec) | raw }} {
99

10+
enum CodingKeys: String, CodingKey {
11+
{%~ for property in definition.properties %}
12+
case {{ property.name | escapeSwiftKeyword | removeDollarSign }} = "{{ property.name }}"
13+
{%~ endfor %}
14+
{%~ if definition.additionalProperties %}
15+
case data
16+
{%~ endif %}
17+
}
18+
1019
{%~ for property in definition.properties %}
1120
/// {{ property.description }}
1221
public let {{ property.name | escapeSwiftKeyword | removeDollarSign }}: {{ property | propertyType(spec) | raw }}{% if not property.required %}?{% endif %}
@@ -35,6 +44,28 @@ public class {{ definition | modelType(spec) | raw }} {
3544
{%~ endif %}
3645
}
3746

47+
public required init(from decoder: Decoder) throws {
48+
let container = try decoder.container(keyedBy: CodingKeys.self)
49+
50+
{%~ for property in definition.properties %}
51+
self.{{ property.name | escapeSwiftKeyword | removeDollarSign }} = try container.decode{% if not property.required %}IfPresent{% endif %}({{ property | propertyType(spec) | raw }}.self, forKey: .{{ property.name | escapeSwiftKeyword | removeDollarSign }})
52+
{%~ endfor %}
53+
{%~ if definition.additionalProperties %}
54+
self.data = try container.decode(T.self, forKey: .data)
55+
{%~ endif %}
56+
}
57+
58+
public func encode(to encoder: Encoder) throws {
59+
var container = encoder.container(keyedBy: CodingKeys.self)
60+
61+
{%~ for property in definition.properties %}
62+
try container.encode{% if not property.required %}IfPresent{% endif %}({{ property.name | escapeSwiftKeyword | removeDollarSign }}, forKey: .{{ property.name | escapeSwiftKeyword | removeDollarSign }})
63+
{%~ endfor %}
64+
{%~ if definition.additionalProperties %}
65+
try container.encode(data, forKey: .data)
66+
{%~ endif %}
67+
}
68+
3869
public func toMap() -> [String: Any] {
3970
return [
4071
{%~ for property in definition.properties %}

0 commit comments

Comments
 (0)