From e1ecb35531b2fe0a488ef8e7a5aeaf28c26d6709 Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Wed, 18 Dec 2019 13:14:56 -0500 Subject: [PATCH 1/3] remove unused code --- .../MVMCore/Models/Model/ModelRegistry.swift | 27 +++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift index b811646..ee741bf 100644 --- a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift +++ b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift @@ -80,27 +80,20 @@ extension KeyedDecodingContainer where Key: CodingKey { var models = [Model]() var container = try nestedUnkeyedContainer(forKey: codingKey) var containerCopy = container - - var i = 0 - let count = container.count ?? 0 - while !container.isAtEnd { - if i < count { - let nestedContainer = try container.nestedContainer(keyedBy: C.self) - if let identifier = try nestedContainer.decodeIfPresent(String.self, forKey: typeCodingKey) { - //get the type - guard let type = ModelRegistry.types[identifier] else { - throw ModelRegistry.Error.decoderErrorModelNotMapped - } - //now get the decoder to use for the type - let decoder = try containerCopy.superDecoder() - let model = try type.init(from: decoder) - models.append(model) + let nestedContainer = try container.nestedContainer(keyedBy: C.self) + if let identifier = try nestedContainer.decodeIfPresent(String.self, forKey: typeCodingKey) { + //get the type + guard let type = ModelRegistry.types[identifier] else { + throw ModelRegistry.Error.decoderErrorModelNotMapped } - i+=1 + //now get the decoder to use for the type + let decoder = try containerCopy.superDecoder() + let model = try type.init(from: decoder) + models.append(model) } } - return models.count > 0 ? models : nil + return models } /// Decodes an array of registered model based on the identifiers. From 326f761f4fe65849d5ea2be962d7c8c4b4d55d90 Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Wed, 18 Dec 2019 13:24:11 -0500 Subject: [PATCH 2/3] Condensing catch --- MVMCore/MVMCore/Models/Model/ModelRegistry.swift | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift index ee741bf..e73437c 100644 --- a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift +++ b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift @@ -49,13 +49,7 @@ extension KeyedDecodingContainer where Key: CodingKey { /// Decodes to a registered model based on the identifier, optional. public func decodeModelIfPresent(codingKey: KeyedDecodingContainer.Key, typeCodingKey: C) throws -> T? { //get the identifier string - var container: KeyedDecodingContainer? - do { - container = try nestedContainer(keyedBy: C.self, forKey: codingKey) - } catch { - return nil - } - guard let identifier = try container?.decodeIfPresent(String.self, forKey: typeCodingKey) else { + guard let container = try? nestedContainer(keyedBy: C.self, forKey: codingKey), let identifier = try? container.decodeIfPresent(String.self, forKey: typeCodingKey) else { return nil } @@ -77,8 +71,10 @@ extension KeyedDecodingContainer where Key: CodingKey { /// Decodes an array of registered model based on the identifiers, optional. public func decodeModelsIfPresent(codingKey: KeyedDecodingContainer.Key, typeCodingKey: C) throws -> [Model]? { + guard var container = try? nestedUnkeyedContainer(forKey: codingKey) else { + return nil + } var models = [Model]() - var container = try nestedUnkeyedContainer(forKey: codingKey) var containerCopy = container while !container.isAtEnd { let nestedContainer = try container.nestedContainer(keyedBy: C.self) From 2b0b4cf91a5b1372b70531558ca031c7481a3d8b Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Wed, 18 Dec 2019 13:26:15 -0500 Subject: [PATCH 3/3] use helper function for getting type --- MVMCore/MVMCore/Models/Model/ModelRegistry.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift index e73437c..f1391c1 100644 --- a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift +++ b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift @@ -54,7 +54,7 @@ extension KeyedDecodingContainer where Key: CodingKey { } //get the type - guard let type = ModelRegistry.types[identifier] else { + guard let type = ModelRegistry.getType(for: identifier) else { MVMCoreLoggingHandler.logDebugMessage(withDelegate: "Model not mapped: \(identifier)") throw ModelRegistry.Error.decoderErrorModelNotMapped } @@ -80,7 +80,7 @@ extension KeyedDecodingContainer where Key: CodingKey { let nestedContainer = try container.nestedContainer(keyedBy: C.self) if let identifier = try nestedContainer.decodeIfPresent(String.self, forKey: typeCodingKey) { //get the type - guard let type = ModelRegistry.types[identifier] else { + guard let type = ModelRegistry.getType(for: identifier) else { throw ModelRegistry.Error.decoderErrorModelNotMapped } //now get the decoder to use for the type