additions

This commit is contained in:
Suresh, Kamlesh 2019-12-06 15:08:09 -05:00
parent 72758675dc
commit 2105133c38
2 changed files with 14 additions and 0 deletions

View File

@ -35,6 +35,15 @@ extension KeyedDecodingContainerProtocol {
}
extension Decodable {
public static func decode(jsonDict: [String: Any]) throws -> Self {
let jsonData = try JSONSerialization.data(withJSONObject: jsonDict)
do {
return try jsonData.decode()
} catch {
throw JSONError.other(error: error)
}
}
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

View File

@ -22,6 +22,10 @@ public struct ModelRegistry {
public static func register<M: Model>(_ type: M.Type) {
types[M.identifier] = type
}
public static func getType(for name: String) -> Model.Type? {
return types[name]
}
}
extension KeyedDecodingContainer where Key: CodingKey {
@ -50,6 +54,7 @@ extension KeyedDecodingContainer where Key: CodingKey {
//get the type
guard let found = ModelRegistry.types[type] else {
MVMCoreLoggingHandler.logDebugMessage(withDelegate: "Model not mapped: \(type)")
throw ModelRegistry.Error.decoderErrorModelNotMapped
}