- updated the ClientParameterProtocol to use an associatedType for the Model being used in then Handler

- refactored the ClientParameterHandler with the above changes.

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-11-09 10:57:05 -06:00
parent 77ab27d79d
commit 641ebcac7f
2 changed files with 47 additions and 14 deletions

View File

@ -10,10 +10,19 @@
public static let DefaultTimeout = 30.0 public static let DefaultTimeout = 30.0
open func createParametersHandler(_ clientParameterModel: ClientParameterModelProtocol) -> ClientParameterProtocol? { open func createParametersHandler(_ clientParameterModel: ClientParameterModelProtocol) -> (any ClientParameterProtocol)? {
do { do {
let parameterType = try ModelRegistry.getHandler(clientParameterModel) as! ClientParameterProtocol.Type //Ensure the handlerType return will be initable using ClientParameterHandler
return parameterType.init(clientParameterModel) let handlerType = try ModelRegistry.getHandler(clientParameterModel) as! AnyClientParameterProtocol.Type
//init the handler
let handler = try handlerType.init(clientParameterModel: clientParameterModel)
//Cast from AnyClientParameterProtocol to AnyClientParameterProtocol, if not throw
guard let castedHandler = handler as? any ClientParameterProtocol else { throw ClientParameterError.castingError }
//Return the any ClientParameterProtocol
return castedHandler
} catch { } catch {
if let errorObject = MVMCoreErrorObject.createErrorObject(for: error, location: #function) { if let errorObject = MVMCoreErrorObject.createErrorObject(for: error, location: #function) {
MVMCoreLoggingHandler.addError(toLog: errorObject) MVMCoreLoggingHandler.addError(toLog: errorObject)

View File

@ -8,20 +8,44 @@
import Foundation import Foundation
public protocol ClientParameterProtocol: ModelHandlerProtocol { /// Errors used for ClientParamter
static var name: String { get } public enum ClientParameterError: Error {
case castingError
case modelMismatch(message: String)
}
init(_ clientParameterModel: ClientParameterModelProtocol) public protocol AnyClientParameterProtocol {
/// Original init using a ClientParameterModelProtocol to get around the self - associatedType issues with ClientParameterProtocol
/// - Parameter clientParameterModel: any Model using the ClientParameterModelProtocol
init(clientParameterModel: ClientParameterModelProtocol) throws
}
var clientParameterModel: ClientParameterModelProtocol { get set } public protocol ClientParameterProtocol<Model>: ModelHandlerProtocol, AnyClientParameterProtocol {
associatedtype Model: ClientParameterModelProtocol
func fetchClientParameters(requestParameters: [String: Any], timingOutIn timeout: TimeInterval, completionHandler:@escaping ([String: AnyHashable]?) -> ()) static var name: String { get }
/// Default parameter for timeout scenarios. It will use the protocol extension method bydefault. Can override to send custom values. var clientParameterModel: Model { get set }
func valueOnTimeout() -> [String: AnyHashable]
func fetchClientParameters(requestParameters: [String: Any], timingOutIn timeout: TimeInterval, completionHandler:@escaping ([String: AnyHashable]?) -> ())
/// Default parameter for timeout scenarios. It will use the protocol extension method bydefault. Can override to send custom values.
func valueOnTimeout() -> [String: AnyHashable]
init(clientParameterModel: Model)
} }
public extension ClientParameterProtocol { public extension ClientParameterProtocol {
/// Default Implementation should never be overridden
init(clientParameterModel: ClientParameterModelProtocol) throws {
// Cast the clientParameterModel to ensure it matches the asscociatedType Model assigned to this Handler.
guard let castedModel = clientParameterModel as? Model else {
let message = "Expected \(Model.self) but received \(clientParameterModel.type)"
throw ClientParameterError.modelMismatch(message: message)
}
// Call init for the Handler Class and pass in the casted Model.
self.init(clientParameterModel: castedModel)
}
func valueOnTimeout() -> [String: AnyHashable] { func valueOnTimeout() -> [String: AnyHashable] {
return [Self.name: "failed_to_collect"] return [Self.name: "failed_to_collect"]