@@ -55,109 +55,109 @@ class Swift extends LanguageMeta {
55
55
<% if (attribute.format === ' enum' ) { -%>
56
56
public enum <% - toPascalCase(attribute.key) %> : String, Codable, CaseIterable {
57
57
<% for (const [index, element] of Object .entries(attribute.elements)) { -%>
58
- case <% - strict ? toCamelCase(element) : element %> = "<% - element %> "
58
+ case <% - strict ? toCamelCase(element) : element %> = "<% - element %> "
59
59
<% } -%>
60
60
}
61
61
62
62
<% } -%>
63
63
<% } -%>
64
64
public class <% - toPascalCase(collection.name) %> : Codable {
65
65
<% for (const attribute of collection.attributes) { -%>
66
- public let <% - strict ? toCamelCase(attribute.key) : attribute.key %> : <% - getType(attribute) %>
66
+ public let <% - strict ? toCamelCase(attribute.key) : attribute.key %> : <% - getType(attribute) %>
67
67
<% } %>
68
- enum CodingKeys: String, CodingKey {
68
+ enum CodingKeys: String, CodingKey {
69
69
<% for (const attribute of collection.attributes) { -%>
70
- case <% - strict ? toCamelCase(attribute.key) : attribute.key %> = "<% - attribute.key %> "
70
+ case <% - strict ? toCamelCase(attribute.key) : attribute.key %> = "<% - attribute.key %> "
71
71
<% } -%>
72
- }
72
+ }
73
73
74
- init(
74
+ init(
75
75
<% for (const [index, attribute] of Object .entries(collection.attributes)) { -%>
76
76
<% - strict ? toCamelCase(attribute.key) : attribute.key %> : <% - getType(attribute) %><% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
77
77
<% } -%>
78
- ) {
78
+ ) {
79
79
<% for (const attribute of collection.attributes) { -%>
80
80
self.<% - strict ? toCamelCase(attribute.key) : attribute.key %> = <% - strict ? toCamelCase(attribute.key) : attribute.key %>
81
81
<% } -%>
82
- }
82
+ }
83
83
84
- public required init(from decoder: Decoder) throws {
85
- let container = try decoder.container(keyedBy: CodingKeys.self)
84
+ public required init(from decoder: Decoder) throws {
85
+ let container = try decoder.container(keyedBy: CodingKeys.self)
86
86
87
87
<% for (const attribute of collection.attributes) { -%>
88
88
<% if (attribute.required) { -%>
89
- self.<% - strict ? toCamelCase(attribute.key) : attribute.key %> = try container.decode(<% - getType(attribute).replace(' ?' , ' ' ) %> .self, forKey: .<% - strict ? toCamelCase(attribute.key) : attribute.key %> )
89
+ self.<% - strict ? toCamelCase(attribute.key) : attribute.key %> = try container.decode(<% - getType(attribute).replace(' ?' , ' ' ) %> .self, forKey: .<% - strict ? toCamelCase(attribute.key) : attribute.key %> )
90
90
<% } else { -%>
91
- self.<% - strict ? toCamelCase(attribute.key) : attribute.key %> = try container.decodeIfPresent(<% - getType(attribute).replace(' ?' , ' ' ) %> .self, forKey: .<% - strict ? toCamelCase(attribute.key) : attribute.key %> )
91
+ self.<% - strict ? toCamelCase(attribute.key) : attribute.key %> = try container.decodeIfPresent(<% - getType(attribute).replace(' ?' , ' ' ) %> .self, forKey: .<% - strict ? toCamelCase(attribute.key) : attribute.key %> )
92
92
<% } -%>
93
93
<% } -%>
94
- }
94
+ }
95
95
96
- public func encode(to encoder: Encoder) throws {
97
- var container = encoder.container(keyedBy: CodingKeys.self)
96
+ public func encode(to encoder: Encoder) throws {
97
+ var container = encoder.container(keyedBy: CodingKeys.self)
98
98
99
99
<% for (const attribute of collection.attributes) { -%>
100
100
<% if (attribute.required) { -%>
101
- try container.encode(<% - strict ? toCamelCase(attribute.key) : attribute.key %> , forKey: .<% - strict ? toCamelCase(attribute.key) : attribute.key %> )
101
+ try container.encode(<% - strict ? toCamelCase(attribute.key) : attribute.key %> , forKey: .<% - strict ? toCamelCase(attribute.key) : attribute.key %> )
102
102
<% } else { -%>
103
- try container.encodeIfPresent(<% - strict ? toCamelCase(attribute.key) : attribute.key %> , forKey: .<% - strict ? toCamelCase(attribute.key) : attribute.key %> )
103
+ try container.encodeIfPresent(<% - strict ? toCamelCase(attribute.key) : attribute.key %> , forKey: .<% - strict ? toCamelCase(attribute.key) : attribute.key %> )
104
104
<% } -%>
105
105
<% } -%>
106
- }
106
+ }
107
107
108
- public func toMap() -> [String: Any] {
109
- return [
108
+ public func toMap() -> [String: Any] {
109
+ return [
110
110
<% for (const [index, attribute] of Object .entries(collection.attributes)) { -%>
111
111
<% if (attribute.type === ' relationship' ) { -%>
112
- "<% - attribute.key %> ": <% - strict ? toCamelCase(attribute.key) : attribute.key %> as Any<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
112
+ "<% - attribute.key %> ": <% - strict ? toCamelCase(attribute.key) : attribute.key %> as Any<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
113
113
<% } else if (attribute.array && attribute.type != = ' string' && attribute.type != = ' integer' && attribute.type != = ' float' && attribute.type != = ' boolean' ) { -%>
114
- "<% - attribute.key %> ": <% - strict ? toCamelCase(attribute.key) : attribute.key %> ?.map { $0.toMap() } as Any<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
114
+ "<% - attribute.key %> ": <% - strict ? toCamelCase(attribute.key) : attribute.key %> ?.map { $0.toMap() } as Any<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
115
115
<% } else { -%>
116
- "<% - attribute.key %> ": <% - strict ? toCamelCase(attribute.key) : attribute.key %> as Any<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
116
+ "<% - attribute.key %> ": <% - strict ? toCamelCase(attribute.key) : attribute.key %> as Any<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
117
117
<% } -%>
118
118
<% } -%>
119
- ]
120
- }
119
+ ]
120
+ }
121
121
122
- public static func from(map: [String: Any]) -> <% - toPascalCase(collection.name) %> {
123
- return <% - toPascalCase(collection.name) %> (
122
+ public static func from(map: [String: Any]) -> <% - toPascalCase(collection.name) %> {
123
+ return <% - toPascalCase(collection.name) %> (
124
124
<% for (const [index, attribute] of Object .entries(collection.attributes)) { -%>
125
125
<% if (attribute.type === ' relationship' ) { -%>
126
126
<% if ((attribute.relationType === ' oneToMany' && attribute.side === ' parent' ) || (attribute.relationType === ' manyToOne' && attribute.side === ' child' ) || attribute.relationType === ' manyToMany' ) { -%>
127
- <% - strict ? toCamelCase(attribute.key) : attribute.key %> : map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> [<% - toPascalCase(attribute.relatedCollection) %> ]<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
127
+ <% - strict ? toCamelCase(attribute.key) : attribute.key %> : map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> [<% - toPascalCase(attribute.relatedCollection) %> ]<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
128
128
<% } else { -%>
129
- <% - strict ? toCamelCase(attribute.key) : attribute.key %> : map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> <% - toPascalCase(attribute.relatedCollection) %><% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
129
+ <% - strict ? toCamelCase(attribute.key) : attribute.key %> : map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> <% - toPascalCase(attribute.relatedCollection) %><% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
130
130
<% } -%>
131
131
<% } else if (attribute.array) { -%>
132
132
<% if (attribute.type === ' string' ) { -%>
133
- <% - strict ? toCamelCase(attribute.key) : attribute.key %> : map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> [String]<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
133
+ <% - strict ? toCamelCase(attribute.key) : attribute.key %> : map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> [String]<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
134
134
<% } else if (attribute.type === ' integer' ) { -%>
135
- <% - strict ? toCamelCase(attribute.key) : attribute.key %> : map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> [Int]<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
135
+ <% - strict ? toCamelCase(attribute.key) : attribute.key %> : map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> [Int]<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
136
136
<% } else if (attribute.type === ' float' ) { -%>
137
- <% - strict ? toCamelCase(attribute.key) : attribute.key %> : map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> [Double]<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
137
+ <% - strict ? toCamelCase(attribute.key) : attribute.key %> : map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> [Double]<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
138
138
<% } else if (attribute.type === ' boolean' ) { -%>
139
- <% - strict ? toCamelCase(attribute.key) : attribute.key %> : map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> [Bool]<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
139
+ <% - strict ? toCamelCase(attribute.key) : attribute.key %> : map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> [Bool]<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
140
140
<% } else { -%>
141
- <% - strict ? toCamelCase(attribute.key) : attribute.key %> : (map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> [[String: Any]])<% if (!attribute.required) { %> ?<% } %> .map { <% - toPascalCase(attribute.type) %> .from(map: $0) }<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
141
+ <% - strict ? toCamelCase(attribute.key) : attribute.key %> : (map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> [[String: Any]])<% if (!attribute.required) { %> ?<% } %> .map { <% - toPascalCase(attribute.type) %> .from(map: $0) }<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
142
142
<% } -%>
143
143
<% } else { -%>
144
144
<% if ((attribute.type === ' string' || attribute.type === ' email' || attribute.type === ' datetime' ) && attribute.format != = ' enum' ) { -%>
145
- <% - strict ? toCamelCase(attribute.key) : attribute.key %> : map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> String<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
145
+ <% - strict ? toCamelCase(attribute.key) : attribute.key %> : map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> String<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
146
146
<% } else if (attribute.type === ' string' && attribute.format === ' enum' ) { -%>
147
- <% - strict ? toCamelCase(attribute.key) : attribute.key %> : <% - toPascalCase(attribute.key) %> (rawValue: map["<% - attribute.key %> "] as! String)!<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
147
+ <% - strict ? toCamelCase(attribute.key) : attribute.key %> : <% - toPascalCase(attribute.key) %> (rawValue: map["<% - attribute.key %> "] as! String)!<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
148
148
<% } else if (attribute.type === ' integer' ) { -%>
149
- <% - strict ? toCamelCase(attribute.key) : attribute.key %> : map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> Int<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
149
+ <% - strict ? toCamelCase(attribute.key) : attribute.key %> : map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> Int<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
150
150
<% } else if (attribute.type === ' float' ) { -%>
151
- <% - strict ? toCamelCase(attribute.key) : attribute.key %> : map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> Double<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
151
+ <% - strict ? toCamelCase(attribute.key) : attribute.key %> : map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> Double<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
152
152
<% } else if (attribute.type === ' boolean' ) { -%>
153
- <% - strict ? toCamelCase(attribute.key) : attribute.key %> : map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> Bool<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
153
+ <% - strict ? toCamelCase(attribute.key) : attribute.key %> : map["<% - attribute.key %> "] as<% if (!attribute.required) { %> ?<% } else { %> !<% } %> Bool<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
154
154
<% } else { -%>
155
- <% - strict ? toCamelCase(attribute.key) : attribute.key %> : <% - toPascalCase(attribute.type) %> .from(map: map["<% - attribute.key %> "] as! [String: Any])<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
155
+ <% - strict ? toCamelCase(attribute.key) : attribute.key %> : <% - toPascalCase(attribute.type) %> .from(map: map["<% - attribute.key %> "] as! [String: Any])<% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
156
156
<% } -%>
157
157
<% } -%>
158
158
<% } -%>
159
- )
160
- }
159
+ )
160
+ }
161
161
}`;
162
162
}
163
163
0 commit comments