Skip to content

Commit 12ddafe

Browse files
committed
Rework extra Info de/encoding
1 parent fe3501f commit 12ddafe

File tree

7 files changed

+54
-113
lines changed

7 files changed

+54
-113
lines changed

Sources/MatrixClient/API/Auth/Interactive.swift

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -140,40 +140,25 @@ public struct MatrixInteractiveAuthResponse: Codable {
140140
}
141141

142142
public extension MatrixInteractiveAuthResponse {
143-
private enum KnownCodingKeys: String, CodingKey, CaseIterable {
143+
private enum KnownCodingKeys: String, MatrixKnownCodingKeys {
144144
case session
145145
case type
146146

147-
static func doesNotContain(_ key: DynamicCodingKeys) -> Bool {
148-
!Self.allCases.map(\.stringValue).contains(key.stringValue)
149-
}
150147
}
151148

152-
internal struct DynamicCodingKeys: CodingKey {
153-
var stringValue: String
154-
init?(stringValue: String) {
155-
self.stringValue = stringValue
156-
}
157-
158-
// not used here, but a protocol requirement
159-
var intValue: Int?
160-
init?(intValue _: Int) {
161-
nil
162-
}
163-
}
164149

165150
init(from decoder: Decoder) throws {
166151
let container = try decoder.container(keyedBy: KnownCodingKeys.self)
167152
session = try container.decodeIfPresent(String.self, forKey: .session)
168153
type = try container.decode(MatrixLoginFlowType.self, forKey: .type)
169154

170155
extraInfo = [:]
171-
let extraContainer = try decoder.container(keyedBy: DynamicCodingKeys.self)
156+
let extraContainer = try decoder.container(keyedBy: MatrixDynamicCodingKeys.self)
172157

173158
for key in extraContainer.allKeys where KnownCodingKeys.doesNotContain(key) {
174159
let decoded = try extraContainer.decode(
175160
AnyCodable.self,
176-
forKey: DynamicCodingKeys(stringValue: key.stringValue)!
161+
forKey: MatrixDynamicCodingKeys(stringValue: key.stringValue)!
177162
)
178163
self.extraInfo[key.stringValue] = decoded
179164
}
@@ -184,7 +169,7 @@ public extension MatrixInteractiveAuthResponse {
184169
try container.encodeIfPresent(session, forKey: .session)
185170
try container.encodeIfPresent(type?.rawValue, forKey: .type)
186171

187-
var extraContainer = encoder.container(keyedBy: DynamicCodingKeys.self)
172+
var extraContainer = encoder.container(keyedBy: MatrixDynamicCodingKeys.self)
188173
for (name, value) in extraInfo {
189174
try extraContainer.encode(value, forKey: .init(stringValue: name)!)
190175
}

Sources/MatrixClient/API/Auth/Login.swift

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -223,26 +223,9 @@ public struct MatrixLoginFlow {
223223
}
224224

225225
extension MatrixLoginFlow: Codable {
226-
private enum KnownCodingKeys: String, CodingKey, CaseIterable {
226+
private enum KnownCodingKeys: String, MatrixKnownCodingKeys {
227227
case type
228228
case identiyProviders = "identity_providers"
229-
230-
static func doesNotContain(_ key: DynamicCodingKeys) -> Bool {
231-
!Self.allCases.map(\.stringValue).contains(key.stringValue)
232-
}
233-
}
234-
235-
struct DynamicCodingKeys: CodingKey {
236-
var stringValue: String
237-
init?(stringValue: String) {
238-
self.stringValue = stringValue
239-
}
240-
241-
// not used here, but a protocol requirement
242-
var intValue: Int?
243-
init?(intValue _: Int) {
244-
nil
245-
}
246229
}
247230

248231
public init(from decoder: Decoder) throws {
@@ -251,12 +234,12 @@ extension MatrixLoginFlow: Codable {
251234
identiyProviders = try container.decodeIfPresent([IdentityProvider].self, forKey: .identiyProviders)
252235

253236
extraInfo = [:]
254-
let extraContainer = try decoder.container(keyedBy: DynamicCodingKeys.self)
237+
let extraContainer = try decoder.container(keyedBy: MatrixDynamicCodingKeys.self)
255238

256239
for key in extraContainer.allKeys where KnownCodingKeys.doesNotContain(key) {
257240
let decoded = try extraContainer.decode(
258241
AnyCodable.self,
259-
forKey: DynamicCodingKeys(stringValue: key.stringValue)!
242+
forKey: MatrixDynamicCodingKeys(stringValue: key.stringValue)!
260243
)
261244
self.extraInfo[key.stringValue] = decoded
262245
}
@@ -267,7 +250,7 @@ extension MatrixLoginFlow: Codable {
267250
try container.encode(type, forKey: .type)
268251
try container.encodeIfPresent(identiyProviders, forKey: .identiyProviders)
269252

270-
var extraContainer = encoder.container(keyedBy: DynamicCodingKeys.self)
253+
var extraContainer = encoder.container(keyedBy: MatrixDynamicCodingKeys.self)
271254
for (name, value) in extraInfo {
272255
try extraContainer.encode(value, forKey: .init(stringValue: name)!)
273256
}

Sources/MatrixClient/API/Capability.swift

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -307,29 +307,12 @@ public extension MatrixCapabilities {
307307
// MARK: - Codable
308308

309309
extension MatrixCapabilities.Capabilities {
310-
enum KnownCodingKeys: String, CodingKey, CaseIterable {
310+
enum KnownCodingKeys: String, MatrixKnownCodingKeys {
311311
case changePassword = "m.change_password"
312312
case roomVersions = "m.room_versions"
313313
case setDisplayName = "m.set_displayname"
314314
case setAvatarUrl = "m.set_avatar_url"
315315
case change3Pid = "m.3pid_changes"
316-
317-
static func doesNotContain(_ key: DynamicCodingKeys) -> Bool {
318-
!Self.allCases.map(\.stringValue).contains(key.stringValue)
319-
}
320-
}
321-
322-
struct DynamicCodingKeys: CodingKey {
323-
var stringValue: String
324-
init?(stringValue: String) {
325-
self.stringValue = stringValue
326-
}
327-
328-
// not used here, but a protocol requirement
329-
var intValue: Int?
330-
init?(intValue _: Int) {
331-
nil
332-
}
333316
}
334317

335318
public init(from decoder: Decoder) throws {
@@ -338,12 +321,12 @@ extension MatrixCapabilities.Capabilities {
338321
roomVersions = try container.decodeIfPresent(RoomVersionsCapability.self, forKey: .roomVersions)
339322

340323
extraInfo = [:]
341-
let extraContainer = try decoder.container(keyedBy: DynamicCodingKeys.self)
324+
let extraContainer = try decoder.container(keyedBy: MatrixDynamicCodingKeys.self)
342325

343326
for key in extraContainer.allKeys where KnownCodingKeys.doesNotContain(key) {
344327
let decoded = try extraContainer.decode(
345328
AnyCodable.self,
346-
forKey: DynamicCodingKeys(stringValue: key.stringValue)!
329+
forKey: .init(stringValue: key.stringValue)!
347330
)
348331
self.extraInfo[key.stringValue] = decoded
349332
}
@@ -354,7 +337,7 @@ extension MatrixCapabilities.Capabilities {
354337
try container.encodeIfPresent(changePassword, forKey: .changePassword)
355338
try container.encodeIfPresent(roomVersions, forKey: .roomVersions)
356339

357-
var extraContainer = encoder.container(keyedBy: DynamicCodingKeys.self)
340+
var extraContainer = encoder.container(keyedBy: MatrixDynamicCodingKeys.self)
358341
for (name, value) in extraInfo {
359342
try extraContainer.encode(value, forKey: .init(stringValue: name)!)
360343
}

Sources/MatrixClient/API/HomeServer.swift

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -180,26 +180,10 @@ public struct MatrixWellKnown: MatrixResponse {
180180
}
181181

182182
extension MatrixWellKnown: Codable {
183-
private enum KnownCodingKeys: String, CodingKey, CaseIterable {
183+
private enum KnownCodingKeys: String, MatrixKnownCodingKeys {
184184
case homeserver = "m.homeserver"
185185
case identityServer = "m.identity_server"
186186

187-
static func doesNotContain(_ key: DynamicCodingKeys) -> Bool {
188-
!Self.allCases.map(\.stringValue).contains(key.stringValue)
189-
}
190-
}
191-
192-
struct DynamicCodingKeys: CodingKey {
193-
var stringValue: String
194-
init?(stringValue: String) {
195-
self.stringValue = stringValue
196-
}
197-
198-
// not used here, but a protocol requirement
199-
var intValue: Int?
200-
init?(intValue _: Int) {
201-
nil
202-
}
203187
}
204188

205189
public init(from decoder: Decoder) throws {
@@ -208,12 +192,12 @@ extension MatrixWellKnown: Codable {
208192
identityServer = try container.decodeIfPresent(ServerInformation.self, forKey: .identityServer)
209193

210194
extraInfo = [:]
211-
let extraContainer = try decoder.container(keyedBy: DynamicCodingKeys.self)
195+
let extraContainer = try decoder.container(keyedBy: MatrixDynamicCodingKeys.self)
212196

213197
for key in extraContainer.allKeys where KnownCodingKeys.doesNotContain(key) {
214198
let decoded = try extraContainer.decode(
215199
AnyCodable.self,
216-
forKey: DynamicCodingKeys(stringValue: key.stringValue)!
200+
forKey: MatrixDynamicCodingKeys(stringValue: key.stringValue)!
217201
)
218202
self.extraInfo[key.stringValue] = decoded
219203
}
@@ -224,7 +208,7 @@ extension MatrixWellKnown: Codable {
224208
try container.encodeIfPresent(homeserver, forKey: .homeserver)
225209
try container.encodeIfPresent(identityServer, forKey: .identityServer)
226210

227-
var extraContainer = encoder.container(keyedBy: DynamicCodingKeys.self)
211+
var extraContainer = encoder.container(keyedBy: MatrixDynamicCodingKeys.self)
228212
for (name, value) in extraInfo {
229213
try extraContainer.encode(value, forKey: .init(stringValue: name)!)
230214
}

Sources/MatrixClient/API/Request.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
import Foundation
9+
import AnyCodable
910

1011
/// HTTP method to be used by ``MatrixClient/MatrixRequest``.
1112
public enum HttpMethod: String, CaseIterable {
@@ -219,3 +220,30 @@ public extension MatrixClient {
219220
}
220221

221222
public struct MatrixEmptyResponse: MatrixResponse {}
223+
224+
225+
226+
227+
// MARK: - Codable
228+
229+
public struct MatrixDynamicCodingKeys: CodingKey {
230+
public var stringValue: String
231+
public init?(stringValue: String) {
232+
self.stringValue = stringValue
233+
}
234+
235+
public var intValue: Int?
236+
public init?(intValue: Int) {
237+
nil
238+
}
239+
}
240+
241+
public protocol MatrixKnownCodingKeys: CodingKey, CaseIterable {
242+
243+
}
244+
245+
public extension MatrixKnownCodingKeys {
246+
static func doesNotContain(_ key: MatrixDynamicCodingKeys) -> Bool {
247+
!Self.allCases.map(\.stringValue).contains(key.stringValue)
248+
}
249+
}

Sources/MatrixClient/API/UserData/Direct.swift

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,8 @@ public struct MatrixDirectAccountData: MatrixAccountData {
1414
}
1515

1616
extension MatrixDirectAccountData: Codable {
17-
private struct DynamicCodingKeys: CodingKey {
18-
var stringValue: String
19-
init?(stringValue: String) {
20-
self.stringValue = stringValue
21-
}
22-
23-
var intValue: Int?
24-
init?(intValue _: Int) {
25-
nil
26-
}
27-
}
28-
2917
public init(from decoder: Decoder) throws {
30-
let container = try decoder.container(keyedBy: DynamicCodingKeys.self)
18+
let container = try decoder.container(keyedBy: MatrixDynamicCodingKeys.self)
3119

3220
for key in container.allKeys {
3321
let decoded = try container.decode([String].self, forKey: key)
@@ -36,7 +24,7 @@ extension MatrixDirectAccountData: Codable {
3624
}
3725

3826
public func encode(to encoder: Encoder) throws {
39-
var container = encoder.container(keyedBy: DynamicCodingKeys.self)
27+
var container = encoder.container(keyedBy: MatrixDynamicCodingKeys.self)
4028
for (name, value) in users {
4129
try container.encode(value, forKey: .init(stringValue: name)!)
4230
}

Sources/MatrixClient/Error.swift

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ public enum MatrixCommonErrorCode: String, Error, Codable {
106106
case passwordWeak = "M_WEAK_PASSWORD"
107107
case termsNotSigned = "M_TERMS_NOT_SIGNED"
108108
case invalidPepper = "M_INVALID_PEPPER"
109+
110+
// MSC 3575
111+
case unknownPos = "M_UNKNOWN_POS"
109112
}
110113

111114
public struct MatrixErrorCode: RawRepresentable, Error, Codable {
@@ -292,7 +295,7 @@ public struct MatrixServerError: Error, Codable {
292295
}
293296

294297
public extension MatrixServerError {
295-
internal enum KnownCodingKeys: String, CodingKey, CaseIterable {
298+
internal enum KnownCodingKeys: String, MatrixKnownCodingKeys {
296299
case errcode
297300
case error
298301

@@ -303,37 +306,24 @@ public extension MatrixServerError {
303306
"completed",
304307
]
305308

306-
static func doesNotContain(_ key: DynamicCodingKeys) -> Bool {
309+
static func doesNotContain(_ key: MatrixDynamicCodingKeys) -> Bool {
307310
!Self.allCases.map(\.stringValue).contains(key.stringValue) && !Self.extraIgnoreValues
308311
.contains(key.stringValue)
309312
}
310313
}
311314

312-
internal struct DynamicCodingKeys: CodingKey {
313-
var stringValue: String
314-
init?(stringValue: String) {
315-
self.stringValue = stringValue
316-
}
317-
318-
// not used here, but a protocol requirement
319-
var intValue: Int?
320-
init?(intValue _: Int) {
321-
nil
322-
}
323-
}
324-
325315
init(from decoder: Decoder) throws {
326316
let container = try decoder.container(keyedBy: KnownCodingKeys.self)
327317
errcode = try container.decodeIfPresent(MatrixErrorCode.self, forKey: .errcode) ?? .Unknown
328318
error = try container.decodeIfPresent(String.self, forKey: .error) ?? ""
329319

330320
extraInfo = [:]
331-
let extraContainer = try decoder.container(keyedBy: DynamicCodingKeys.self)
321+
let extraContainer = try decoder.container(keyedBy: MatrixDynamicCodingKeys.self)
332322

333323
for key in extraContainer.allKeys where KnownCodingKeys.doesNotContain(key) {
334324
let decoded = try extraContainer.decode(
335325
AnyCodable.self,
336-
forKey: DynamicCodingKeys(stringValue: key.stringValue)!
326+
forKey: .init(stringValue: key.stringValue)!
337327
)
338328
self.extraInfo[key.stringValue] = decoded
339329
}
@@ -356,7 +346,7 @@ public extension MatrixServerError {
356346
try container.encode(errcode, forKey: .errcode)
357347
try container.encode(error, forKey: .error)
358348

359-
var extraContainer = encoder.container(keyedBy: DynamicCodingKeys.self)
349+
var extraContainer = encoder.container(keyedBy: MatrixDynamicCodingKeys.self)
360350
for (name, value) in extraInfo {
361351
try extraContainer.encode(value, forKey: .init(stringValue: name)!)
362352
}

0 commit comments

Comments
 (0)