From ffd61f91c32d8eb38ae8a380bde98918e3f075e3 Mon Sep 17 00:00:00 2001 From: Kevin G Christiano Date: Wed, 12 Feb 2020 14:14:01 -0500 Subject: [PATCH] integrating changes to make fan out instance models into categories due to name conflicts --- .../ActionType/ActionModelProtocol.swift | 8 ++--- MVMCore/MVMCore/Models/Model/Model.swift | 2 +- .../MVMCore/Models/Model/ModelRegistry.swift | 30 ++++++++++++++----- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/MVMCore/MVMCore/Models/ActionType/ActionModelProtocol.swift b/MVMCore/MVMCore/Models/ActionType/ActionModelProtocol.swift index 09e4673..00997fb 100644 --- a/MVMCore/MVMCore/Models/ActionType/ActionModelProtocol.swift +++ b/MVMCore/MVMCore/Models/ActionType/ActionModelProtocol.swift @@ -25,17 +25,17 @@ public protocol ActionModelProtocol: Model { var title: String? { get set } } -extension ActionModelProtocol { +public extension ActionModelProtocol { - public var actionType: String? { + var actionType: String? { get { return Self.identifier } } - public static var categoryCodingKey: String { + static var categoryCodingKey: String { return "actionType" } - public static var categoryName: String { + static var categoryName: String { return "\(ActionModelProtocol.self)" } } diff --git a/MVMCore/MVMCore/Models/Model/Model.swift b/MVMCore/MVMCore/Models/Model/Model.swift index dc17f7e..74166f2 100644 --- a/MVMCore/MVMCore/Models/Model/Model.swift +++ b/MVMCore/MVMCore/Models/Model/Model.swift @@ -16,7 +16,7 @@ public protocol Model: Codable { /// Name of the defining object. static var categoryName: String { get } - /// The JSON key name of the + /// The primary category key name of the static var categoryCodingKey: String { get } /// Convenience function to decode to model using a keyed container. diff --git a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift index 026bf7b..da7df9b 100644 --- a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift +++ b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift @@ -9,6 +9,7 @@ import Foundation + public struct ModelRegistry { public enum Error: Swift.Error { @@ -33,11 +34,7 @@ public struct ModelRegistry { public static func register(_ type: M.Type) throws { var category: Category - print("\n\n") - print("HI= \(M.identifier)") - print("HI= \(M.categoryName)") - print("HI= \(M.categoryCodingKey)") - print("\n\n") + if let c = categories[M.categoryName] { category = c } else { @@ -61,6 +58,23 @@ public struct ModelRegistry { return getCategory(for: type)?.instanceTypes[name] } + + + private static func getCategory(for typeString: String) -> Category? { + + for (_, value) in categories where value.codingKey == typeString { + return value + } + + return nil + } + + public static func getType(for name: String, with typeString: String) -> Model.Type? { + return getCategory(for: typeString)?.instanceTypes[name] + } + + + public static func getCodingKey(for type: T.Type) throws -> AnyCodingKey{ guard let category = getCategory(for: type) else { throw ModelRegistry.Error.decoderOther(message: "decodeModelsIfPresent only works for objects implementing the Model protocol") @@ -100,7 +114,7 @@ extension KeyedDecodingContainer where Key: CodingKey { let identifier = try? container.decodeIfPresent(String.self, forKey: typeCodingKey) else { return nil } - guard let type = ModelRegistry.getType(for: identifier, with: M.self) else { + guard let type = ModelRegistry.getType(for: identifier, with: typeCodingKey.stringValue) else { MVMCoreLoggingHandler.logDebugMessage(withDelegate: "Model not mapped: \(identifier)") throw ModelRegistry.Error.decoderErrorModelNotMapped } @@ -119,7 +133,7 @@ extension KeyedDecodingContainer where Key: CodingKey { /// Decodes an array of registered model based on the identifiers, optional. public func decodeModelsIfPresent(codingKey: KeyedDecodingContainer.Key, typeCodingKey: TypeKey) throws -> [Model]? { guard contains(codingKey), - var container = try? nestedUnkeyedContainer(forKey: codingKey) + var container = try? self.nestedUnkeyedContainer(forKey: codingKey) else { return nil } return try container.decodeModelsIfPresent(typeCodingKey: typeCodingKey) @@ -211,7 +225,7 @@ public extension UnkeyedDecodingContainer { while !containerCopy.isAtEnd { let nestedContainer = try containerCopy.nestedContainer(keyedBy: TypeKey.self) if let identifier = try nestedContainer.decodeIfPresent(String.self, forKey: typeCodingKey) { - guard let type = ModelRegistry.getType(for: identifier, with: TypeKey.self) else { + guard let type = ModelRegistry.getType(for: identifier, with: typeCodingKey.stringValue) else { MVMCoreLoggingHandler.logDebugMessage(withDelegate: "ModelRegistry Error decoderErrorModelNotMapped: \(identifier)") throw ModelRegistry.Error.decoderErrorModelNotMapped }