Skip to content

Commit b8f07c0

Browse files
committed
feat: complete codable for swift
1 parent 2e4b643 commit b8f07c0

File tree

1 file changed

+83
-2
lines changed

1 file changed

+83
-2
lines changed

templates/cli/lib/type-generation/languages/swift.js.twig

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,98 @@ class Swift extends LanguageMeta {
3838

3939
getTemplate() {
4040
return `import Foundation
41-
import Codable
4241

4342
public class <%- toPascalCase(collection.name) %>: Codable {
4443
<% for (const attribute of collection.attributes) { -%>
4544
public let <%- toCamelCase(attribute.key) %>: <%- getType(attribute) %>
4645
<% } %>
4746
enum CodingKeys: String, CodingKey {
4847
<% for (const attribute of collection.attributes) { -%>
49-
case <%- toCamelCase(attribute.key) %>
48+
case <%- toCamelCase(attribute.key) %> = "<%- attribute.key %>"
5049
<% } -%>
5150
}
51+
52+
init(
53+
<% for (const attribute of collection.attributes) { -%>
54+
<%- toCamelCase(attribute.key) %>: <%- getType(attribute) %><% if (attribute !== collection.attributes[collection.attributes.length - 1]) { %>,<% } %>
55+
<% } -%>
56+
) {
57+
<% for (const attribute of collection.attributes) { -%>
58+
self.<%- toCamelCase(attribute.key) %> = <%- toCamelCase(attribute.key) %>
59+
<% } -%>
60+
}
61+
62+
public required init(from decoder: Decoder) throws {
63+
let container = try decoder.container(keyedBy: CodingKeys.self)
64+
65+
<% for (const attribute of collection.attributes) { -%>
66+
<% if (attribute.required) { -%>
67+
self.<%- toCamelCase(attribute.key) %> = try container.decode(<%- getType(attribute).replace('?', '') %>.self, forKey: .<%- toCamelCase(attribute.key) %>)
68+
<% } else { -%>
69+
self.<%- toCamelCase(attribute.key) %> = try container.decodeIfPresent(<%- getType(attribute).replace('?', '') %>.self, forKey: .<%- toCamelCase(attribute.key) %>)
70+
<% } -%>
71+
<% } -%>
72+
}
73+
74+
public func encode(to encoder: Encoder) throws {
75+
var container = encoder.container(keyedBy: CodingKeys.self)
76+
77+
<% for (const attribute of collection.attributes) { -%>
78+
<% if (attribute.required) { -%>
79+
try container.encode(<%- toCamelCase(attribute.key) %>, forKey: .<%- toCamelCase(attribute.key) %>)
80+
<% } else { -%>
81+
try container.encodeIfPresent(<%- toCamelCase(attribute.key) %>, forKey: .<%- toCamelCase(attribute.key) %>)
82+
<% } -%>
83+
<% } -%>
84+
}
85+
86+
public func toMap() -> [String: Any] {
87+
return [
88+
<% for (const attribute of collection.attributes) { -%>
89+
<% if (attribute.type === 'relationship') { -%>
90+
"<%- attribute.key %>": <%- toCamelCase(attribute.key) %> as Any<% if (attribute !== collection.attributes[collection.attributes.length - 1]) { %>,<% } %>
91+
<% } else if (attribute.array && attribute.type !== 'string' && attribute.type !== 'integer' && attribute.type !== 'float' && attribute.type !== 'boolean') { -%>
92+
"<%- attribute.key %>": <%- toCamelCase(attribute.key) %>?.map { $0.toMap() } as Any<% if (attribute !== collection.attributes[collection.attributes.length - 1]) { %>,<% } %>
93+
<% } else { -%>
94+
"<%- attribute.key %>": <%- toCamelCase(attribute.key) %> as Any<% if (attribute !== collection.attributes[collection.attributes.length - 1]) { %>,<% } %>
95+
<% } -%>
96+
<% } -%>
97+
]
98+
}
99+
100+
public static func from(map: [String: Any]) -> <%- toPascalCase(collection.name) %> {
101+
return <%- toPascalCase(collection.name) %>(
102+
<% for (const attribute of collection.attributes) { -%>
103+
<% if (attribute.type === 'relationship') { -%>
104+
<%- toCamelCase(attribute.key) %>: map["<%- attribute.key %>"] as<% if (!attribute.required) { %>?<% } else { %>!<% } %> Map<String, Any><% if (attribute !== collection.attributes[collection.attributes.length - 1]) { %>,<% } %>
105+
<% } else if (attribute.array) { -%>
106+
<% if (attribute.type === 'string') { -%>
107+
<%- toCamelCase(attribute.key) %>: map["<%- attribute.key %>"] as<% if (!attribute.required) { %>?<% } else { %>!<% } %> [String]<% if (attribute !== collection.attributes[collection.attributes.length - 1]) { %>,<% } %>
108+
<% } else if (attribute.type === 'integer') { -%>
109+
<%- toCamelCase(attribute.key) %>: map["<%- attribute.key %>"] as<% if (!attribute.required) { %>?<% } else { %>!<% } %> [Int]<% if (attribute !== collection.attributes[collection.attributes.length - 1]) { %>,<% } %>
110+
<% } else if (attribute.type === 'float') { -%>
111+
<%- toCamelCase(attribute.key) %>: map["<%- attribute.key %>"] as<% if (!attribute.required) { %>?<% } else { %>!<% } %> [Double]<% if (attribute !== collection.attributes[collection.attributes.length - 1]) { %>,<% } %>
112+
<% } else if (attribute.type === 'boolean') { -%>
113+
<%- toCamelCase(attribute.key) %>: map["<%- attribute.key %>"] as<% if (!attribute.required) { %>?<% } else { %>!<% } %> [Bool]<% if (attribute !== collection.attributes[collection.attributes.length - 1]) { %>,<% } %>
114+
<% } else { -%>
115+
<%- toCamelCase(attribute.key) %>: (map["<%- attribute.key %>"] as<% if (!attribute.required) { %>?<% } else { %>!<% } %> [[String: Any]])<% if (!attribute.required) { %>?<% } %>.map { <%- toPascalCase(attribute.type) %>.from(map: $0) }<% if (attribute !== collection.attributes[collection.attributes.length - 1]) { %>,<% } %>
116+
<% } -%>
117+
<% } else { -%>
118+
<% if (attribute.type === 'string' || attribute.type === 'email' || attribute.type === 'datetime' || attribute.type === 'enum') { -%>
119+
<%- toCamelCase(attribute.key) %>: map["<%- attribute.key %>"] as<% if (!attribute.required) { %>?<% } else { %>!<% } %> String<% if (attribute !== collection.attributes[collection.attributes.length - 1]) { %>,<% } %>
120+
<% } else if (attribute.type === 'integer') { -%>
121+
<%- toCamelCase(attribute.key) %>: map["<%- attribute.key %>"] as<% if (!attribute.required) { %>?<% } else { %>!<% } %> Int<% if (attribute !== collection.attributes[collection.attributes.length - 1]) { %>,<% } %>
122+
<% } else if (attribute.type === 'float') { -%>
123+
<%- toCamelCase(attribute.key) %>: map["<%- attribute.key %>"] as<% if (!attribute.required) { %>?<% } else { %>!<% } %> Double<% if (attribute !== collection.attributes[collection.attributes.length - 1]) { %>,<% } %>
124+
<% } else if (attribute.type === 'boolean') { -%>
125+
<%- toCamelCase(attribute.key) %>: map["<%- attribute.key %>"] as<% if (!attribute.required) { %>?<% } else { %>!<% } %> Bool<% if (attribute !== collection.attributes[collection.attributes.length - 1]) { %>,<% } %>
126+
<% } else { -%>
127+
<%- toCamelCase(attribute.key) %>: <%- toPascalCase(attribute.type) %>.from(map: map["<%- attribute.key %>"] as! [String: Any])<% if (attribute !== collection.attributes[collection.attributes.length - 1]) { %>,<% } %>
128+
<% } -%>
129+
<% } -%>
130+
<% } -%>
131+
)
132+
}
52133
}`;
53134
}
54135

0 commit comments

Comments
 (0)