Skip to content

Commit

Permalink
- added throwing mapArray functions to Mapper ImmutableMappable exten…
Browse files Browse the repository at this point in the history
…sion
  • Loading branch information
tristanhimmelman committed Oct 12, 2016
1 parent 5eabd64 commit e98adbf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
30 changes: 28 additions & 2 deletions Sources/ImmutableMappable.swift
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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]'")
}
Expand Down
7 changes: 0 additions & 7 deletions Sources/Mapper.swift
Expand Up @@ -102,13 +102,6 @@ public final class Mapper<N: BaseMappable> {
}
}

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 {
Expand Down
3 changes: 1 addition & 2 deletions Tests/ImmutableTests.swift
Expand Up @@ -128,10 +128,9 @@ class ImmutableObjectTests: XCTestCase {
}

func testMappingFromArray() {
let mapper = Mapper<Struct>()
let JSONArray: [[String: Any]] = [JSON]

let array: [Struct] = mapper.mapArray(JSONArray: JSONArray) ?? []
let array: [Struct] = try! Mapper<Struct>().mapArray(JSONArray: JSONArray)
XCTAssertNotNil(array.first)
}

Expand Down

0 comments on commit e98adbf

Please sign in to comment.