Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/nmccann/ObjectMapper into…
Browse files Browse the repository at this point in the history
… nmccann-master
  • Loading branch information
tristanhimmelman committed Oct 12, 2016
2 parents 833c761 + 42e5d22 commit 5eabd64
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 44 deletions.
7 changes: 7 additions & 0 deletions Sources/Mapper.swift
Expand Up @@ -102,6 +102,13 @@ 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
97 changes: 53 additions & 44 deletions Tests/ImmutableTests.swift
Expand Up @@ -31,53 +31,54 @@ import XCTest
import ObjectMapper

class ImmutableObjectTests: XCTestCase {
let JSON: [String: Any] = [

// Basic types
"prop1": "Immutable!",
"prop2": 255,
"prop3": true,
// prop4 has a default value

// String
"prop5": "prop5",
"prop6": "prop6",
"prop7": "prop7",

// [String]
"prop8": ["prop8"],
"prop9": ["prop9"],
"prop10": ["prop10"],

// [String: String]
"prop11": ["key": "prop11"],
"prop12": ["key": "prop12"],
"prop13": ["key": "prop13"],

// Base
"prop14": ["base": "prop14"],
"prop15": ["base": "prop15"],
"prop16": ["base": "prop16"],

// [Base]
"prop17": [["base": "prop17"]],
"prop18": [["base": "prop18"]],
"prop19": [["base": "prop19"]],

// [String: Base]
"prop20": ["key": ["base": "prop20"]],
"prop21": ["key": ["base": "prop21"]],
"prop22": ["key": ["base": "prop22"]],

// Optional with immutables
"prop23": "Optional",
"prop24": 255,
"prop25": true,
"prop26": 255.0,
]

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

// Basic types
"prop1": "Immutable!",
"prop2": 255,
"prop3": true,
// prop4 has a default value

// String
"prop5": "prop5",
"prop6": "prop6",
"prop7": "prop7",

// [String]
"prop8": ["prop8"],
"prop9": ["prop9"],
"prop10": ["prop10"],

// [String: String]
"prop11": ["key": "prop11"],
"prop12": ["key": "prop12"],
"prop13": ["key": "prop13"],

// Base
"prop14": ["base": "prop14"],
"prop15": ["base": "prop15"],
"prop16": ["base": "prop16"],

// [Base]
"prop17": [["base": "prop17"]],
"prop18": [["base": "prop18"]],
"prop19": [["base": "prop19"]],

// [String: Base]
"prop20": ["key": ["base": "prop20"]],
"prop21": ["key": ["base": "prop21"]],
"prop22": ["key": ["base": "prop22"]],

// Optional with immutables
"prop23": "Optional",
"prop24": 255,
"prop25": true,
"prop26": 255.0,
]


let immutable: Struct = try! mapper.map(JSON: JSON)
XCTAssertNotNil(immutable)
Expand Down Expand Up @@ -126,6 +127,14 @@ class ImmutableObjectTests: XCTestCase {
assertImmutableObjectsEqual(objectFromJSON!, immutable)
}

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

let array: [Struct] = mapper.mapArray(JSONArray: JSONArray) ?? []
XCTAssertNotNil(array.first)
}

}

struct Struct {
Expand Down

0 comments on commit 5eabd64

Please sign in to comment.