Further name changes
This commit is contained in:
parent
2b0b4cf91a
commit
6f6575c43e
@ -13,31 +13,29 @@ public protocol Model: Codable {
|
||||
static var identifier: String { get }
|
||||
|
||||
/// Convenience function to decode to model using a keyed container.
|
||||
static func decode<K:CodingKey>(keyedContainer: KeyedDecodingContainer<K>, codingKey: K) throws -> Self?
|
||||
static func decode<K: CodingKey>(keyedContainer: KeyedDecodingContainer<K>, codingKey: K) throws -> Self?
|
||||
/// Convenience function to decode to model using an unkeyed container.
|
||||
static func decode(unkeyedContainer: inout UnkeyedDecodingContainer) throws -> Self?
|
||||
/// Convenience function to encode model using a keyed container.
|
||||
func encode<K:CodingKey>(keyedContainer: inout KeyedEncodingContainer<K>, codingKey: K) throws
|
||||
func encode<K: CodingKey>(keyedContainer: inout KeyedEncodingContainer<K>, codingKey: K) throws
|
||||
/// Convenience function to encode model using an unkeyed container.
|
||||
func encode(unkeyedContainer: inout UnkeyedEncodingContainer) throws
|
||||
}
|
||||
|
||||
extension Model {
|
||||
static public func decode<K:CodingKey>(keyedContainer: KeyedDecodingContainer<K>, codingKey: K) throws -> Self? {
|
||||
let m = try keyedContainer.decodeIfPresent(self, forKey: codingKey)
|
||||
return m
|
||||
static public func decode<K: CodingKey>(keyedContainer: KeyedDecodingContainer<K>, codingKey: K) throws -> Self? {
|
||||
return try keyedContainer.decodeIfPresent(self, forKey: codingKey)
|
||||
}
|
||||
|
||||
static public func decode(unkeyedContainer: inout UnkeyedDecodingContainer) throws -> Self? {
|
||||
let m = try unkeyedContainer.decodeIfPresent(self)
|
||||
return m
|
||||
return try unkeyedContainer.decodeIfPresent(self)
|
||||
}
|
||||
|
||||
public func encode<K:CodingKey>(keyedContainer: inout KeyedEncodingContainer<K>, codingKey: K) throws {
|
||||
public func encode<K: CodingKey>(keyedContainer: inout KeyedEncodingContainer<K>, codingKey: K) throws {
|
||||
try keyedContainer.encode(self, forKey: codingKey)
|
||||
}
|
||||
|
||||
public func encode(unkeyedContainer: inout UnkeyedEncodingContainer) throws{
|
||||
public func encode(unkeyedContainer: inout UnkeyedEncodingContainer) throws {
|
||||
try unkeyedContainer.encode(self)
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,23 +33,23 @@ extension KeyedDecodingContainer where Key: CodingKey {
|
||||
|
||||
//MARK: - Decode
|
||||
/// Decodes to a registered model based on the identifier
|
||||
public func decodeModel<T, C: CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: C) throws -> T {
|
||||
public func decodeModel<M, TypeKey: CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: TypeKey) throws -> M {
|
||||
guard let model: Model = try decodeModelIfPresent(codingKey: codingKey, typeCodingKey: typeCodingKey) else {
|
||||
throw ModelRegistry.Error.decoderErrorObjectNotPresent
|
||||
}
|
||||
|
||||
guard let m = model as? T else {
|
||||
if let model = model as? M {
|
||||
return model
|
||||
} else {
|
||||
throw ModelRegistry.Error.decoderError
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
//MARK: - DecodeIfPresent
|
||||
/// Decodes to a registered model based on the identifier, optional.
|
||||
public func decodeModelIfPresent<T, C: CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: C) throws -> T? {
|
||||
public func decodeModelIfPresent<M, TypeKey: CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: TypeKey) throws -> M? {
|
||||
//get the identifier string
|
||||
guard let container = try? nestedContainer(keyedBy: C.self, forKey: codingKey), let identifier = try? container.decodeIfPresent(String.self, forKey: typeCodingKey) else {
|
||||
guard let container = try? nestedContainer(keyedBy: TypeKey.self, forKey: codingKey), let identifier = try? container.decodeIfPresent(String.self, forKey: typeCodingKey) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -62,22 +62,22 @@ extension KeyedDecodingContainer where Key: CodingKey {
|
||||
//decode the type using the decoder
|
||||
let model = try type.decode(keyedContainer: self, codingKey: codingKey)
|
||||
|
||||
guard let m = model as? T else {
|
||||
if let model = model as? M {
|
||||
return model
|
||||
} else {
|
||||
throw ModelRegistry.Error.decoderError
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
/// Decodes an array of registered model based on the identifiers, optional.
|
||||
public func decodeModelsIfPresent<C: CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: C) throws -> [Model]? {
|
||||
public func decodeModelsIfPresent<TypeKey: CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: TypeKey) throws -> [Model]? {
|
||||
guard var container = try? nestedUnkeyedContainer(forKey: codingKey) else {
|
||||
return nil
|
||||
}
|
||||
var models = [Model]()
|
||||
var containerCopy = container
|
||||
while !container.isAtEnd {
|
||||
let nestedContainer = try container.nestedContainer(keyedBy: C.self)
|
||||
let nestedContainer = try container.nestedContainer(keyedBy: TypeKey.self)
|
||||
if let identifier = try nestedContainer.decodeIfPresent(String.self, forKey: typeCodingKey) {
|
||||
//get the type
|
||||
guard let type = ModelRegistry.getType(for: identifier) else {
|
||||
@ -93,7 +93,7 @@ extension KeyedDecodingContainer where Key: CodingKey {
|
||||
}
|
||||
|
||||
/// Decodes an array of registered model based on the identifiers.
|
||||
public func decodeModels<C: CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: C) throws -> [Model] {
|
||||
public func decodeModels<TypeKey: CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: TypeKey) throws -> [Model] {
|
||||
guard let models: [Model] = try decodeModelsIfPresent(codingKey: codingKey, typeCodingKey: typeCodingKey) else {
|
||||
throw ModelRegistry.Error.decoderErrorObjectNotPresent
|
||||
}
|
||||
@ -105,9 +105,9 @@ extension KeyedEncodingContainer where Key: CodingKey {
|
||||
|
||||
/// Encodes a model, optional.
|
||||
public mutating func encodeModelIfPresent(_ value: Model?, forKey key: KeyedEncodingContainer<K>.Key) throws {
|
||||
if let v = value {
|
||||
if let value = value {
|
||||
let encoder = self.superEncoder(forKey: key)
|
||||
try v.encode(to: encoder)
|
||||
try value.encode(to: encoder)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user