Skip to content

Commit

Permalink
Merge pull request #41 from ikesyo/make-mapper-generic
Browse files Browse the repository at this point in the history
Make Mapper class itself generic, 🔥 `toType` parameter
  • Loading branch information
tristanhimmelman committed Jan 26, 2015
2 parents b531c21 + 21f73a8 commit 5fa298c
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 77 deletions.
12 changes: 6 additions & 6 deletions ObjectMapper/Core/FromJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ class FromJSON<CollectionType> {

func object<N: Mappable>(inout field: N, object: AnyObject?) {
if let value = object as? [String : AnyObject] {
field = Mapper().map(value, toType: N.self)
field = Mapper().map(value)
}
}

func object<N: Mappable>(inout field: N?, object: AnyObject?) {
if let value = object as? [String : AnyObject] {
field = Mapper().map(value, toType: N.self)
field = Mapper().map(value)
}
}

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

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

var parsedObjects = Array<N>()

if let array = object as [AnyObject]? {
for object in array {
let objectJSON = object as [String : AnyObject]
var parsedObj = mapper.map(objectJSON, toType: N.self)
var parsedObj = mapper.map(objectJSON)
parsedObjects.append(parsedObj)
}
}
Expand Down Expand Up @@ -141,14 +141,14 @@ class FromJSON<CollectionType> {

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

var parsedObjectsDictionary = Dictionary<String, N>()

if let dictionary = object as Dictionary<String, AnyObject>? {
for (key, object) in dictionary {
let objectJSON = object as [String : AnyObject]
var parsedObj = mapper.map(objectJSON, toType: N.self)
var parsedObj = mapper.map(objectJSON)
parsedObjectsDictionary[key] = parsedObj
}
}
Expand Down
28 changes: 14 additions & 14 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 {
mutating func map(mapper: Mapper)
mutating func map<N>(mapper: Mapper<N>)
init()
}

Expand All @@ -18,7 +18,7 @@ enum MappingType {
case toJSON
}

public class Mapper {
public class Mapper<N: Mappable> {
var JSONDictionary: [String : AnyObject] = [:]
var currentValue: AnyObject?
var currentKey: String?
Expand All @@ -43,7 +43,7 @@ public class Mapper {
}

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

// map a JSON string to an object Type that conforms to Mappable
public func map<N: Mappable>(string JSONString: String, toType type: N.Type) -> N! {
public func map(string JSONString: String) -> N! {
var json = parseJSONDictionary(JSONString)
if let json = json {
return map(json, toType: type)
return map(json)
}
return nil
}

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

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

// maps a JSON array to an object that conforms to Mappable
public func mapArray<N: Mappable>(string JSONString: String, toType type: N.Type) -> [N]! {
public func mapArray(string JSONString: String) -> [N]! {
var jsonArray = parseJSONArray(JSONString)
if let jsonArray = jsonArray {
return mapArray(jsonArray, toType: type)
return mapArray(jsonArray)
} else {
// failed to parse JSON into array form
// try to parse it into a dictionary and then wrap it in an array
var jsonDict = parseJSONDictionary(JSONString)
if let jsonDict = jsonDict {
return mapArray([jsonDict], toType: type)
return mapArray([jsonDict])
}
}
return nil
}

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

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

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

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

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

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

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

var err: NSError?
Expand Down
28 changes: 14 additions & 14 deletions ObjectMapper/Core/Operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Foundation
//infix operator <= {}

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

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

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

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

// MARK:- T: Mappable
public func <=<T: Mappable>(inout left: T, right: Mapper) {
public func <=<T: Mappable, U>(inout left: T, right: Mapper<U>) {
if right.mappingType == MappingType.fromJSON {
FromJSON<T>().object(&left, object: right.currentValue)
} else {
Expand All @@ -66,7 +66,7 @@ public func <=<T: Mappable>(inout left: T, right: Mapper) {
}

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

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

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

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

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

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

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

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

// Optional array of String type
public func <=(inout left: Array<AnyObject>?, right: Mapper) {
public func <=<U>(inout left: Array<AnyObject>?, right: Mapper<U>) {
if right.mappingType == MappingType.fromJSON {
FromJSON<AnyObject>().optionalBaseType(&left, object: right.currentValue)
} else {
Expand Down
6 changes: 3 additions & 3 deletions ObjectMapper/Core/ToJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class ToJSON {
}

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

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

for object in field {
let mapper = Mapper()
let mapper = Mapper<N>()
JSONObjects.addObject(mapper.toJSON(object))
}

Expand All @@ -122,7 +122,7 @@ class ToJSON {
var JSONObjects = NSMutableDictionary()

for (k, object) in field {
let mapper = Mapper()
let mapper = Mapper<N>()
JSONObjects.setObject(mapper.toJSON(object), forKey: k)

}
Expand Down
Loading

0 comments on commit 5fa298c

Please sign in to comment.