From 95e36ffc2d76a941dfa56fdd915c70ebc2d2fac1 Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Thu, 8 Apr 2021 11:13:50 -0400 Subject: [PATCH] helper for always present --- .../ModelProtocols/MoleculeModelProtocol.swift | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/MVMCoreUI/Atomic/Protocols/ModelProtocols/MoleculeModelProtocol.swift b/MVMCoreUI/Atomic/Protocols/ModelProtocols/MoleculeModelProtocol.swift index 2823e5a5..0cd54170 100644 --- a/MVMCoreUI/Atomic/Protocols/ModelProtocols/MoleculeModelProtocol.swift +++ b/MVMCoreUI/Atomic/Protocols/ModelProtocols/MoleculeModelProtocol.swift @@ -21,6 +21,8 @@ public extension MoleculeModelProtocol { static var categoryCodingKey: String { "moleculeName" } } + +// Helpers made due to swift not able to reconcile which category. extension KeyedDecodingContainer where Key: CodingKey { /// Decodes to a registered molecule based on the identifier public func decodeMoleculeIfPresent(codingKey: KeyedDecodingContainer.Key) throws -> T? { @@ -33,6 +35,15 @@ extension KeyedDecodingContainer where Key: CodingKey { } return modelT } + + /// Decodes to a registered molecule based on the identifier. + public func decodeMolecule(codingKey: KeyedDecodingContainer.Key) throws -> T { + guard let model: T = try decodeMoleculeIfPresent(codingKey: codingKey) else { + MVMCoreLoggingHandler.logDebugMessage(withDelegate: "ModelRegistry Error decoderErrorObjectNotPresent: \(codingKey.stringValue)") + throw ModelRegistry.Error.decoderErrorObjectNotPresent(codingKey: codingKey, codingPath: codingPath) + } + return model + } } public extension MoleculeModelProtocol {