From 9293894d367da338f7397b4a9d6fc37332b53a0a Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Fri, 29 Oct 2021 09:25:24 -0500 Subject: [PATCH] refactored the hasActionHanlder guard bug - added public to JSONValue decode Signed-off-by: Matt Bruce --- .../MVMCoreActionHandler+Extension.swift | 23 +++++++++---------- MVMCore/MVMCore/Models/JSON/JSONValue.swift | 2 +- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler+Extension.swift b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler+Extension.swift index 04ea87c..273b8b9 100644 --- a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler+Extension.swift +++ b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler+Extension.swift @@ -32,30 +32,29 @@ public extension MVMCoreActionHandler { } @objc func hasActionHandler(actionType: String?, actionInformation: [String: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) -> Bool { - //ensure there is a Serialized version of the Action - guard let actionType = actionType, + + guard //ensure there is a actinType + let actionType = actionType, + //ensure there is a serialized version of the Action let actionInformation = actionInformation, + //esnure the actionModelType let actionModelType = ModelRegistry.getType(for: actionType, with: ActionModelProtocol.self), - let actionHandlerType = try? ModelRegistry.getHandlerType(for: actionModelType) + //ensure there is handlerType for the action of MVMCoreActionHandlerProtocol + let actionHandlerType = try? ModelRegistry.getHandlerType(for: actionModelType) as? MVMCoreActionHandlerProtocol.Type else { return false } do { - //Cast the actionHandlerType to the new Protocol - guard let actionHandlerProtocolType = actionHandlerType as? MVMCoreActionHandlerProtocol.Type else { - throw ModelRegistry.Error.decoderOther(message: "ModelHandlerProtocol found not of MVMCoreActionHandlerProtocol Type") - } - - //deserialize the actionModel for the actionType found + //ensure the decoded actionModel is of ActionModelProtocol guard let actionModel = try actionModelType.decode(jsonDict: actionInformation) as? ActionModelProtocol else { throw ModelRegistry.Error.decoderOther(message: "Could not decode to ActionModelProtocol") } - + //create the handler since we know it can initialize - let actionHandler = actionHandlerProtocolType.init() + let actionHandler = actionHandlerType.init() //call the handleAction of the handler actionHandler.handleAction(actionModel, additionalData: additionalData, delegateObject: delegateObject) - + } catch { //log the error if let errorObject = MVMCoreErrorObject.createErrorObject(for: error, location: "") { diff --git a/MVMCore/MVMCore/Models/JSON/JSONValue.swift b/MVMCore/MVMCore/Models/JSON/JSONValue.swift index a603d23..43796cb 100644 --- a/MVMCore/MVMCore/Models/JSON/JSONValue.swift +++ b/MVMCore/MVMCore/Models/JSON/JSONValue.swift @@ -51,7 +51,7 @@ public enum JSONValue: Codable, Equatable { self = value ?? JSONValue.null } - func decode() throws -> T { + public func decode() throws -> T { let encoded = try JSONEncoder().encode(self) return try JSONDecoder().decode(T.self, from: encoded) }