Skip to content

Instantly share code, notes, and snippets.

@pietrobasso
Last active January 11, 2022 13:10
Show Gist options
  • Save pietrobasso/85267f94466265347d89896729211802 to your computer and use it in GitHub Desktop.
Save pietrobasso/85267f94466265347d89896729211802 to your computer and use it in GitHub Desktop.
Throwable Response
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