Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat swift codable models #1005

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
8 changes: 4 additions & 4 deletions src/SDK/Language/Swift.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public function getFiles(): array
* @param array $parameter
* @return string
*/
public function getTypeName(array $parameter, array $spec = []): string
public function getTypeName(array $parameter, array $spec = [], bool $isProperty = false): string
{
if (isset($parameter['enumName'])) {
return ($spec['title'] ?? '') . 'Enums.' . \ucfirst($parameter['enumName']);
Expand All @@ -319,8 +319,8 @@ public function getTypeName(array $parameter, array $spec = []): string
self::TYPE_BOOLEAN => 'Bool',
self::TYPE_ARRAY => (!empty(($parameter['array'] ?? [])['type']) && !\is_array($parameter['array']['type']))
? '[' . $this->getTypeName($parameter['array']) . ']'
: '[Any]',
self::TYPE_OBJECT => 'Any',
: '[AnyCodable]',
self::TYPE_OBJECT => $isProperty ? '[String: AnyCodable]' : 'Any',
default => $parameter['type'],
};
}
Expand Down Expand Up @@ -527,7 +527,7 @@ protected function getPropertyType(array $property, array $spec, string $generic
$type = '[' . $type . ']';
}
} else {
$type = $this->getTypeName($property);
$type = $this->getTypeName($property, isProperty: true);
}

return $type;
Expand Down
1 change: 0 additions & 1 deletion templates/apple/Sources/Client.swift.twig
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ open class Client {
case 0..<400:
if response.headers["Set-Cookie"].count > 0 {
let domain = URL(string: request.url)!.host!
let existing = UserDefaults.standard.stringArray(forKey: domain)
let new = response.headers["Set-Cookie"]

UserDefaults.standard.set(new, forKey: domain)
Expand Down
35 changes: 33 additions & 2 deletions templates/swift/Sources/Models/Model.swift.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@ import JSONCodable

/// {{ definition.description }}
{% if definition.properties | length == 0 and not definition.additionalProperties %}
public class {{ definition | modelType(spec) | raw }} {}
open class {{ definition | modelType(spec) | raw }}: Codable {}
{% else %}
public class {{ definition | modelType(spec) | raw }} {
open class {{ definition | modelType(spec) | raw }}: Codable {

enum CodingKeys: String, CodingKey {
{%~ for property in definition.properties %}
case {{ property.name | escapeSwiftKeyword | removeDollarSign }} = "{{ property.name }}"
{%~ endfor %}
{%~ if definition.additionalProperties %}
case data
{%~ endif %}
}

{%~ for property in definition.properties %}
/// {{ property.description }}
Expand Down Expand Up @@ -35,6 +44,28 @@ public class {{ definition | modelType(spec) | raw }} {
{%~ endif %}
}

public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

{%~ for property in definition.properties %}
self.{{ property.name | escapeSwiftKeyword | removeDollarSign }} = try container.decode{% if not property.required %}IfPresent{% endif %}({{ property | propertyType(spec) | raw }}.self, forKey: .{{ property.name | escapeSwiftKeyword | removeDollarSign }})
{%~ endfor %}
{%~ if definition.additionalProperties %}
self.data = try container.decode(T.self, forKey: .data)
{%~ endif %}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

{%~ for property in definition.properties %}
try container.encode{% if not property.required %}IfPresent{% endif %}({{ property.name | escapeSwiftKeyword | removeDollarSign }}, forKey: .{{ property.name | escapeSwiftKeyword | removeDollarSign }})
{%~ endfor %}
{%~ if definition.additionalProperties %}
try container.encode(data, forKey: .data)
{%~ endif %}
}

public func toMap() -> [String: Any] {
return [
{%~ for property in definition.properties %}
Expand Down
Loading