|
| 1 | +/// A ``ValueCodingStrategy`` type that specializes decoding/encoding |
| 2 | +/// numeric data. |
| 3 | +protocol NumberCodingStrategy: ValueCodingStrategy where Value == Self {} |
| 4 | + |
| 5 | +public extension ValueCodingStrategy |
| 6 | +where Value: Decodable & ExpressibleByIntegerLiteral & LosslessStringConvertible |
| 7 | +{ |
| 8 | + /// Decodes numeric data from the given `decoder`. |
| 9 | + /// |
| 10 | + /// Decodes basic data type `String,` `Bool` |
| 11 | + /// and converts to numeric representation. |
| 12 | + /// |
| 13 | + /// For decoded boolean type `true` is mapped to `1` |
| 14 | + /// and `false` to `0`. |
| 15 | + /// |
| 16 | + /// - Parameter decoder: The decoder to read data from. |
| 17 | + /// - Returns: The decoded number. |
| 18 | + /// |
| 19 | + /// - Throws: If decoding fails due to corrupted or invalid data |
| 20 | + /// or decoded data can't be mapped to numeric type. |
| 21 | + static func decode(from decoder: Decoder) throws -> Value { |
| 22 | + do { |
| 23 | + return try Value(from: decoder) |
| 24 | + } catch { |
| 25 | + let fallbacks: [(Decoder) -> Value?] = [ |
| 26 | + String.numberValue, |
| 27 | + Bool.numberValue, |
| 28 | + Double.numberValue, |
| 29 | + ] |
| 30 | + guard let value = fallbacks.lazy.compactMap({ $0(decoder) }).first |
| 31 | + else { throw error } |
| 32 | + return value |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +private extension Bool { |
| 38 | + /// Decodes optional numeric data from the given `decoder`. |
| 39 | + /// |
| 40 | + /// - Parameter decoder: The decoder to read data from. |
| 41 | + /// - Returns: The decoded number value, `nil` if boolean |
| 42 | + /// data can't be decoded. |
| 43 | + static func numberValue<Number>( |
| 44 | + from decoder: Decoder |
| 45 | + ) -> Number? where Number: ExpressibleByIntegerLiteral { |
| 46 | + guard let boolValue = try? Self(from: decoder) else { return nil } |
| 47 | + return boolValue ? 1 : 0 |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +private extension String { |
| 52 | + /// Decodes optional numeric data from the given `decoder`. |
| 53 | + /// |
| 54 | + /// - Parameter decoder: The decoder to read data from. |
| 55 | + /// - Returns: The decoded number value, `nil` if text data |
| 56 | + /// can't be decoded or converted to numeric representation. |
| 57 | + static func numberValue<Number>( |
| 58 | + from decoder: Decoder |
| 59 | + ) -> Number? |
| 60 | + where Number: LosslessStringConvertible & ExpressibleByIntegerLiteral { |
| 61 | + guard let strValue = try? Self(from: decoder) else { return nil } |
| 62 | + return Number(strValue) ?? Number(exact: Double(strValue)) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +internal extension Double { |
| 67 | + /// Decodes optional numeric data from the given `decoder`. |
| 68 | + /// |
| 69 | + /// - Parameter decoder: The decoder to read data from. |
| 70 | + /// - Returns: The decoded number value, `nil` if float |
| 71 | + /// data can't be converted to exact number value. |
| 72 | + @inlinable |
| 73 | + static func numberValue<Number>( |
| 74 | + from decoder: Decoder |
| 75 | + ) -> Number? where Number: ExpressibleByIntegerLiteral { |
| 76 | + return Number(exact: try? Self(from: decoder)) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +internal extension ExpressibleByIntegerLiteral { |
| 81 | + /// Converts optional given float to integer. |
| 82 | + /// |
| 83 | + /// - Parameter float: The float value to convert. |
| 84 | + /// - Returns: The integer value, `nil` if float |
| 85 | + /// data can't be converted to exact integer value. |
| 86 | + @usableFromInline |
| 87 | + init?(exact float: Double?) { |
| 88 | + guard |
| 89 | + let float = float, |
| 90 | + let type = Self.self as? any BinaryInteger.Type, |
| 91 | + let intVal = type.init(exactly: float) as (any BinaryInteger)?, |
| 92 | + let val = intVal as? Self |
| 93 | + else { return nil } |
| 94 | + self = val |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +extension Double: NumberCodingStrategy {} |
| 99 | +extension Float: NumberCodingStrategy {} |
| 100 | +extension Int: NumberCodingStrategy {} |
| 101 | +extension Int64: NumberCodingStrategy {} |
| 102 | +extension Int32: NumberCodingStrategy {} |
| 103 | +extension Int16: NumberCodingStrategy {} |
| 104 | +extension Int8: NumberCodingStrategy {} |
| 105 | +extension UInt: NumberCodingStrategy {} |
| 106 | +extension UInt64: NumberCodingStrategy {} |
| 107 | +extension UInt32: NumberCodingStrategy {} |
| 108 | +extension UInt16: NumberCodingStrategy {} |
| 109 | +extension UInt8: NumberCodingStrategy {} |
0 commit comments