123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- public protocol ImmutableMappable: BaseMappable {
- init(map: Map) throws
- }
- public extension ImmutableMappable {
-
-
- func mapping(map: Map) {}
-
-
- init(JSONString: String, context: MapContext? = nil) throws {
- self = try Mapper(context: context).map(JSONString: JSONString)
- }
-
-
- init(JSON: [String: Any], context: MapContext? = nil) throws {
- self = try Mapper(context: context).map(JSON: JSON)
- }
-
-
- init(JSONObject: Any, context: MapContext? = nil) throws {
- self = try Mapper(context: context).map(JSONObject: JSONObject)
- }
-
- }
- public extension Map {
- fileprivate func currentValue(for key: String, nested: Bool? = nil, delimiter: String = ".") -> Any? {
- let isNested = nested ?? key.contains(delimiter)
- return self[key, nested: isNested, delimiter: delimiter].currentValue
- }
-
-
-
- func value<T>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> T {
- let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
- guard let value = currentValue as? T else {
- throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '\(T.self)'", file: file, function: function, line: line)
- }
- return value
- }
-
- func value<Transform: TransformType>(_ key: String, nested: Bool? = nil, delimiter: String = ".", using transform: Transform, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> Transform.Object {
- let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
- guard let value = transform.transformFromJSON(currentValue) else {
- throw MapError(key: key, currentValue: currentValue, reason: "Cannot transform to '\(Transform.Object.self)' using \(transform)", file: file, function: function, line: line)
- }
- return value
- }
-
-
- func value<T: RawRepresentable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> T {
- return try self.value(key, nested: nested, delimiter: delimiter, using: EnumTransform(), file: file, function: function, line: line)
- }
-
-
- func value<T: RawRepresentable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> T? {
- return try self.value(key, nested: nested, delimiter: delimiter, using: EnumTransform(), file: file, function: function, line: line)
- }
-
- func value<T: RawRepresentable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [T] {
- return try self.value(key, nested: nested, delimiter: delimiter, using: EnumTransform(), file: file, function: function, line: line)
- }
-
- func value<T: RawRepresentable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [T]? {
- return try self.value(key, nested: nested, delimiter: delimiter, using: EnumTransform(), file: file, function: function, line: line)
- }
-
-
- func value<T: BaseMappable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> T {
- let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
- guard let JSONObject = currentValue else {
- throw MapError(key: key, currentValue: currentValue, reason: "Found unexpected nil value", file: file, function: function, line: line)
- }
- return try Mapper<T>(context: context).mapOrFail(JSONObject: JSONObject)
- }
-
-
- func value<T: BaseMappable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> T? {
- let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
- guard let JSONObject = currentValue else {
- throw MapError(key: key, currentValue: currentValue, reason: "Found unexpected nil value", file: file, function: function, line: line)
- }
- return try Mapper<T>(context: context).mapOrFail(JSONObject: JSONObject)
- }
-
-
- func value<T: BaseMappable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [T] {
- let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
- guard let jsonArray = currentValue as? [Any] else {
- throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[Any]'", file: file, function: function, line: line)
- }
-
- return try jsonArray.map { JSONObject -> T in
- return try Mapper<T>(context: context).mapOrFail(JSONObject: JSONObject)
- }
- }
-
-
- func value<T: BaseMappable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [T]? {
- let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
- guard let jsonArray = currentValue as? [Any] else {
- throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[Any]'", file: file, function: function, line: line)
- }
-
- return try jsonArray.map { JSONObject -> T in
- return try Mapper<T>(context: context).mapOrFail(JSONObject: JSONObject)
- }
- }
-
- func value<Transform: TransformType>(_ key: String, nested: Bool? = nil, delimiter: String = ".", using transform: Transform, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [Transform.Object] {
- let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
- guard let jsonArray = currentValue as? [Any] else {
- throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[Any]'", file: file, function: function, line: line)
- }
-
- return try jsonArray.map { json -> Transform.Object in
- guard let object = transform.transformFromJSON(json) else {
- throw MapError(key: "\(key)", currentValue: json, reason: "Cannot transform to '\(Transform.Object.self)' using \(transform)", file: file, function: function, line: line)
- }
- return object
- }
- }
-
-
- func value<T: BaseMappable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [String: T] {
-
- let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
- guard let jsonDictionary = currentValue as? [String: Any] else {
- throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[String: Any]'", file: file, function: function, line: line)
- }
- return try jsonDictionary.mapValues { json in
- return try Mapper<T>(context: context).mapOrFail(JSONObject: json)
- }
- }
-
- func value<T: BaseMappable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [String: T]? {
-
- let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
- guard let jsonDictionary = currentValue as? [String: Any] else {
- throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[String: Any]'", file: file, function: function, line: line)
- }
- var value: [String: T] = [:]
- for (key, json) in jsonDictionary {
- value[key] = try Mapper<T>(context: context).mapOrFail(JSONObject: json)
- }
- return value
- }
-
- func value<Transform: TransformType>(_ key: String, nested: Bool? = nil, delimiter: String = ".", using transform: Transform, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [String: Transform.Object] {
-
- let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
- guard let jsonDictionary = currentValue as? [String: Any] else {
- throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[String: Any]'", file: file, function: function, line: line)
- }
- return try jsonDictionary.mapValues { json in
- guard let object = transform.transformFromJSON(json) else {
- throw MapError(key: key, currentValue: json, reason: "Cannot transform to '\(Transform.Object.self)' using \(transform)", file: file, function: function, line: line)
- }
- return object
- }
- }
-
-
- func value<T: BaseMappable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [[T]]? {
-
- let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
- guard let json2DArray = currentValue as? [[Any]] else {
- throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[[Any]]'", file: file, function: function, line: line)
- }
- return try json2DArray.map { jsonArray in
- try jsonArray.map { jsonObject -> T in
- return try Mapper<T>(context: context).mapOrFail(JSONObject: jsonObject)
- }
- }
- }
-
-
-
- func value<T: BaseMappable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [[T]] {
-
- let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
- guard let json2DArray = currentValue as? [[Any]] else {
- throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[[Any]]'", file: file, function: function, line: line)
- }
- return try json2DArray.map { jsonArray in
- try jsonArray.map { jsonObject -> T in
- return try Mapper<T>(context: context).mapOrFail(JSONObject: jsonObject)
- }
- }
- }
-
-
- func value<Transform: TransformType>(_ key: String, nested: Bool? = nil, delimiter: String = ".", using transform: Transform, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [[Transform.Object]] {
-
- let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
- guard let json2DArray = currentValue as? [[Any]] else {
- throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[[Any]]'",
- file: file, function: function, line: line)
- }
-
- return try json2DArray.map { jsonArray in
- try jsonArray.map { json -> Transform.Object in
- guard let object = transform.transformFromJSON(json) else {
- throw MapError(key: "\(key)", currentValue: json, reason: "Cannot transform to '\(Transform.Object.self)' using \(transform)", file: file, function: function, line: line)
- }
- return object
- }
- }
- }
- }
- public extension Mapper where N: ImmutableMappable {
-
- func map(JSON: [String: Any]) throws -> N {
- return try self.mapOrFail(JSON: JSON)
- }
-
- func map(JSONString: String) throws -> N {
- return try mapOrFail(JSONString: JSONString)
- }
-
- func map(JSONObject: Any) throws -> N {
- return try mapOrFail(JSONObject: JSONObject)
- }
-
-
-
- func mapArray(JSONArray: [[String: Any]]) throws -> [N] {
- #if swift(>=4.1)
- return try JSONArray.compactMap(mapOrFail)
- #else
- return try JSONArray.flatMap(mapOrFail)
- #endif
- }
-
- func mapArray(JSONString: String) throws -> [N] {
- guard let JSONObject = Mapper.parseJSONString(JSONString: JSONString) else {
- throw MapError(key: nil, currentValue: JSONString, reason: "Cannot convert string into Any'")
- }
-
- return try mapArray(JSONObject: JSONObject)
- }
-
- func mapArray(JSONObject: Any) throws -> [N] {
- guard let JSONArray = JSONObject as? [[String: Any]] else {
- throw MapError(key: nil, currentValue: JSONObject, reason: "Cannot cast to '[[String: Any]]'")
- }
-
- return try mapArray(JSONArray: JSONArray)
- }
-
- func mapDictionary(JSONString: String) throws -> [String: N] {
- guard let JSONObject = Mapper.parseJSONString(JSONString: JSONString) else {
- throw MapError(key: nil, currentValue: JSONString, reason: "Cannot convert string into Any'")
- }
- return try mapDictionary(JSONObject: JSONObject)
- }
- func mapDictionary(JSONObject: Any?) throws -> [String: N] {
- guard let JSON = JSONObject as? [String: [String: Any]] else {
- throw MapError(key: nil, currentValue: JSONObject, reason: "Cannot cast to '[String: [String: Any]]''")
- }
- return try mapDictionary(JSON: JSON)
- }
- func mapDictionary(JSON: [String: [String: Any]]) throws -> [String: N] {
- return try JSON.filterMap(mapOrFail)
- }
-
- func mapDictionaryOfArrays(JSONObject: Any?) throws -> [String: [N]] {
- guard let JSON = JSONObject as? [String: [[String: Any]]] else {
- throw MapError(key: nil, currentValue: JSONObject, reason: "Cannot cast to '[String: [String: Any]]''")
- }
- return try mapDictionaryOfArrays(JSON: JSON)
- }
- func mapDictionaryOfArrays(JSON: [String: [[String: Any]]]) throws -> [String: [N]] {
- return try JSON.filterMap { array -> [N] in
- try mapArray(JSONArray: array)
- }
- }
-
- func mapArrayOfArrays(JSONObject: Any?) throws -> [[N]] {
- guard let JSONArray = JSONObject as? [[[String: Any]]] else {
- throw MapError(key: nil, currentValue: JSONObject, reason: "Cannot cast to '[[[String: Any]]]''")
- }
- return try JSONArray.map(mapArray)
- }
- }
- internal extension Mapper {
- func mapOrFail(JSON: [String: Any]) throws -> N {
- let map = Map(mappingType: .fromJSON, JSON: JSON, context: context, shouldIncludeNilValues: shouldIncludeNilValues)
-
-
- if let klass = N.self as? ImmutableMappable.Type,
- var object = try klass.init(map: map) as? N {
- object.mapping(map: map)
- return object
- }
-
-
- guard let value = self.map(JSON: JSON) else {
- throw MapError(key: nil, currentValue: JSON, reason: "Cannot map to '\(N.self)'")
- }
- return value
- }
- func mapOrFail(JSONString: String) throws -> N {
- guard let JSON = Mapper.parseJSONStringIntoDictionary(JSONString: JSONString) else {
- throw MapError(key: nil, currentValue: JSONString, reason: "Cannot parse into '[String: Any]'")
- }
- return try mapOrFail(JSON: JSON)
- }
- func mapOrFail(JSONObject: Any) throws -> N {
- guard let JSON = JSONObject as? [String: Any] else {
- throw MapError(key: nil, currentValue: JSONObject, reason: "Cannot cast to '[String: Any]'")
- }
- return try mapOrFail(JSON: JSON)
- }
- }
|