|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// CodableVariable wraps a `Codable` instance to make it compatible with @objc methods. |
| 4 | +/// |
| 5 | +/// `Codable` documentation: https://developer.apple.com/documentation/swift/codable |
| 6 | +/// |
| 7 | +/// The Swift compiler doesn't allow developers to declare an @objc method for eDO as below: |
| 8 | +/// |
| 9 | +/// ``` |
| 10 | +/// @objc public FooClass : NSObject { |
| 11 | +/// @objc public func callBar(value: Int?) // Compile Error! |
| 12 | +/// } |
| 13 | +/// ``` |
| 14 | +/// |
| 15 | +/// The same issue also applies to other auto-synthesized Codable types, including classes, structs |
| 16 | +/// and enums (Swift 5.5+). This is because Optional<Int> is a pure Swift type that cannot be |
| 17 | +/// represented in Objective-C land. As a workaround, developers can declare a new method in the |
| 18 | +/// class extension as below: |
| 19 | +/// |
| 20 | +/// ``` |
| 21 | +/// extension FooClass { |
| 22 | +/// @objc public func callBar(codedValue: CodableVariable) throws { |
| 23 | +/// self.callBar(value: try codedValue.unwrap(Int?.self)) |
| 24 | +/// } |
| 25 | +/// } |
| 26 | +/// ``` |
| 27 | +/// |
| 28 | +/// The new method is compatible with @objc and forwards the coded variables to the original method. |
| 29 | +/// So developers can use the new method in the remote call: |
| 30 | +/// |
| 31 | +/// ``` |
| 32 | +/// var value : Int? |
| 33 | +/// // Do something... |
| 34 | +/// try remoteFooInstance.callBar(codedValue: try CodableVariable.wrap(value)) |
| 35 | +/// ``` |
| 36 | +@objc(EDOCodableVariable) |
| 37 | +public class CodableVariable: NSObject, NSSecureCoding, Codable { |
| 38 | + |
| 39 | + /// Error thrown by `CodableVariable` during the decoding. |
| 40 | + public enum DecodingError: Error, LocalizedError { |
| 41 | + /// The type of encoded data doesn't match the caller's expecting type. |
| 42 | + /// - expectedType: The type expected by the decoding method caller. |
| 43 | + /// - actualType: The type of the encoded data. |
| 44 | + case typeUnmatched(expectedType: String, actualType: String) |
| 45 | + |
| 46 | + public var errorDescription: String? { |
| 47 | + switch self { |
| 48 | + case let .typeUnmatched(expectedType, actualType): |
| 49 | + return "Expecting to decode \(expectedType) but the codable variable is \(actualType)." |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + internal static let typeKey = "EDOTypeKey" |
| 55 | + internal let data: Data |
| 56 | + internal let type: String |
| 57 | + |
| 58 | + /// Creates `CodableVariable` instance with a `Codable` instance. |
| 59 | + /// |
| 60 | + /// - Parameter parameter: The Codable instance to be wrapped. |
| 61 | + /// - Returns: A `CodableVariable` instance. |
| 62 | + /// - Throws: Errors propagated from JSONEncoder when encoding `parameter`. |
| 63 | + public static func wrap<T: Encodable>(_ parameter: T) throws -> CodableVariable { |
| 64 | + let encoder = JSONEncoder() |
| 65 | + return CodableVariable(data: try encoder.encode(parameter), type: String(describing: T.self)) |
| 66 | + } |
| 67 | + |
| 68 | + internal init(data: Data, type: String) { |
| 69 | + self.data = data |
| 70 | + self.type = type |
| 71 | + } |
| 72 | + |
| 73 | + /// Decodes the Codable instance. |
| 74 | + /// |
| 75 | + /// - Parameter type: The expected type of the decoded instance. |
| 76 | + /// - Returns: The decoded instance of `type`. |
| 77 | + /// - Throws: `CodableVariable.DecodingError` if decoding fails. |
| 78 | + public func unwrap<T: Decodable>(_ type: T.Type) throws -> T { |
| 79 | + guard self.type == String(describing: type) else { |
| 80 | + throw DecodingError.typeUnmatched( |
| 81 | + expectedType: self.type, |
| 82 | + actualType: String(describing: type)) |
| 83 | + } |
| 84 | + let decoder = JSONDecoder() |
| 85 | + return try decoder.decode(T.self, from: data) |
| 86 | + } |
| 87 | + |
| 88 | + // MARK - NSSecureCoding |
| 89 | + |
| 90 | + public required init?(coder: NSCoder) { |
| 91 | + guard let data = coder.decodeData(), |
| 92 | + let type = coder.decodeObject(forKey: CodableVariable.typeKey) as? String |
| 93 | + else { |
| 94 | + return nil |
| 95 | + } |
| 96 | + self.data = data |
| 97 | + self.type = type |
| 98 | + } |
| 99 | + |
| 100 | + public func encode(with coder: NSCoder) { |
| 101 | + coder.encode(data) |
| 102 | + coder.encode(type, forKey: CodableVariable.typeKey) |
| 103 | + } |
| 104 | + |
| 105 | + @objc public var edo_isEDOValueType: Bool { return true } |
| 106 | + |
| 107 | + @objc public static var supportsSecureCoding: Bool { return true } |
| 108 | +} |
| 109 | + |
| 110 | +/// Extends Encodable to easily produce `CodableVariable` from the instance. |
| 111 | +extension Encodable { |
| 112 | + /// Produces a `CodableVariable` instance from `self`. |
| 113 | + public var eDOCodableVariable: CodableVariable { |
| 114 | + // try! is used here because`CodableVariable.wrap` only throws programmer errors when |
| 115 | + // JSONEncoder fails to encode a `Encodable` type. |
| 116 | + return try! CodableVariable.wrap(self) |
| 117 | + } |
| 118 | +} |
0 commit comments