(_: DefaultCodable
.Type, forKey key: Key) throws -> DefaultCodable
{ 53 | if let value = try decodeIfPresent(DefaultCodable
.self, forKey: key) {
54 | return value
55 | } else {
56 | return DefaultCodable(wrappedValue: P.defaultValue)
57 | }
58 | }
59 |
60 | /// Default implementation of decoding a `DefaultCodable` where its strategy is a `BoolCodableStrategy`.
61 | ///
62 | /// Tries to initially Decode a `Bool` if available, otherwise tries to decode it as an `Int` or `String`
63 | /// when there is a `typeMismatch` decoding error. This preserves the actual value of the `Bool` in which
64 | /// the data provider might be sending the value as different types. If everything fails defaults to
65 | /// the `defaultValue` provided by the strategy.
66 | func decode .Type, forKey key: Key) throws -> DefaultCodable {
67 | do {
68 | let value = try decode(Bool.self, forKey: key)
69 | return DefaultCodable(wrappedValue: value)
70 | } catch let error {
71 | guard let decodingError = error as? DecodingError,
72 | case .typeMismatch = decodingError else {
73 | return DefaultCodable(wrappedValue: P.defaultValue)
74 | }
75 | if let intValue = try? decodeIfPresent(Int.self, forKey: key),
76 | let bool = Bool(exactly: NSNumber(value: intValue)) {
77 | return DefaultCodable(wrappedValue: bool)
78 | } else if let stringValue = try? decodeIfPresent(String.self, forKey: key),
79 | let bool = Bool(stringValue) {
80 | return DefaultCodable(wrappedValue: bool)
81 | } else {
82 | return DefaultCodable(wrappedValue: P.defaultValue)
83 | }
84 | }
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/Sources/BetterCodable/DefaultEmptyArray.swift:
--------------------------------------------------------------------------------
1 | public struct DefaultEmptyArrayStrategy