integrating changes to make fan out instance models into categories due to name conflicts

This commit is contained in:
Kevin G Christiano 2020-02-12 11:25:03 -05:00
parent 605959a58f
commit 53e05ca42f
10 changed files with 187 additions and 57 deletions

View File

@ -31,6 +31,7 @@
01F2A04E23A82CF500D954D8 /* ActionPopupModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01F2A04D23A82CF500D954D8 /* ActionPopupModel.swift */; };
01F2A05023A82D0800D954D8 /* ActionTopAlertModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01F2A04F23A82D0800D954D8 /* ActionTopAlertModel.swift */; };
01F2A05223A8325100D954D8 /* ModelMapping.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01F2A05123A8325100D954D8 /* ModelMapping.swift */; };
0A42538F23F3414800554656 /* Codable+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A42538E23F3414800554656 /* Codable+Helpers.swift */; };
30349BF11FCCA78A00546A1E /* MVMCoreSessionTimeHandler.h in Headers */ = {isa = PBXBuildFile; fileRef = 30349BEF1FCCA78A00546A1E /* MVMCoreSessionTimeHandler.h */; settings = {ATTRIBUTES = (Public, ); }; };
30349BF21FCCA78A00546A1E /* MVMCoreSessionTimeHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = 30349BF01FCCA78A00546A1E /* MVMCoreSessionTimeHandler.m */; };
881D26931FCC9D180079C521 /* MVMCoreErrorObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 881D268F1FCC9D180079C521 /* MVMCoreErrorObject.m */; };
@ -177,6 +178,7 @@
01F2A05123A8325100D954D8 /* ModelMapping.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModelMapping.swift; sourceTree = "<group>"; };
0A11030B20864F94008ADD90 /* es */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = es; path = es.lproj/Localizable.strings; sourceTree = "<group>"; };
0A11030C20864F9A008ADD90 /* es-MX */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "es-MX"; path = "es-MX.lproj/Localizable.strings"; sourceTree = "<group>"; };
0A42538E23F3414800554656 /* Codable+Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Codable+Helpers.swift"; sourceTree = "<group>"; };
30349BEF1FCCA78A00546A1E /* MVMCoreSessionTimeHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MVMCoreSessionTimeHandler.h; sourceTree = "<group>"; };
30349BF01FCCA78A00546A1E /* MVMCoreSessionTimeHandler.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MVMCoreSessionTimeHandler.m; sourceTree = "<group>"; };
881D268F1FCC9D180079C521 /* MVMCoreErrorObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MVMCoreErrorObject.m; sourceTree = "<group>"; };
@ -429,6 +431,7 @@
children = (
946EE1AA237B5C940036751F /* Decoder.swift */,
946EE1B3237B619D0036751F /* Encoder.swift */,
0A42538E23F3414800554656 /* Codable+Helpers.swift */,
);
path = Extensions;
sourceTree = "<group>";
@ -894,6 +897,7 @@
946EE1AB237B5C940036751F /* Decoder.swift in Sources */,
946EE1BC237B691A0036751F /* ActionOpenPageModel.swift in Sources */,
AFBB96A61FBA3A9A0008D868 /* MVMCoreTopAlertOperation.m in Sources */,
0A42538F23F3414800554656 /* Codable+Helpers.swift in Sources */,
881D26931FCC9D180079C521 /* MVMCoreErrorObject.m in Sources */,
946EE1B0237B5EF70036751F /* JSONHelper.swift in Sources */,
30349BF21FCCA78A00546A1E /* MVMCoreSessionTimeHandler.m in Sources */,

View File

@ -13,10 +13,29 @@ public enum ActionCodingKey: String, CodingKey {
}
public protocol ActionModelProtocol: Model {
var actionType: String? { get set }
var extraParameters: JSONValueDictionary? { get set }
var analyticsData: JSONValueDictionary? { get set }
static var categoryName: String { get }
static var categoryCodingKey: String { get }
// Temporary fix till server changes
var title: String? { get set }
}
extension ActionModelProtocol {
public var actionType: String? {
get { return Self.identifier }
}
public static var categoryCodingKey: String {
return "actionType"
}
public static var categoryName: String {
return "\(ActionModelProtocol.self)"
}
}

View File

@ -8,7 +8,7 @@
import Foundation
@objcMembers public class ActionOpenAppModel: ActionModelProtocol {
@objcMembers public class ActionOpenAppModel: ActionModelProtocol {
public static var identifier: String = "openApp"
public var actionType: String?
public var appURL: String

View File

@ -9,6 +9,7 @@
import Foundation
@objcMembers public class ActionPopupModel: ActionModelProtocol {
public static var identifier: String = "popup"
public var actionType: String?
public var title: String?

View File

@ -9,6 +9,7 @@
import Foundation
@objcMembers public class ActionTopAlertModel: ActionModelProtocol {
public static var identifier: String = "topAlert"
public var actionType: String?
public var pageType: String

View File

@ -0,0 +1,45 @@
//
// Codable+Helpers.swift
// MVMCore
//
// Created by Kevin Christiano on 2/11/20.
// Copyright © 2020 myverizon. All rights reserved.
//
import Foundation
// MARK: - Date formatters
/// Protocol acting as a common API for all types of date formatters,
/// such as `DateFormatter` and `ISO8601DateFormatter`.
public protocol AnyDateFormatter {
/// Format a string into a date
func date(from string: String) -> Date?
/// Format a date into a string
func string(from date: Date) -> String
}
extension DateFormatter: AnyDateFormatter {}
@available(iOS 10.0, macOS 10.12, tvOS 10.0, *)
extension ISO8601DateFormatter: AnyDateFormatter {}
// MARK: supporting types
public struct AnyCodingKey: CodingKey {
public var stringValue: String
public var intValue: Int?
public init(_ string: String) {
stringValue = string
}
public init?(stringValue: String) {
self.stringValue = stringValue
}
public init?(intValue: Int) {
self.intValue = intValue
self.stringValue = String(intValue)
}
}

View File

@ -10,19 +10,30 @@ import Foundation
public protocol Model: Codable {
/// The key name of the molecule
static var identifier: String { get }
/// Name of the defining object.
static var categoryName: String { get }
/// The JSON key name of the
static var categoryCodingKey: String { get }
/// Convenience function to decode to model using a keyed container.
static func decode<K: CodingKey>(keyedContainer: KeyedDecodingContainer<K>, codingKey: K) throws -> Self?
/// Convenience function to decode to model using an unkeyed container.
static func decode(unkeyedContainer: inout UnkeyedDecodingContainer) throws -> Self?
/// Convenience function to encode model using a keyed container.
func encode<K: CodingKey>(keyedContainer: inout KeyedEncodingContainer<K>, codingKey: K) throws
/// Convenience function to encode model using an unkeyed container.
func encode(unkeyedContainer: inout UnkeyedEncodingContainer) throws
}
extension Model {
static public func decode<K: CodingKey>(keyedContainer: KeyedDecodingContainer<K>, codingKey: K) throws -> Self? {
return try keyedContainer.decodeIfPresent(self, forKey: codingKey)
}

View File

@ -10,36 +10,78 @@
import Foundation
public struct ModelRegistry {
public enum Error: Swift.Error {
case keyNotFound
case encoderError
case decoderError
case decoderOther(message: String)
case decoderErrorObjectNotPresent
case decoderErrorModelNotMapped
case other(message: String)
}
fileprivate static var types = [String: Model.Type]()
/// Register the model.
public static func register<M: Model>(_ type: M.Type) {
types[M.identifier] = type
private struct Category {
var name: String
var codingKey: String
var instanceTypes: [String: Model.Type] = [:]
}
public static func getType(for name: String) -> Model.Type? {
return types[name]
private static var categories: [String: Category] = [:]
/// Registers models for Atomic use.
public static func register<M: Model>(_ type: M.Type) throws {
var category: Category
print("\n\n")
print("HI= \(M.identifier)")
print("HI= \(M.categoryName)")
print("HI= \(M.categoryCodingKey)")
print("\n\n")
if let c = categories[M.categoryName] {
category = c
} else {
category = Category(name: M.categoryName, codingKey: M.categoryCodingKey)
}
// Check to ensure the Category/Type combination doesn't exist.
if category.instanceTypes[M.identifier] != nil {
throw ModelRegistry.Error.other(message: "Model: \(M.identifier) already exists in Category: \(M.categoryName)")
}
category.instanceTypes[M.identifier] = type
categories[M.categoryName] = category
}
private static func getCategory<T>(for type: T.Type) -> Category? {
return categories["\(T.self)"]
}
public static func getType<T>(for name: String, with type: T.Type) -> Model.Type? {
return getCategory(for: type)?.instanceTypes[name]
}
public static func getCodingKey<T>(for type: T.Type) throws -> AnyCodingKey{
guard let category = getCategory(for: type) else {
throw ModelRegistry.Error.decoderOther(message: "decodeModelsIfPresent only works for objects implementing the Model protocol")
}
return AnyCodingKey(category.codingKey)
}
}
extension KeyedDecodingContainer where Key: CodingKey {
//MARK: - Decode
// MARK: - Decode
/// Decodes to a registered model based on the identifier
public func decodeModel<M, TypeKey: CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: TypeKey) throws -> M {
guard let model: Model = try decodeModelIfPresent(codingKey: codingKey, typeCodingKey: typeCodingKey) else {
MVMCoreLoggingHandler.logDebugMessage(withDelegate: "ModelRegistry Error decoderErrorObjectNotPresent: \(codingKey)")
throw ModelRegistry.Error.decoderErrorObjectNotPresent
}
if let model = model as? M {
return model
} else {
@ -48,25 +90,24 @@ extension KeyedDecodingContainer where Key: CodingKey {
}
}
//MARK: - DecodeIfPresent
// MARK: - DecodeIfPresent
/// Decodes to a registered model based on the identifier, optional.
public func decodeModelIfPresent<M, TypeKey: CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: TypeKey) throws -> M? {
//get the identifier string
guard contains(codingKey),
let container = try? nestedContainer(keyedBy: TypeKey.self, forKey: codingKey),
let identifier = try? container.decodeIfPresent(String.self, forKey: typeCodingKey) else {
return nil
}
//get the type
guard let type = ModelRegistry.getType(for: identifier) else {
let identifier = try? container.decodeIfPresent(String.self, forKey: typeCodingKey)
else { return nil }
guard let type = ModelRegistry.getType(for: identifier, with: M.self) else {
MVMCoreLoggingHandler.logDebugMessage(withDelegate: "Model not mapped: \(identifier)")
throw ModelRegistry.Error.decoderErrorModelNotMapped
}
//decode the type using the decoder
let model = try type.decode(keyedContainer: self, codingKey: codingKey)
if let model = model as? M {
return model
} else {
@ -78,12 +119,12 @@ extension KeyedDecodingContainer where Key: CodingKey {
/// Decodes an array of registered model based on the identifiers, optional.
public func decodeModelsIfPresent<TypeKey: CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: TypeKey) throws -> [Model]? {
guard contains(codingKey),
var container = try? nestedUnkeyedContainer(forKey: codingKey) else {
return nil
}
var container = try? nestedUnkeyedContainer(forKey: codingKey)
else { return nil }
return try container.decodeModelsIfPresent(typeCodingKey: typeCodingKey)
}
/// Decodes an array of registered model based on the identifiers.
public func decodeModels<TypeKey: CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: TypeKey) throws -> [Model] {
guard let models: [Model] = try decodeModelsIfPresent(codingKey: codingKey, typeCodingKey: typeCodingKey) else {
@ -96,9 +137,9 @@ extension KeyedDecodingContainer where Key: CodingKey {
/// Decodes an array with arrays of models based on the identifiers, optional.
public func decodeModels2DIfPresent<TypeKey: CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: TypeKey) throws -> [[Model]]? {
guard contains(codingKey),
var container = try? nestedUnkeyedContainer(forKey: codingKey) else {
return nil
}
var container = try? nestedUnkeyedContainer(forKey: codingKey)
else { return nil }
return try container.decodeModels2DIfPresent(typeCodingKey: typeCodingKey)
}
@ -112,40 +153,44 @@ extension KeyedDecodingContainer where Key: CodingKey {
}
}
extension KeyedEncodingContainer where Key: CodingKey {
public extension KeyedEncodingContainer where Key: CodingKey {
/// Encodes a model, optional.
public mutating func encodeModelIfPresent(_ value: Model?, forKey key: KeyedEncodingContainer<K>.Key) throws {
mutating func encodeModelIfPresent(_ value: Model?, forKey key: KeyedEncodingContainer<K>.Key) throws {
if let value = value {
let encoder = self.superEncoder(forKey: key)
let encoder = superEncoder(forKey: key)
try value.encode(to: encoder)
}
}
/// Encodes a model.
public mutating func encodeModel(_ value: Model, forKey key: KeyedEncodingContainer<K>.Key) throws {
let encoder = self.superEncoder(forKey: key)
mutating func encodeModel(_ value: Model, forKey key: KeyedEncodingContainer<K>.Key) throws {
let encoder = superEncoder(forKey: key)
try value.encode(to: encoder)
}
/// Encodes an array of models
public mutating func encodeModels(_ list: [Model], forKey key:KeyedEncodingContainer<K>.Key) throws {
mutating func encodeModels(_ list: [Model], forKey key:KeyedEncodingContainer<K>.Key) throws {
try encodeModelsIfPresent(list, forKey: key)
}
/// Encodes an array of models, optional, need instance type as input paramaeter list
public mutating func encodeModelsIfPresent(_ list: [Model]?, forKey key:KeyedEncodingContainer<K>.Key) throws {
mutating func encodeModelsIfPresent(_ list: [Model]?, forKey key:KeyedEncodingContainer<K>.Key) throws {
guard let list = list else { return }
var unkeyedContainer = self.nestedUnkeyedContainer(forKey: key)
var unkeyedContainer = nestedUnkeyedContainer(forKey: key)
for model in list {
try model.encode(unkeyedContainer: &unkeyedContainer)
}
}
/// Convenience function for encoding the container into a list of lists of Models. needs instance type as input paramaeter list
public mutating func encodeModels2D(_ list: [[Model]]?, forKey key:KeyedEncodingContainer<K>.Key) throws {
mutating func encodeModels2D(_ list: [[Model]]?, forKey key:KeyedEncodingContainer<K>.Key) throws {
guard let list = list else { return }
var unkeyedContainer = self.nestedUnkeyedContainer(forKey: key)
var unkeyedContainer = nestedUnkeyedContainer(forKey: key)
for models in list {
var arrayunkeyedContainer = unkeyedContainer.nestedUnkeyedContainer()
for model in models {
@ -155,20 +200,22 @@ extension KeyedEncodingContainer where Key: CodingKey {
}
}
extension UnkeyedDecodingContainer {
public extension UnkeyedDecodingContainer {
/// Decodes the container into a list of Models.
public mutating func decodeModelsIfPresent<TypeKey: CodingKey>(typeCodingKey: TypeKey) throws -> [Model]? {
mutating func decodeModelsIfPresent<TypeKey: CodingKey>(typeCodingKey: TypeKey) throws -> [Model]? {
var models = [Model]()
var containerCopy = self
while !containerCopy.isAtEnd {
let nestedContainer = try containerCopy.nestedContainer(keyedBy: TypeKey.self)
if let identifier = try nestedContainer.decodeIfPresent(String.self, forKey: typeCodingKey) {
//get the type
guard let type = ModelRegistry.getType(for: identifier) else {
guard let type = ModelRegistry.getType(for: identifier, with: TypeKey.self) else {
MVMCoreLoggingHandler.logDebugMessage(withDelegate: "ModelRegistry Error decoderErrorModelNotMapped: \(identifier)")
throw ModelRegistry.Error.decoderErrorModelNotMapped
}
//now get the decoder to use for the type
// Now get the decoder to use for the type
let decoder = try self.superDecoder()
let model = try type.init(from: decoder)
models.append(model)
@ -178,9 +225,11 @@ extension UnkeyedDecodingContainer {
}
/// Convenience function for decoding the container into a list of lists of Models.
public mutating func decodeModels2DIfPresent<TypeKey: CodingKey>(typeCodingKey: TypeKey) throws -> [[Model]]? {
mutating func decodeModels2DIfPresent<TypeKey: CodingKey>(typeCodingKey: TypeKey) throws -> [[Model]]? {
var modelLists = [[Model]]()
var containerCopy = self
while !containerCopy.isAtEnd {
var arraycontainerCopy = try containerCopy.nestedUnkeyedContainer()
guard let models = try arraycontainerCopy.decodeModelsIfPresent(typeCodingKey: typeCodingKey) else {

View File

@ -10,12 +10,12 @@ import Foundation
@objcMembers public class ModelMapping: NSObject {
public static func registerObjects() {
ModelRegistry.register(ActionOpenPageModel.self)
ModelRegistry.register(ActionOpenUrlModel.self)
ModelRegistry.register(ActionOpenAppModel.self)
ModelRegistry.register(ActionCallModel.self)
ModelRegistry.register(ActionPopupModel.self)
ModelRegistry.register(ActionTopAlertModel.self)
ModelRegistry.register(ActionBackModel.self)
try? ModelRegistry.register(ActionOpenPageModel.self)
try? ModelRegistry.register(ActionOpenUrlModel.self)
try? ModelRegistry.register(ActionOpenAppModel.self)
try? ModelRegistry.register(ActionCallModel.self)
try? ModelRegistry.register(ActionPopupModel.self)
try? ModelRegistry.register(ActionTopAlertModel.self)
try? ModelRegistry.register(ActionBackModel.self)
}
}

View File

@ -9,7 +9,7 @@
#import <Foundation/Foundation.h>
#import "MVMCoreRequestParameters.h"
#define ENABLE_HARD_CODED_RESPONSE 0 && DEBUG
#define ENABLE_HARD_CODED_RESPONSE 1 && DEBUG
#if ENABLE_HARD_CODED_RESPONSE