- 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:
parent
77ab27d79d
commit
641ebcac7f
@ -10,10 +10,19 @@
|
||||
|
||||
public static let DefaultTimeout = 30.0
|
||||
|
||||
open func createParametersHandler(_ clientParameterModel: ClientParameterModelProtocol) -> ClientParameterProtocol? {
|
||||
open func createParametersHandler(_ clientParameterModel: ClientParameterModelProtocol) -> (any ClientParameterProtocol)? {
|
||||
do {
|
||||
let parameterType = try ModelRegistry.getHandler(clientParameterModel) as! ClientParameterProtocol.Type
|
||||
return parameterType.init(clientParameterModel)
|
||||
//Ensure the handlerType return will be initable using ClientParameterHandler
|
||||
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 {
|
||||
if let errorObject = MVMCoreErrorObject.createErrorObject(for: error, location: #function) {
|
||||
MVMCoreLoggingHandler.addError(toLog: errorObject)
|
||||
|
||||
@ -8,20 +8,44 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
public protocol ClientParameterProtocol: ModelHandlerProtocol {
|
||||
/// Errors used for ClientParamter
|
||||
public enum ClientParameterError: Error {
|
||||
case castingError
|
||||
case modelMismatch(message: String)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
public protocol ClientParameterProtocol<Model>: ModelHandlerProtocol, AnyClientParameterProtocol {
|
||||
associatedtype Model: ClientParameterModelProtocol
|
||||
|
||||
static var name: String { get }
|
||||
|
||||
init(_ clientParameterModel: ClientParameterModelProtocol)
|
||||
|
||||
var clientParameterModel: ClientParameterModelProtocol { get set }
|
||||
var clientParameterModel: Model { get set }
|
||||
|
||||
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 {
|
||||
/// 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] {
|
||||
return [Self.name: "failed_to_collect"]
|
||||
|
||||
Loading…
Reference in New Issue
Block a user