update array decoding method
This commit is contained in:
parent
0d1c3196bf
commit
b2eb88f7f6
@ -12,5 +12,19 @@ public protocol Model: Codable {
|
|||||||
|
|
||||||
static var identifier: String { get }
|
static var identifier: String { get }
|
||||||
|
|
||||||
|
static func mvmdecode<K:CodingKey>(keyedContainer: KeyedDecodingContainer<K>, codingKey: K) throws -> Self?
|
||||||
|
static func mvmdecode(unkeyedContainer: inout UnkeyedDecodingContainer) throws -> Self?
|
||||||
|
}
|
||||||
|
|
||||||
|
extension Model {
|
||||||
|
static public func mvmdecode<K:CodingKey>(keyedContainer: KeyedDecodingContainer<K>, codingKey: K) throws -> Self? {
|
||||||
|
let m = try keyedContainer.decodeIfPresent(self, forKey: codingKey)
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
static public func mvmdecode(unkeyedContainer: inout UnkeyedDecodingContainer) throws -> Self? {
|
||||||
|
let m = try unkeyedContainer.decodeIfPresent(self)
|
||||||
|
return m
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -24,28 +24,6 @@ public struct ModelRegistry {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension UnkeyedDecodingContainer {
|
|
||||||
|
|
||||||
public mutating func decodeUnKeyedIfPresent<T, C:CodingKey>(_ type: T.Type, typeCodingKey: C) throws -> T? where T : Decodable {
|
|
||||||
let meta = try self.nestedContainer(keyedBy: C.self)
|
|
||||||
guard let type = try meta.decodeIfPresent(String.self, forKey: typeCodingKey) else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//get the type
|
|
||||||
guard let found = ModelRegistry.types[type] else {
|
|
||||||
throw ModelRegistry.Error.decoderErrorModelNotMapped
|
|
||||||
}
|
|
||||||
let model = try self.decode(found)
|
|
||||||
|
|
||||||
guard let m = model as? T else {
|
|
||||||
throw ModelRegistry.Error.decoderError
|
|
||||||
}
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
extension KeyedDecodingContainer where Key: CodingKey {
|
extension KeyedDecodingContainer where Key: CodingKey {
|
||||||
|
|
||||||
private enum TypeCodingKey: String, CodingKey {
|
private enum TypeCodingKey: String, CodingKey {
|
||||||
@ -79,17 +57,46 @@ extension KeyedDecodingContainer where Key: CodingKey {
|
|||||||
throw ModelRegistry.Error.decoderErrorModelNotMapped
|
throw ModelRegistry.Error.decoderErrorModelNotMapped
|
||||||
}
|
}
|
||||||
|
|
||||||
//get the decoder for the propertyKey
|
|
||||||
let decoder = try self.superDecoder(forKey: codingKey)
|
|
||||||
|
|
||||||
//decode the type using the decoder
|
//decode the type using the decoder
|
||||||
let model = try found.init(from: decoder)
|
let model = try found.mvmdecode(keyedContainer: self, codingKey: codingKey)
|
||||||
|
|
||||||
guard let m = model as? T else {
|
guard let m = model as? T else {
|
||||||
throw ModelRegistry.Error.decoderError
|
throw ModelRegistry.Error.decoderError
|
||||||
}
|
}
|
||||||
|
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public func decodeArrayIfPresent<C:CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: C) throws -> [Model]? {
|
||||||
|
|
||||||
|
var unkeyedContainer = try nestedUnkeyedContainer(forKey: codingKey)
|
||||||
|
var otherUnkeyedContainer = try nestedUnkeyedContainer(forKey: codingKey)
|
||||||
|
var attributes = [Model.Type]()
|
||||||
|
while !unkeyedContainer.isAtEnd {
|
||||||
|
|
||||||
|
let meta = try unkeyedContainer.nestedContainer(keyedBy: C.self)
|
||||||
|
guard let type = try meta.decodeIfPresent(String.self, forKey: typeCodingKey) else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
//get the type
|
||||||
|
guard let found = ModelRegistry.types[type] else {
|
||||||
|
throw ModelRegistry.Error.decoderErrorModelNotMapped
|
||||||
|
}
|
||||||
|
attributes.append(found)
|
||||||
|
}
|
||||||
|
|
||||||
|
var i = 0
|
||||||
|
var models = [Model]()
|
||||||
|
while !otherUnkeyedContainer.isAtEnd {
|
||||||
|
let foundModel = attributes[i]
|
||||||
|
if let model = try foundModel.mvmdecode(unkeyedContainer: &otherUnkeyedContainer) {
|
||||||
|
models.append(model)
|
||||||
|
}
|
||||||
|
i += 1
|
||||||
|
}
|
||||||
|
return models.count > 0 ? models : nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user