Skip to content

Instantly share code, notes, and snippets.

@tail-call
Created October 11, 2023 05:55
Show Gist options
  • Save tail-call/9ea3eacaf4c1adf1383c096124d3aca0 to your computer and use it in GitHub Desktop.
Save tail-call/9ea3eacaf4c1adf1383c096124d3aca0 to your computer and use it in GitHub Desktop.
/// Используется для декодирования объектов, которые могут принимать один из двух типов
enum EitherDecodableEquatable<
Left: Decodable & Equatable,
Right: Decodable & Equatable
>: Decodable, Equatable {
case left(Left)
case right(Right)
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
do {
self = .left(try container.decode(Left.self))
} catch {
self = .right(try container.decode(Right.self))
}
}
static func == (
lhs: EitherDecodableEquatable,
rhs: EitherDecodableEquatable
) -> Bool {
switch (lhs, rhs) {
case (.left(let lhsLeft), .left(let rhsLeft)):
return lhsLeft == rhsLeft
case (.right(let lhsRight), .right(let rhsRight)):
return lhsRight == rhsRight
default:
return false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment