Skip to content

Commit 09bc721

Browse files
authored
Merge pull request #1005 from appwrite/feat-swift-codable-models
Feat swift codable models
2 parents d288013 + 1b62d9c commit 09bc721

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

src/SDK/Language/Swift.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ public function getFiles(): array
299299
* @param array $parameter
300300
* @return string
301301
*/
302-
public function getTypeName(array $parameter, array $spec = []): string
302+
public function getTypeName(array $parameter, array $spec = [], bool $isProperty = false): string
303303
{
304304
if (isset($parameter['enumName'])) {
305305
return ($spec['title'] ?? '') . 'Enums.' . \ucfirst($parameter['enumName']);
@@ -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 => $isProperty ? '[String: AnyCodable]' : 'Any',
324324
default => $parameter['type'],
325325
};
326326
}
@@ -527,7 +527,7 @@ protected function getPropertyType(array $property, array $spec, string $generic
527527
$type = '[' . $type . ']';
528528
}
529529
} else {
530-
$type = $this->getTypeName($property);
530+
$type = $this->getTypeName($property, isProperty: true);
531531
}
532532

533533
return $type;

templates/apple/Sources/Client.swift.twig

-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@ open class Client {
310310
case 0..<400:
311311
if response.headers["Set-Cookie"].count > 0 {
312312
let domain = URL(string: request.url)!.host!
313-
let existing = UserDefaults.standard.stringArray(forKey: domain)
314313
let new = response.headers["Set-Cookie"]
315314

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

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

+33-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@ import JSONCodable
33

44
/// {{ definition.description }}
55
{% if definition.properties | length == 0 and not definition.additionalProperties %}
6-
public class {{ definition | modelType(spec) | raw }} {}
6+
open class {{ definition | modelType(spec) | raw }}: Codable {}
77
{% else %}
8-
public class {{ definition | modelType(spec) | raw }} {
8+
open class {{ definition | modelType(spec) | raw }}: Codable {
9+
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+
}
918

1019
{%~ for property in definition.properties %}
1120
/// {{ property.description }}
@@ -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)