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
import Foundation | |
/// Allows a property to fail decoding, defaulting to nil instead of surfacing the error. | |
@propertyWrapper | |
struct AllowDecodeFailure<Wrapped>: Codable where Wrapped: Codable { | |
var wrappedValue: Wrapped? | |
init() { | |
self.wrappedValue = nil | |
} | |
init(wrappedValue: Wrapped?) { | |
self.wrappedValue = wrappedValue | |
} | |
init(from decoder: Decoder) throws { | |
let container = try decoder.singleValueContainer() | |
self.wrappedValue = try? container.decode(Wrapped.self) | |
} | |
} | |
extension AllowDecodeFailure: Equatable where Wrapped: Equatable {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment