refactored the hasActionHanlder guard

bug - added public to JSONValue decode

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2021-10-29 09:25:24 -05:00
parent 7f5419d81b
commit 9293894d36
2 changed files with 12 additions and 13 deletions

View File

@ -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: "") {

View File

@ -51,7 +51,7 @@ public enum JSONValue: Codable, Equatable {
self = value ?? JSONValue.null
}
func decode<T: Decodable>() throws -> T {
public func decode<T: Decodable>() throws -> T {
let encoded = try JSONEncoder().encode(self)
return try JSONDecoder().decode(T.self, from: encoded)
}