unify the mappings

This commit is contained in:
Pfeil, Scott Robert 2021-03-31 14:00:11 -04:00
parent e5e2d334d8
commit 2a8d248366
2 changed files with 18 additions and 5 deletions

View File

@ -13,8 +13,15 @@
let group = DispatchGroup()
open func createParametersHandler(_ clientParameterModel: ClientParameterModelProtocol) -> ClientParameterProtocol? {
guard let parameterType = ModelRegistry.getHandler(clientParameterModel) as? ClientParameterProtocol.Type else { return nil }
return parameterType.init(clientParameterModel)
do {
let parameterType = try ModelRegistry.getHandler(clientParameterModel) as! ClientParameterProtocol.Type
return parameterType.init(clientParameterModel)
} catch {
if let errorObject = MVMCoreErrorObject.createErrorObject(for: error, location: #function) {
MVMCoreLoggingHandler.shared()?.addError(toLog: errorObject)
}
return nil
}
}
func getClientParameterModel(_ clientParameters: [String: Any]) throws -> ClientParameterModel? {

View File

@ -18,6 +18,7 @@ public struct ModelRegistry {
case decoderErrorObjectNotPresent(codingKey: CodingKey, codingPath: [CodingKey])
case decoderErrorModelNotMapped(identifer: String? = nil, codingKey: CodingKey? = nil, codingPath: [CodingKey]? = nil)
case other(message: String)
case handlerNotMapped(identifer: String? = nil, categoryName: String? = nil)
}
private struct Category {
@ -62,15 +63,20 @@ public struct ModelRegistry {
categories[M.categoryName] = category
}
public static func getHandler(_ model: ModelProtocol) -> ModelHandlerProtocol.Type? {
public static func getHandler(_ model: ModelProtocol) throws -> ModelHandlerProtocol.Type {
//get the modelType
let modelType = type(of: model)
//get the category for the ModelProtocol
guard let category = categories[modelType.categoryName] else { return nil }
guard let category = categories[modelType.categoryName] else {
throw ModelRegistry.Error.other(message: "Category not found: \(modelType.categoryName)")
}
//get the containerProtocol for this ModelProtocol you had registered
return category.handlerTypes[modelType.identifier]
guard let handlerType = category.handlerTypes[modelType.identifier] else {
throw ModelRegistry.Error.handlerNotMapped(identifer: modelType.identifier, categoryName: category.name)
}
return handlerType
}
private static func getCategory<M: ModelProtocol>(for type: M.Type) -> Category {