Skip to content

Commit 5fa298c

Browse files
Merge pull request tristanhimmelman#41 from ikesyo/make-mapper-generic
Make Mapper class itself generic, 🔥 `toType` parameter
2 parents b531c21 + 21f73a8 commit 5fa298c

File tree

7 files changed

+79
-77
lines changed

7 files changed

+79
-77
lines changed

ObjectMapper/Core/FromJSON.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ class FromJSON<CollectionType> {
7474

7575
func object<N: Mappable>(inout field: N, object: AnyObject?) {
7676
if let value = object as? [String : AnyObject] {
77-
field = Mapper().map(value, toType: N.self)
77+
field = Mapper().map(value)
7878
}
7979
}
8080

8181
func object<N: Mappable>(inout field: N?, object: AnyObject?) {
8282
if let value = object as? [String : AnyObject] {
83-
field = Mapper().map(value, toType: N.self)
83+
field = Mapper().map(value)
8484
}
8585
}
8686

@@ -104,14 +104,14 @@ class FromJSON<CollectionType> {
104104

105105
// parses a JSON array into an array of objects of type <N: Mappable>
106106
private func parseObjectArray<N: Mappable>(object: AnyObject?) -> Array<N>{
107-
let mapper = Mapper()
107+
let mapper = Mapper<N>()
108108

109109
var parsedObjects = Array<N>()
110110

111111
if let array = object as [AnyObject]? {
112112
for object in array {
113113
let objectJSON = object as [String : AnyObject]
114-
var parsedObj = mapper.map(objectJSON, toType: N.self)
114+
var parsedObj = mapper.map(objectJSON)
115115
parsedObjects.append(parsedObj)
116116
}
117117
}
@@ -141,14 +141,14 @@ class FromJSON<CollectionType> {
141141

142142
// parses a JSON Dictionary into an Dictionay of objects of type <N: Mappable>
143143
private func parseObjectDictionary<N: Mappable>(object: AnyObject?) -> Dictionary<String, N>{
144-
let mapper = Mapper()
144+
let mapper = Mapper<N>()
145145

146146
var parsedObjectsDictionary = Dictionary<String, N>()
147147

148148
if let dictionary = object as Dictionary<String, AnyObject>? {
149149
for (key, object) in dictionary {
150150
let objectJSON = object as [String : AnyObject]
151-
var parsedObj = mapper.map(objectJSON, toType: N.self)
151+
var parsedObj = mapper.map(objectJSON)
152152
parsedObjectsDictionary[key] = parsedObj
153153
}
154154
}

ObjectMapper/Core/Mapper.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import Foundation
1010

1111
public protocol Mappable {
12-
mutating func map(mapper: Mapper)
12+
mutating func map<N>(mapper: Mapper<N>)
1313
init()
1414
}
1515

@@ -18,7 +18,7 @@ enum MappingType {
1818
case toJSON
1919
}
2020

21-
public class Mapper {
21+
public class Mapper<N: Mappable> {
2222
var JSONDictionary: [String : AnyObject] = [:]
2323
var currentValue: AnyObject?
2424
var currentKey: String?
@@ -43,7 +43,7 @@ public class Mapper {
4343
}
4444

4545
// map a JSON string onto an existing object
46-
public func map<N: Mappable>(string JSON: String, var toObject object: N) -> N! {
46+
public func map(string JSON: String, var toObject object: N) -> N! {
4747
var json = parseJSONDictionary(JSON)
4848
if let json = json {
4949
mappingType = .fromJSON
@@ -56,47 +56,47 @@ public class Mapper {
5656
}
5757

5858
// map a JSON string to an object Type that conforms to Mappable
59-
public func map<N: Mappable>(string JSONString: String, toType type: N.Type) -> N! {
59+
public func map(string JSONString: String) -> N! {
6060
var json = parseJSONDictionary(JSONString)
6161
if let json = json {
62-
return map(json, toType: type)
62+
return map(json)
6363
}
6464
return nil
6565
}
6666

6767
// maps a JSON dictionary to an object that conforms to Mappable
68-
public func map<N: Mappable>(JSON: [String : AnyObject], toType type: N.Type) -> N! {
68+
public func map(JSON: [String : AnyObject]) -> N! {
6969
var object = N()
7070
return map(JSON, toObject: object)
7171
}
7272

7373
// maps a JSON dictionary to an existing object that conforms to Mappable.
7474
// Usefull for those pesky objects that have crappy designated initializers like NSManagedObject
75-
public func map<N: Mappable>(JSON: [String : AnyObject], var toObject object: N) -> N! {
75+
public func map(JSON: [String : AnyObject], var toObject object: N) -> N! {
7676
mappingType = .fromJSON
7777
self.JSONDictionary = JSON
7878
object.map(self)
7979
return object
8080
}
8181

8282
// maps a JSON array to an object that conforms to Mappable
83-
public func mapArray<N: Mappable>(string JSONString: String, toType type: N.Type) -> [N]! {
83+
public func mapArray(string JSONString: String) -> [N]! {
8484
var jsonArray = parseJSONArray(JSONString)
8585
if let jsonArray = jsonArray {
86-
return mapArray(jsonArray, toType: type)
86+
return mapArray(jsonArray)
8787
} else {
8888
// failed to parse JSON into array form
8989
// try to parse it into a dictionary and then wrap it in an array
9090
var jsonDict = parseJSONDictionary(JSONString)
9191
if let jsonDict = jsonDict {
92-
return mapArray([jsonDict], toType: type)
92+
return mapArray([jsonDict])
9393
}
9494
}
9595
return nil
9696
}
9797

9898
// maps a JSON dictionary to an object that conforms to Mappable
99-
public func mapArray<N: Mappable>(JSON: [[String : AnyObject]], toType type: N.Type) -> [N] {
99+
public func mapArray(JSON: [[String : AnyObject]]) -> [N] {
100100
mappingType = .fromJSON
101101

102102
var objects: Array<N> = []
@@ -113,7 +113,7 @@ public class Mapper {
113113
}
114114

115115
// maps an Object to a JSON dictionary <String : AnyObject>
116-
public func toJSON<N: Mappable>(var object: N) -> [String : AnyObject] {
116+
public func toJSON(var object: N) -> [String : AnyObject] {
117117
mappingType = .toJSON
118118

119119
self.JSONDictionary = [String : AnyObject]()
@@ -123,7 +123,7 @@ public class Mapper {
123123
}
124124

125125
// maps an array of Objects to an array of JSON dictionaries [[String : AnyObject]]
126-
public func toJSONArray<N: Mappable>(array: [N]) -> [[String : AnyObject]] {
126+
public func toJSONArray(array: [N]) -> [[String : AnyObject]] {
127127
mappingType = .toJSON
128128

129129
var JSONArray = [[String : AnyObject]]()
@@ -138,7 +138,7 @@ public class Mapper {
138138
}
139139

140140
// maps an Object to a JSON string
141-
public func toJSONString<N: Mappable>(object: N, prettyPrint: Bool) -> String! {
141+
public func toJSONString(object: N, prettyPrint: Bool) -> String! {
142142
let JSONDict = toJSON(object)
143143

144144
var err: NSError?

ObjectMapper/Core/Operators.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Foundation
1313
//infix operator <= {}
1414

1515
// MARK: basic type
16-
public func <=<T>(inout left: T, right: Mapper) {
16+
public func <=<T, U>(inout left: T, right: Mapper<U>) {
1717
if right.mappingType == MappingType.fromJSON {
1818
FromJSON<T>().baseType(&left, object: right.currentValue)
1919
} else {
@@ -22,7 +22,7 @@ public func <=<T>(inout left: T, right: Mapper) {
2222
}
2323

2424
// Optional basic type
25-
public func <=<T>(inout left: T?, right: Mapper) {
25+
public func <=<T, U>(inout left: T?, right: Mapper<U>) {
2626
if right.mappingType == MappingType.fromJSON {
2727
FromJSON<T>().optionalBaseType(&left, object: right.currentValue)
2828
} else {
@@ -31,7 +31,7 @@ public func <=<T>(inout left: T?, right: Mapper) {
3131
}
3232

3333
// Basic type with Transform
34-
public func <=<T, N>(inout left: T, right: (Mapper, MapperTransform<T, N>)) {
34+
public func <=<T, U, N>(inout left: T, right: (Mapper<U>, MapperTransform<T, N>)) {
3535
if right.0.mappingType == MappingType.fromJSON {
3636
var value: T? = right.1.transformFromJSON(right.0.currentValue)
3737
//println("FromJSON \(value)");
@@ -44,7 +44,7 @@ public func <=<T, N>(inout left: T, right: (Mapper, MapperTransform<T, N>)) {
4444
}
4545

4646
// Optional basic type with Transform
47-
public func <=<T, N>(inout left: T?, right: (Mapper, MapperTransform<T, N>)) {
47+
public func <=<T, U, N>(inout left: T?, right: (Mapper<U>, MapperTransform<T, N>)) {
4848
if right.0.mappingType == MappingType.fromJSON {
4949
var value: T? = right.1.transformFromJSON(right.0.currentValue)
5050
//println("FromJSON \(value)");
@@ -57,7 +57,7 @@ public func <=<T, N>(inout left: T?, right: (Mapper, MapperTransform<T, N>)) {
5757
}
5858

5959
// MARK:- T: Mappable
60-
public func <=<T: Mappable>(inout left: T, right: Mapper) {
60+
public func <=<T: Mappable, U>(inout left: T, right: Mapper<U>) {
6161
if right.mappingType == MappingType.fromJSON {
6262
FromJSON<T>().object(&left, object: right.currentValue)
6363
} else {
@@ -66,7 +66,7 @@ public func <=<T: Mappable>(inout left: T, right: Mapper) {
6666
}
6767

6868
// Optional object conforming to Mappable
69-
public func <=<T: Mappable>(inout left: T?, right: Mapper) {
69+
public func <=<T: Mappable, U>(inout left: T?, right: Mapper<U>) {
7070
if right.mappingType == MappingType.fromJSON {
7171
FromJSON<T>().object(&left, object: right.currentValue)
7272
} else {
@@ -75,7 +75,7 @@ public func <=<T: Mappable>(inout left: T?, right: Mapper) {
7575
}
7676

7777
// MARK:- Dictionary <String, T: Mappable>
78-
public func <=<T: Mappable>(inout left: Dictionary<String, T>, right: Mapper) {
78+
public func <=<T: Mappable, U>(inout left: Dictionary<String, T>, right: Mapper<U>) {
7979
if right.mappingType == MappingType.fromJSON {
8080
FromJSON<T>().objectDictionary(&left, object: right.currentValue)
8181
} else {
@@ -84,7 +84,7 @@ public func <=<T: Mappable>(inout left: Dictionary<String, T>, right: Mapper) {
8484
}
8585

8686
// Optional Dictionary <String, T: Mappable>
87-
public func <=<T: Mappable>(inout left: Dictionary<String, T>?, right: Mapper) {
87+
public func <=<T: Mappable, U>(inout left: Dictionary<String, T>?, right: Mapper<U>) {
8888
if right.mappingType == MappingType.fromJSON {
8989
FromJSON<T>().optionalObjectDictionary(&left, object: right.currentValue)
9090
} else {
@@ -93,7 +93,7 @@ public func <=<T: Mappable>(inout left: Dictionary<String, T>?, right: Mapper) {
9393
}
9494

9595
// MARK: Dictionary <String, AnyObject>
96-
public func <=(inout left: Dictionary<String, AnyObject>, right: Mapper) {
96+
public func <=<U>(inout left: Dictionary<String, AnyObject>, right: Mapper<U>) {
9797
if right.mappingType == MappingType.fromJSON {
9898
FromJSON<AnyObject>().baseType(&left, object: right.currentValue)
9999
} else {
@@ -102,7 +102,7 @@ public func <=(inout left: Dictionary<String, AnyObject>, right: Mapper) {
102102
}
103103

104104
// Optional dictionary <String, AnyObject>
105-
public func <=(inout left: Dictionary<String, AnyObject>?, right: Mapper) {
105+
public func <=<U>(inout left: Dictionary<String, AnyObject>?, right: Mapper<U>) {
106106
if right.mappingType == MappingType.fromJSON {
107107
FromJSON<AnyObject>().optionalBaseType(&left, object: right.currentValue)
108108
} else {
@@ -111,7 +111,7 @@ public func <=(inout left: Dictionary<String, AnyObject>?, right: Mapper) {
111111
}
112112

113113
// MARK:- Array<T: Mappable>
114-
public func <=<T: Mappable>(inout left: Array<T>, right: Mapper) {
114+
public func <=<T: Mappable, U>(inout left: Array<T>, right: Mapper<U>) {
115115
if right.mappingType == MappingType.fromJSON {
116116
FromJSON<T>().objectArray(&left, object: right.currentValue)
117117
} else {
@@ -120,7 +120,7 @@ public func <=<T: Mappable>(inout left: Array<T>, right: Mapper) {
120120
}
121121

122122
// Optional array of objects conforming to Mappable
123-
public func <=<T: Mappable>(inout left: Array<T>?, right: Mapper) {
123+
public func <=<T: Mappable, U>(inout left: Array<T>?, right: Mapper<U>) {
124124
if right.mappingType == MappingType.fromJSON {
125125
FromJSON<T>().optionalObjectArray(&left, object: right.currentValue)
126126
} else {
@@ -129,7 +129,7 @@ public func <=<T: Mappable>(inout left: Array<T>?, right: Mapper) {
129129
}
130130

131131
// MARK: Array<AnyObject>
132-
public func <=(inout left: Array<AnyObject>, right: Mapper) {
132+
public func <=<U>(inout left: Array<AnyObject>, right: Mapper<U>) {
133133
if right.mappingType == MappingType.fromJSON {
134134
FromJSON<AnyObject>().baseType(&left, object: right.currentValue)
135135
} else {
@@ -138,7 +138,7 @@ public func <=(inout left: Array<AnyObject>, right: Mapper) {
138138
}
139139

140140
// Optional array of String type
141-
public func <=(inout left: Array<AnyObject>?, right: Mapper) {
141+
public func <=<U>(inout left: Array<AnyObject>?, right: Mapper<U>) {
142142
if right.mappingType == MappingType.fromJSON {
143143
FromJSON<AnyObject>().optionalBaseType(&left, object: right.currentValue)
144144
} else {

ObjectMapper/Core/ToJSON.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class ToJSON {
8888
}
8989

9090
func object<N: Mappable>(field: N, key: String, inout dictionary: [String : AnyObject]) {
91-
let mapper = Mapper()
91+
let mapper = Mapper<N>()
9292

9393
dictionary[key] = NSDictionary(dictionary: mapper.toJSON(field))
9494
}
@@ -103,7 +103,7 @@ class ToJSON {
103103
var JSONObjects = NSMutableArray()
104104

105105
for object in field {
106-
let mapper = Mapper()
106+
let mapper = Mapper<N>()
107107
JSONObjects.addObject(mapper.toJSON(object))
108108
}
109109

@@ -122,7 +122,7 @@ class ToJSON {
122122
var JSONObjects = NSMutableDictionary()
123123

124124
for (k, object) in field {
125-
let mapper = Mapper()
125+
let mapper = Mapper<N>()
126126
JSONObjects.setObject(mapper.toJSON(object), forKey: k)
127127

128128
}

0 commit comments

Comments
 (0)