Separate action and molecule name

This commit is contained in:
panxi 2019-11-13 18:02:40 -05:00
parent 16c7b61108
commit d38b521c66

View File

@ -26,14 +26,13 @@ public struct ModelRegistry {
extension KeyedDecodingContainer where Key : CodingKey{ extension KeyedDecodingContainer where Key : CodingKey{
public enum TypeCodingKey: String, CodingKey {
//need to discuss this the enum private enum TypeCodingKey: String, CodingKey {
case moleculeName
case actionMap case actionMap
} }
//MARK: - Decode //MARK: - Decode
public func decode<T>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: TypeCodingKey) throws -> T { public func decode<T, C:CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: C) throws -> T {
guard let model: Model = try decodeIfPresent(codingKey: codingKey, typeCodingKey: typeCodingKey) else { guard let model: Model = try decodeIfPresent(codingKey: codingKey, typeCodingKey: typeCodingKey) else {
throw ModelRegistry.Error.decoderErrorObjectNotPresent throw ModelRegistry.Error.decoderErrorObjectNotPresent
} }
@ -46,14 +45,14 @@ extension KeyedDecodingContainer where Key : CodingKey{
} }
public func decode(codingKey: KeyedDecodingContainer<K>.Key) throws -> ActionMapModel { public func decode(codingKey: KeyedDecodingContainer<K>.Key) throws -> ActionMapModel {
return try decode(codingKey: codingKey, typeCodingKey: .actionMap) return try decode(codingKey: codingKey, typeCodingKey: TypeCodingKey.actionMap)
} }
//MARK: - DecodeIfPresent //MARK: - DecodeIfPresent
public func decodeIfPresent<T>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: TypeCodingKey) throws -> T? { public func decodeIfPresent<T, C:CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: C) throws -> T? {
//get the type string //get the type string
let meta = try self.nestedContainer(keyedBy: TypeCodingKey.self, forKey: codingKey) let meta = try self.nestedContainer(keyedBy: type(of: typeCodingKey), forKey: codingKey)
guard let type = try meta.decodeIfPresent(String.self, forKey: typeCodingKey) else { guard let type = try meta.decodeIfPresent(String.self, forKey: typeCodingKey) else {
return nil return nil
} }
@ -76,7 +75,7 @@ extension KeyedDecodingContainer where Key : CodingKey{
} }
public func decodeIfPresent(codingKey: KeyedDecodingContainer<K>.Key) throws -> ActionMapModel? { public func decodeIfPresent(codingKey: KeyedDecodingContainer<K>.Key) throws -> ActionMapModel? {
return try decodeIfPresent(codingKey: codingKey, typeCodingKey: .actionMap) return try decodeIfPresent(codingKey: codingKey, typeCodingKey: TypeCodingKey.actionMap)
} }
} }