Skip to content

Commit

Permalink
Updated Mappable definition
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanhimmelman committed Jul 27, 2015
1 parent 75aa88c commit 9d2a5a6
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 141 deletions.
12 changes: 7 additions & 5 deletions ObjectMapper/Core/Mapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

public protocol Mappable {
init?(_ map: Map)
static func newInstance() -> Mappable
mutating func mapping(map: Map)
}

Expand All @@ -18,7 +18,6 @@ public enum MappingType {
case ToJSON
}


/// A class used for holding mapping data
public final class Map {
public let mappingType: MappingType
Expand Down Expand Up @@ -166,9 +165,12 @@ public final class Mapper<N: Mappable> {

/// Maps a JSON dictionary to an object that conforms to Mappable
public func map(JSONDictionary: [String : AnyObject]) -> N? {
let map = Map(mappingType: .FromJSON, JSONDictionary: JSONDictionary)
let object = N(map)
return object
if var object = N.newInstance() as? N {
let map = Map(mappingType: .FromJSON, JSONDictionary: JSONDictionary)
object.mapping(map)
return object
}
return nil
}

//MARK: Mapping functions for Arrays and Dictionaries
Expand Down
12 changes: 4 additions & 8 deletions ObjectMapperTests/BasicTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,8 @@ class BasicTypes: Mappable {
var dictEnumIntOptional: [String: EnumInt]?
var dictEnumIntImplicitlyUnwrapped: [String: EnumInt]!

init() {}

required init?(_ map: Map) {
mapping(map)
static func newInstance() -> Mappable {
return BasicTypes()
}

func mapping(map: Map) {
Expand Down Expand Up @@ -207,10 +205,8 @@ class TestCollectionOfPrimitives : Mappable {
var arrayDouble: [Double] = []
var arrayFloat: [Float] = []

init() {}

required init?(_ map: Map) {
mapping(map)
static func newInstance() -> Mappable {
return TestCollectionOfPrimitives()
}

func mapping(map: Map) {
Expand Down
8 changes: 3 additions & 5 deletions ObjectMapperTests/CustomTransformTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,9 @@ class Transforms: Mappable {

var firstImageType: ImageType?
var secondImageType: ImageType?

init() {}

required init?(_ map: Map) {
mapping(map)

static func newInstance() -> Mappable {
return Transforms()
}

func mapping(map: Map) {
Expand Down
14 changes: 7 additions & 7 deletions ObjectMapperTests/NestedKeysTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ class NestedKeys: Mappable {
var object: Object?
var objectArray: [Object] = []
var objectDict: [String: Object] = [:]

required init?(_ map: Map) {
mapping(map)
static func newInstance() -> Mappable {
return NestedKeys()
}

func mapping(map: Map) {
Expand Down Expand Up @@ -178,11 +178,11 @@ class NestedKeys: Mappable {

class Object: Mappable, Equatable {
var value: Int = Int.min

required init?(_ map: Map) {
mapping(map)
static func newInstance() -> Mappable {
return Object()
}

func mapping(map: Map) {
value <- map["value"]
}
Expand Down
168 changes: 52 additions & 116 deletions ObjectMapperTests/ObjectMapperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@ class ObjectMapperTests: XCTestCase {
super.tearDown()
}

func testImmutableMappable() {
let mapper = Mapper<Immutable>()
let JSON = [ "prop1": "Immutable!", "prop2": 255, "prop3": true ]

let immutable: Immutable! = mapper.map(JSON)
expect(immutable).notTo(beNil())
expect(immutable?.prop1).to(equal("Immutable!"))
expect(immutable?.prop2).to(equal(255))
expect(immutable?.prop3).to(equal(true))
expect(immutable?.prop4).to(equal(DBL_MAX))

let JSON2 = [ "prop1": "prop1", "prop2": NSNull() ]
let immutable2 = mapper.map(JSON2)
expect(immutable2).to(beNil())

let JSONFromObject = mapper.toJSON(immutable)
expect(mapper.map(JSONFromObject)).to(equal(immutable))
}
// func testImmutableMappable() {
// let mapper = Mapper<Immutable>()
// let JSON = [ "prop1": "Immutable!", "prop2": 255, "prop3": true ]
//
// let immutable: Immutable! = mapper.map(JSON)
// expect(immutable).notTo(beNil())
// expect(immutable?.prop1).to(equal("Immutable!"))
// expect(immutable?.prop2).to(equal(255))
// expect(immutable?.prop3).to(equal(true))
// expect(immutable?.prop4).to(equal(DBL_MAX))
//
// let JSON2 = [ "prop1": "prop1", "prop2": NSNull() ]
// let immutable2 = mapper.map(JSON2)
// expect(immutable2).to(beNil())
//
// let JSONFromObject = mapper.toJSON(immutable)
// expect(mapper.map(JSONFromObject)).to(equal(immutable))
// }

func testBasicParsing() {
let username = "John Doe"
Expand Down Expand Up @@ -432,60 +432,13 @@ class ObjectMapperTests: XCTestCase {
}
}

struct Immutable: Equatable {
let prop1: String
let prop2: Int
let prop3: Bool
let prop4: Double
}

extension Immutable: Mappable {
init?(_ map: Map) {
prop1 = map["prop1"].valueOrFail()
prop2 = map["prop2"].valueOrFail()
prop3 = map["prop3"].valueOrFail()
prop4 = map["prop4"].valueOr(DBL_MAX)

if !map.isValid {
return nil
}
}

mutating func mapping(map: Map) {
switch map.mappingType {
case .FromJSON:
if let x = Immutable(map) {
self = x
}

case .ToJSON:
var prop1 = self.prop1
var prop2 = self.prop2
var prop3 = self.prop3
var prop4 = self.prop4

prop1 <- map["prop1"]
prop2 <- map["prop2"]
prop3 <- map["prop3"]
prop4 <- map["prop4"]
}
}
}

func ==(lhs: Immutable, rhs: Immutable) -> Bool {
return lhs.prop1 == rhs.prop1
&& lhs.prop2 == rhs.prop2
&& lhs.prop3 == rhs.prop3
&& lhs.prop4 == rhs.prop4
}

class Response<T: Mappable>: Mappable {
var result: T?

required init?(_ map: Map) {
mapping(map)
class func newInstance() -> Mappable {
return Response()
}

func mapping(map: Map) {
result <- map["result"]
}
Expand All @@ -494,8 +447,8 @@ class Response<T: Mappable>: Mappable {
class Status: Mappable {
var status: Int?

required init?(_ map: Map) {
mapping(map)
class func newInstance() -> Mappable {
return Status()
}

func mapping(map: Map) {
Expand All @@ -507,10 +460,11 @@ class Plan: Mappable {
var tasks: [Task]?
var dictionaryOfTasks: [String: [Task]]?

required init?(_ map: Map) {
mapping(map)
class func newInstance() -> Mappable {
return Plan()
}


func mapping(map: Map) {
tasks <- map["tasks"]
dictionaryOfTasks <- map["dictionaryOfTasks"]
Expand All @@ -520,11 +474,9 @@ class Plan: Mappable {
class Task: Mappable {
var taskId: Int?
var percentage: Double?

init() {}

required init?(_ map: Map) {
mapping(map)
class func newInstance() -> Mappable {
return Task()
}

func mapping(map: Map) {
Expand All @@ -537,10 +489,10 @@ class TaskDictionary: Mappable {
var test: String?
var tasks: [String : Task]?

required init?(_ map: Map) {
mapping(map)
class func newInstance() -> Mappable {
return TaskDictionary()
}

func mapping(map: Map) {
test <- map["test"]
tasks <- map["tasks"]
Expand All @@ -554,11 +506,9 @@ struct Student: Mappable {
var UUID: String?
var major: Int?
var minor: Int?

init() {}

init?(_ map: Map) {
mapping(map)
static func newInstance() -> Mappable {
return Student()
}

mutating func mapping(map: Map) {
Expand Down Expand Up @@ -593,12 +543,9 @@ class User: Mappable {
var friendDictionary: [String : User]?
var friend: User?
var friends: [User]? = []


init() {}

required init?(_ map: Map) {
mapping(map)
class func newInstance() -> Mappable {
return User()
}

func mapping(map: Map) {
Expand All @@ -625,13 +572,11 @@ class User: Mappable {
class Base: Mappable {

var base: String?

init() {}

required init?(_ map: Map) {
mapping(map)
class func newInstance() -> Mappable {
return Base()
}

func mapping(map: Map) {
base <- map["base"]
}
Expand All @@ -640,13 +585,9 @@ class Base: Mappable {
class Subclass: Base {

var sub: String?

override init() {
super.init()
}

required init?(_ map: Map) {
super.init(map)
override class func newInstance() -> Mappable {
return Subclass()
}

override func mapping(map: Map) {
Expand All @@ -661,12 +602,8 @@ class GenericSubclass<T>: Base {

var sub: String?

override init() {
super.init()
}

required init?(_ map: Map) {
super.init(map)
override class func newInstance() -> Mappable {
return GenericSubclass<T>()
}

override func mapping(map: Map) {
Expand All @@ -679,8 +616,8 @@ class GenericSubclass<T>: Base {
class WithGenericArray<T: Mappable>: Mappable {
var genericItems: [T]?

required init?(_ map: Map) {
mapping(map)
class func newInstance() -> Mappable {
return WithGenericArray<T>()
}

func mapping(map: Map) {
Expand All @@ -691,18 +628,17 @@ class WithGenericArray<T: Mappable>: Mappable {
class ConcreteItem: Mappable {
var value: String?

required init?(_ map: Map) {
mapping(map)
class func newInstance() -> Mappable {
return ConcreteItem()
}

func mapping(map: Map) {
value <- map["value"]
}
}

class SubclassWithGenericArrayInSuperclass<Unused>: WithGenericArray<ConcreteItem> {
required init?(_ map: Map) {
super.init(map)
override class func newInstance() -> Mappable {
return SubclassWithGenericArrayInSuperclass<Unused>()
}
}

Expand All @@ -715,8 +651,8 @@ enum ExampleEnum: Int {
class ExampleEnumArray: Mappable {
var enums: [ExampleEnum] = []

required init? (_ map: Map) {
mapping(map)
class func newInstance() -> Mappable {
return ExampleEnumArray()
}

func mapping(map: Map) {
Expand All @@ -727,8 +663,8 @@ class ExampleEnumArray: Mappable {
class ExampleEnumDictionary: Mappable {
var enums: [String: ExampleEnum] = [:]

required init? (_ map: Map) {
mapping(map)
class func newInstance() -> Mappable {
return ExampleEnumDictionary()
}

func mapping(map: Map) {
Expand Down

0 comments on commit 9d2a5a6

Please sign in to comment.