update decodearray and encodearray method
This commit is contained in:
parent
d7a7425bb3
commit
ffc71ae0e2
@ -104,8 +104,31 @@ extension KeyedDecodingContainer where Key: CodingKey {
|
||||
}
|
||||
|
||||
public func decodeArray<C:CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: C) throws -> [Model] {
|
||||
guard let models: [Model] = try decodeArrayIfPresent(codingKey: codingKey, typeCodingKey: typeCodingKey) else {
|
||||
return [Model]()
|
||||
var unkeyedContainer: UnkeyedDecodingContainer?
|
||||
do {
|
||||
unkeyedContainer = try nestedUnkeyedContainer(forKey: codingKey)
|
||||
} catch {
|
||||
throw ModelRegistry.Error.decoderErrorObjectNotPresent
|
||||
}
|
||||
var otherUnkeyedContainer = try nestedUnkeyedContainer(forKey: codingKey)
|
||||
var attributes = [Model.Type]()
|
||||
while !unkeyedContainer!.isAtEnd {
|
||||
let meta = try unkeyedContainer!.nestedContainer(keyedBy: C.self)
|
||||
let type = try meta.decode(String.self, forKey: typeCodingKey)
|
||||
//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
|
||||
}
|
||||
@ -138,6 +161,10 @@ extension KeyedEncodingContainer where Key: CodingKey {
|
||||
|
||||
public mutating func encodeArrayIfPresent(_ list:[Model]?, forKey key:KeyedEncodingContainer<K>.Key) throws {
|
||||
guard let models = list else { return }
|
||||
try self.encodeArray(models, forKey: key)
|
||||
do {
|
||||
try self.encodeArray(models, forKey: key)
|
||||
} catch {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user