additions
This commit is contained in:
parent
72758675dc
commit
2105133c38
@ -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
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user