Created
February 6, 2020 20:17
-
-
Save BetterProgramming/1b0fccb86f8fc4d6698b33a534c7fd84 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension RouterService: RouterServiceAnyRouteDecodingProtocol { | |
public func decodeAnyRoute(fromDecoder decoder: Decoder) throws -> (Route, String) { | |
let container = try decoder.singleValueContainer() | |
let identifier = try container.decode(String.self) | |
guard let routeString = RouteString(fromString: identifier) else { | |
throw RouteDecodingError.failedToParseRouteString | |
} | |
guard let routeType = registeredRoutes[routeString.scheme]?.0 else { | |
throw RouteDecodingError.unregisteredRoute | |
} | |
do { | |
let value = try routeType.decode(JSONDecoder(), routeString.parameterData) | |
return (value, routeString.originalString) | |
} catch { | |
throw error | |
} | |
} | |
public enum RouteDecodingError: Swift.Error { | |
case unregisteredRoute | |
case failedToParseRouteString | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment