diff --git a/MVMCore/MVMCore/Models/Decoder.swift b/MVMCore/MVMCore/Models/Decoder.swift deleted file mode 100644 index c04e494..0000000 --- a/MVMCore/MVMCore/Models/Decoder.swift +++ /dev/null @@ -1,45 +0,0 @@ -import Foundation - -public protocol AnyDecoder { - func decode(_ type: T.Type, from data: Data) throws -> T -} - -extension JSONDecoder: AnyDecoder {} -extension PropertyListDecoder: AnyDecoder {} - -extension Data { - public func decode(using decoder: AnyDecoder = JSONDecoder()) throws -> T { - return try decoder.decode(T.self, from: self) - } -} - -extension KeyedDecodingContainerProtocol { - public func decode(forKey key: Key) throws -> T { - return try decode(T.self, forKey: key) - } - - public func decode( - forKey key: Key, - default defaultExpression: @autoclosure () -> T - ) throws -> T { - return try decodeIfPresent(T.self, forKey: key) ?? defaultExpression() - } -} - -extension Decodable { - public static func decode(fileName: String, bundle: Bundle = Bundle.main) throws -> Self { - guard let path = bundle.path(forResource: fileName, ofType: ".json") else { - throw JSONError.pathNotFound - } - - guard let jsonData = try? Data(contentsOf: URL(fileURLWithPath: path)) else { - throw JSONError.data(path: path) - } - - do { - return try jsonData.decode() - } catch { - throw JSONError.other(error: error) - } - } -} diff --git a/MVMCore/MVMCore/Models/Encoder.swift b/MVMCore/MVMCore/Models/Encoder.swift deleted file mode 100644 index 0a236a5..0000000 --- a/MVMCore/MVMCore/Models/Encoder.swift +++ /dev/null @@ -1,27 +0,0 @@ -import Foundation - -public protocol AnyEncoder { - func encode(_ value: T) throws -> Data -} - -extension JSONEncoder: AnyEncoder {} -extension PropertyListEncoder: AnyEncoder {} - -extension Encodable { - public func encode(using encoder: AnyEncoder = JSONEncoder()) throws -> Data { - return try encoder.encode(self) - } - - public func toJSON() -> JSONObject? { - guard let data = try? encode() else { return nil } - return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)).flatMap { $0 as? JSONObject } - } - - public func toJSONString() -> String? { - guard let data = try? encode(), - let string = String(data: data, encoding: .utf8) else { - return nil - } - return string - } -} diff --git a/MVMCore/MVMCore/Models/Holder.swift b/MVMCore/MVMCore/Models/Holder.swift deleted file mode 100644 index bee0637..0000000 --- a/MVMCore/MVMCore/Models/Holder.swift +++ /dev/null @@ -1,5 +0,0 @@ -import Foundation - -public protocol Holdable: Codable { - static var identifier: String { get set } -} diff --git a/MVMCore/MVMCore/Models/HolderManager.swift b/MVMCore/MVMCore/Models/HolderManager.swift deleted file mode 100644 index 8f28a7c..0000000 --- a/MVMCore/MVMCore/Models/HolderManager.swift +++ /dev/null @@ -1,83 +0,0 @@ -import Foundation - -public struct HolderManager { - public enum Error: Swift.Error { - case codingKeyNotFound - case encoderError - case decoderError - case decoderErrorObjectNotPresent - case decoderErrorModelNotMapped - } - fileprivate static var types: [String: Holdable.Type] = [:] - public static func register(_ type: A.Type) { - types[A.identifier] = type - } -} - -extension KeyedDecodingContainer where Key : CodingKey { - public enum TypeCodingKey: String, CodingKey { - case actionType - } - - //MARK: - Decode - public func decode(codingKey: KeyedDecodingContainer.Key, typeCodingKey: TypeCodingKey) throws -> T { - guard let holdable: Holdable = try decodeIfPresent(codingKey: codingKey, typeCodingKey: typeCodingKey) else { - throw HolderManager.Error.decoderErrorObjectNotPresent - } - - guard let h = holdable as? T else { - throw HolderManager.Error.decoderError - } - return h - } - - public func decode(codingKey: KeyedDecodingContainer.Key) throws -> ActionTypeProtocol { - return try decode(codingKey: codingKey, typeCodingKey: .actionType) - } - - //MARK: - DecodeIfPresent - public func decodeIfPresent(codingKey: KeyedDecodingContainer.Key, typeCodingKey: TypeCodingKey) throws -> T? { - - //get the type - let meta = try self.nestedContainer(keyedBy: TypeCodingKey.self, forKey: codingKey) - - guard let type = try meta.decodeIfPresent(String.self, forKey: typeCodingKey) else { - return nil - } - - //get the type - guard let found = HolderManager.types[type] else { - throw HolderManager.Error.decoderErrorModelNotMapped - } - - //get the decoder for the propertyKey - let decoder = try self.superDecoder(forKey: codingKey) - - //decode the type using the decoder - let holdable = try found.init(from: decoder) - - guard let h = holdable as? T else { - throw HolderManager.Error.decoderError - } - return h - } - - public func decodeIfPresent(codingKey: KeyedDecodingContainer.Key) throws -> ActionTypeProtocol? { - return try decodeIfPresent(codingKey: codingKey, typeCodingKey: .actionType) - } -} - -extension KeyedEncodingContainer where Key : CodingKey { - - public mutating func encodeIfPresent(_ value: Holdable?, forKey key: KeyedEncodingContainer.Key) throws { - if let v = value { - let encoder = self.superEncoder(forKey: key) - try v.encode(to: encoder) - } - } - - public mutating func encode(_ value: Holdable, forKey key: KeyedEncodingContainer.Key) throws { - let encoder = self.superEncoder(forKey: key) - try value.encode(to: encoder) - } -}