From 6f6575c43ed5d2add215c5283b107d440752ae7b Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Wed, 18 Dec 2019 16:07:01 -0500 Subject: [PATCH] Further name changes --- MVMCore/MVMCore/Models/Model/Model.swift | 16 +++++------ .../MVMCore/Models/Model/ModelRegistry.swift | 28 +++++++++---------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/MVMCore/MVMCore/Models/Model/Model.swift b/MVMCore/MVMCore/Models/Model/Model.swift index 544cb3a..936c548 100644 --- a/MVMCore/MVMCore/Models/Model/Model.swift +++ b/MVMCore/MVMCore/Models/Model/Model.swift @@ -13,31 +13,29 @@ public protocol Model: Codable { static var identifier: String { get } /// Convenience function to decode to model using a keyed container. - static func decode(keyedContainer: KeyedDecodingContainer, codingKey: K) throws -> Self? + static func decode(keyedContainer: KeyedDecodingContainer, codingKey: K) throws -> Self? /// Convenience function to decode to model using an unkeyed container. static func decode(unkeyedContainer: inout UnkeyedDecodingContainer) throws -> Self? /// Convenience function to encode model using a keyed container. - func encode(keyedContainer: inout KeyedEncodingContainer, codingKey: K) throws + func encode(keyedContainer: inout KeyedEncodingContainer, codingKey: K) throws /// Convenience function to encode model using an unkeyed container. func encode(unkeyedContainer: inout UnkeyedEncodingContainer) throws } extension Model { - static public func decode(keyedContainer: KeyedDecodingContainer, codingKey: K) throws -> Self? { - let m = try keyedContainer.decodeIfPresent(self, forKey: codingKey) - return m + static public func decode(keyedContainer: KeyedDecodingContainer, codingKey: K) throws -> Self? { + return try keyedContainer.decodeIfPresent(self, forKey: codingKey) } static public func decode(unkeyedContainer: inout UnkeyedDecodingContainer) throws -> Self? { - let m = try unkeyedContainer.decodeIfPresent(self) - return m + return try unkeyedContainer.decodeIfPresent(self) } - public func encode(keyedContainer: inout KeyedEncodingContainer, codingKey: K) throws { + public func encode(keyedContainer: inout KeyedEncodingContainer, codingKey: K) throws { try keyedContainer.encode(self, forKey: codingKey) } - public func encode(unkeyedContainer: inout UnkeyedEncodingContainer) throws{ + public func encode(unkeyedContainer: inout UnkeyedEncodingContainer) throws { try unkeyedContainer.encode(self) } } diff --git a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift index f1391c1..b4043c4 100644 --- a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift +++ b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift @@ -33,23 +33,23 @@ extension KeyedDecodingContainer where Key: CodingKey { //MARK: - Decode /// Decodes to a registered model based on the identifier - public func decodeModel(codingKey: KeyedDecodingContainer.Key, typeCodingKey: C) throws -> T { + public func decodeModel(codingKey: KeyedDecodingContainer.Key, typeCodingKey: TypeKey) throws -> M { guard let model: Model = try decodeModelIfPresent(codingKey: codingKey, typeCodingKey: typeCodingKey) else { throw ModelRegistry.Error.decoderErrorObjectNotPresent } - guard let m = model as? T else { + if let model = model as? M { + return model + } else { throw ModelRegistry.Error.decoderError } - - return m } //MARK: - DecodeIfPresent /// Decodes to a registered model based on the identifier, optional. - public func decodeModelIfPresent(codingKey: KeyedDecodingContainer.Key, typeCodingKey: C) throws -> T? { + public func decodeModelIfPresent(codingKey: KeyedDecodingContainer.Key, typeCodingKey: TypeKey) throws -> M? { //get the identifier string - guard let container = try? nestedContainer(keyedBy: C.self, forKey: codingKey), let identifier = try? container.decodeIfPresent(String.self, forKey: typeCodingKey) else { + guard let container = try? nestedContainer(keyedBy: TypeKey.self, forKey: codingKey), let identifier = try? container.decodeIfPresent(String.self, forKey: typeCodingKey) else { return nil } @@ -62,22 +62,22 @@ extension KeyedDecodingContainer where Key: CodingKey { //decode the type using the decoder let model = try type.decode(keyedContainer: self, codingKey: codingKey) - guard let m = model as? T else { + if let model = model as? M { + return model + } else { throw ModelRegistry.Error.decoderError } - - return m } /// Decodes an array of registered model based on the identifiers, optional. - public func decodeModelsIfPresent(codingKey: KeyedDecodingContainer.Key, typeCodingKey: C) throws -> [Model]? { + public func decodeModelsIfPresent(codingKey: KeyedDecodingContainer.Key, typeCodingKey: TypeKey) throws -> [Model]? { guard var container = try? nestedUnkeyedContainer(forKey: codingKey) else { return nil } var models = [Model]() var containerCopy = container while !container.isAtEnd { - let nestedContainer = try container.nestedContainer(keyedBy: C.self) + let nestedContainer = try container.nestedContainer(keyedBy: TypeKey.self) if let identifier = try nestedContainer.decodeIfPresent(String.self, forKey: typeCodingKey) { //get the type guard let type = ModelRegistry.getType(for: identifier) else { @@ -93,7 +93,7 @@ extension KeyedDecodingContainer where Key: CodingKey { } /// Decodes an array of registered model based on the identifiers. - public func decodeModels(codingKey: KeyedDecodingContainer.Key, typeCodingKey: C) throws -> [Model] { + public func decodeModels(codingKey: KeyedDecodingContainer.Key, typeCodingKey: TypeKey) throws -> [Model] { guard let models: [Model] = try decodeModelsIfPresent(codingKey: codingKey, typeCodingKey: typeCodingKey) else { throw ModelRegistry.Error.decoderErrorObjectNotPresent } @@ -105,9 +105,9 @@ extension KeyedEncodingContainer where Key: CodingKey { /// Encodes a model, optional. public mutating func encodeModelIfPresent(_ value: Model?, forKey key: KeyedEncodingContainer.Key) throws { - if let v = value { + if let value = value { let encoder = self.superEncoder(forKey: key) - try v.encode(to: encoder) + try value.encode(to: encoder) } }