From e98adbffcb1e0dc9f995de4805cc01c236415cf7 Mon Sep 17 00:00:00 2001 From: Tristan Himmelman Date: Tue, 11 Oct 2016 20:59:29 -0400 Subject: [PATCH] - added throwing mapArray functions to Mapper ImmutableMappable extension --- Sources/ImmutableMappable.swift | 30 ++++++++++++++++++++++++++++-- Sources/Mapper.swift | 7 ------- Tests/ImmutableTests.swift | 3 +-- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/Sources/ImmutableMappable.swift b/Sources/ImmutableMappable.swift index ee677704..468936dc 100644 --- a/Sources/ImmutableMappable.swift +++ b/Sources/ImmutableMappable.swift @@ -45,6 +45,11 @@ public extension ImmutableMappable { self = try Mapper(context: context).map(JSON: JSON) } + /// Initializes object from a JSONObject + public init(JSONObject: Any, context: MapContext? = nil) throws { + self = try Mapper(context: context).map(JSONObject: JSONObject) + } + } public extension Map { @@ -151,10 +156,31 @@ public extension Mapper where N: ImmutableMappable { return try mapOrFail(JSONString: JSONString) } - public func map(JSONObject: Any?) throws -> N { + public func map(JSONObject: Any) throws -> N { return try mapOrFail(JSONObject: JSONObject) } + // MARK: Array mapping functions + + public func mapArray(JSONArray: [[String: Any]]) throws -> [N] { + return try JSONArray.flatMap(mapOrFail) + } + + public 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) + } + + public 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) + } } internal extension Mapper where N: BaseMappable { @@ -183,7 +209,7 @@ internal extension Mapper where N: BaseMappable { return try mapOrFail(JSON: JSON) } - internal func mapOrFail(JSONObject: Any?) throws -> N { + internal 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]'") } diff --git a/Sources/Mapper.swift b/Sources/Mapper.swift index 506056a7..bab280b3 100755 --- a/Sources/Mapper.swift +++ b/Sources/Mapper.swift @@ -102,13 +102,6 @@ public final class Mapper { } } - if let klass = N.self as? ImmutableMappable.Type { - if let maybeObject = try? klass.init(map: map) as? N, var object = maybeObject { - object.mapping(map: map) - return object - } - } - // fall back to using init? to create N if let klass = N.self as? Mappable.Type { if var object = klass.init(map: map) as? N { diff --git a/Tests/ImmutableTests.swift b/Tests/ImmutableTests.swift index 84975b5c..aa31933f 100644 --- a/Tests/ImmutableTests.swift +++ b/Tests/ImmutableTests.swift @@ -128,10 +128,9 @@ class ImmutableObjectTests: XCTestCase { } func testMappingFromArray() { - let mapper = Mapper() let JSONArray: [[String: Any]] = [JSON] - let array: [Struct] = mapper.mapArray(JSONArray: JSONArray) ?? [] + let array: [Struct] = try! Mapper().mapArray(JSONArray: JSONArray) XCTAssertNotNil(array.first) }