Skip to content

Instantly share code, notes, and snippets.

@BetterProgramming
Created February 6, 2020 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BetterProgramming/1b0fccb86f8fc4d6698b33a534c7fd84 to your computer and use it in GitHub Desktop.
Save BetterProgramming/1b0fccb86f8fc4d6698b33a534c7fd84 to your computer and use it in GitHub Desktop.
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