Last active
January 11, 2022 13:10
-
-
Save pietrobasso/85267f94466265347d89896729211802 to your computer and use it in GitHub Desktop.
Throwable Response
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
struct Response { | |
let items: [Widget] | |
} | |
extension Response: Decodable { | |
enum CodingKeys: CodingKey { | |
case items | |
} | |
init(from decoder: Decoder) throws { | |
let container = try decoder.container(keyedBy: CodingKeys.self) | |
let items = try container | |
.decode([Throwable<Widget>].self, forKey: .items) | |
.compactMap { $0.get() } | |
guard !items.isEmpty else { | |
throw ResponseDecodingError.noWidgets | |
} | |
self.items = items | |
} | |
} | |
enum ResponseDecodingError: Error { | |
case noWidgets | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment