Skip to content

Commit

Permalink
Merge pull request #567 from Hearst-DD/andrebraga-master
Browse files Browse the repository at this point in the history
Updated protocols so that StaticMappable can be used in an extension
  • Loading branch information
tristanhimmelman committed Sep 7, 2016
2 parents e1cd7c2 + ffe25d1 commit 4c9d0c2
Show file tree
Hide file tree
Showing 15 changed files with 200 additions and 194 deletions.
36 changes: 18 additions & 18 deletions ObjectMapper/Core/FromJSON.swift
Expand Up @@ -46,7 +46,7 @@ internal final class FromJSON {
}

/// Mappable object
class func object<N: Mappable>(inout field: N, map: Map) {
class func object<N: BaseMappable>(inout field: N, map: Map) {
if map.toObject {
Mapper(context: map.context).map(map.currentValue, toObject: field)
} else if let value: N = Mapper(context: map.context).map(map.currentValue) {
Expand All @@ -55,7 +55,7 @@ internal final class FromJSON {
}

/// Optional Mappable Object
class func optionalObject<N: Mappable>(inout field: N?, map: Map) {
class func optionalObject<N: BaseMappable>(inout field: N?, map: Map) {
if let field = field where map.toObject && map.currentValue != nil {
Mapper(context: map.context).map(map.currentValue, toObject: field)
} else {
Expand All @@ -64,7 +64,7 @@ internal final class FromJSON {
}

/// Implicitly unwrapped Optional Mappable Object
class func optionalObject<N: Mappable>(inout field: N!, map: Map) {
class func optionalObject<N: BaseMappable>(inout field: N!, map: Map) {
if let field = field where map.toObject && map.currentValue != nil {
Mapper(context: map.context).map(map.currentValue, toObject: field)
} else {
Expand All @@ -73,14 +73,14 @@ internal final class FromJSON {
}

/// mappable object array
class func objectArray<N: Mappable>(inout field: Array<N>, map: Map) {
class func objectArray<N: BaseMappable>(inout field: Array<N>, map: Map) {
if let objects = Mapper<N>(context: map.context).mapArray(map.currentValue) {
field = objects
}
}

/// optional mappable object array
class func optionalObjectArray<N: Mappable>(inout field: Array<N>?, map: Map) {
class func optionalObjectArray<N: BaseMappable>(inout field: Array<N>?, map: Map) {
if let objects: Array<N> = Mapper(context: map.context).mapArray(map.currentValue) {
field = objects
} else {
Expand All @@ -89,7 +89,7 @@ internal final class FromJSON {
}

/// Implicitly unwrapped optional mappable object array
class func optionalObjectArray<N: Mappable>(inout field: Array<N>!, map: Map) {
class func optionalObjectArray<N: BaseMappable>(inout field: Array<N>!, map: Map) {
if let objects: Array<N> = Mapper(context: map.context).mapArray(map.currentValue) {
field = objects
} else {
Expand All @@ -98,24 +98,24 @@ internal final class FromJSON {
}

/// mappable object array
class func twoDimensionalObjectArray<N: Mappable>(inout field: Array<Array<N>>, map: Map) {
class func twoDimensionalObjectArray<N: BaseMappable>(inout field: Array<Array<N>>, map: Map) {
if let objects = Mapper<N>(context: map.context).mapArrayOfArrays(map.currentValue) {
field = objects
}
}

/// optional mappable 2 dimentional object array
class func optionalTwoDimensionalObjectArray<N: Mappable>(inout field: Array<Array<N>>?, map: Map) {
class func optionalTwoDimensionalObjectArray<N: BaseMappable>(inout field: Array<Array<N>>?, map: Map) {
field = Mapper(context: map.context).mapArrayOfArrays(map.currentValue)
}

/// Implicitly unwrapped optional 2 dimentional mappable object array
class func optionalTwoDimensionalObjectArray<N: Mappable>(inout field: Array<Array<N>>!, map: Map) {
class func optionalTwoDimensionalObjectArray<N: BaseMappable>(inout field: Array<Array<N>>!, map: Map) {
field = Mapper(context: map.context).mapArrayOfArrays(map.currentValue)
}

/// Dctionary containing Mappable objects
class func objectDictionary<N: Mappable>(inout field: Dictionary<String, N>, map: Map) {
class func objectDictionary<N: BaseMappable>(inout field: Dictionary<String, N>, map: Map) {
if map.toObject {
Mapper<N>(context: map.context).mapDictionary(map.currentValue, toDictionary: field)
} else {
Expand All @@ -126,7 +126,7 @@ internal final class FromJSON {
}

/// Optional dictionary containing Mappable objects
class func optionalObjectDictionary<N: Mappable>(inout field: Dictionary<String, N>?, map: Map) {
class func optionalObjectDictionary<N: BaseMappable>(inout field: Dictionary<String, N>?, map: Map) {
if let field = field where map.toObject && map.currentValue != nil {
Mapper(context: map.context).mapDictionary(map.currentValue, toDictionary: field)
} else {
Expand All @@ -135,7 +135,7 @@ internal final class FromJSON {
}

/// Implicitly unwrapped Dictionary containing Mappable objects
class func optionalObjectDictionary<N: Mappable>(inout field: Dictionary<String, N>!, map: Map) {
class func optionalObjectDictionary<N: BaseMappable>(inout field: Dictionary<String, N>!, map: Map) {
if let field = field where map.toObject && map.currentValue != nil {
Mapper(context: map.context).mapDictionary(map.currentValue, toDictionary: field)
} else {
Expand All @@ -144,36 +144,36 @@ internal final class FromJSON {
}

/// Dictionary containing Array of Mappable objects
class func objectDictionaryOfArrays<N: Mappable>(inout field: Dictionary<String, [N]>, map: Map) {
class func objectDictionaryOfArrays<N: BaseMappable>(inout field: Dictionary<String, [N]>, map: Map) {
if let objects = Mapper<N>(context: map.context).mapDictionaryOfArrays(map.currentValue) {
field = objects
}
}

/// Optional Dictionary containing Array of Mappable objects
class func optionalObjectDictionaryOfArrays<N: Mappable>(inout field: Dictionary<String, [N]>?, map: Map) {
class func optionalObjectDictionaryOfArrays<N: BaseMappable>(inout field: Dictionary<String, [N]>?, map: Map) {
field = Mapper<N>(context: map.context).mapDictionaryOfArrays(map.currentValue)
}

/// Implicitly unwrapped Dictionary containing Array of Mappable objects
class func optionalObjectDictionaryOfArrays<N: Mappable>(inout field: Dictionary<String, [N]>!, map: Map) {
class func optionalObjectDictionaryOfArrays<N: BaseMappable>(inout field: Dictionary<String, [N]>!, map: Map) {
field = Mapper<N>(context: map.context).mapDictionaryOfArrays(map.currentValue)
}

/// mappable object Set
class func objectSet<N: Mappable>(inout field: Set<N>, map: Map) {
class func objectSet<N: BaseMappable>(inout field: Set<N>, map: Map) {
if let objects = Mapper<N>(context: map.context).mapSet(map.currentValue) {
field = objects
}
}

/// optional mappable object array
class func optionalObjectSet<N: Mappable>(inout field: Set<N>?, map: Map) {
class func optionalObjectSet<N: BaseMappable>(inout field: Set<N>?, map: Map) {
field = Mapper(context: map.context).mapSet(map.currentValue)
}

/// Implicitly unwrapped optional mappable object array
class func optionalObjectSet<N: Mappable>(inout field: Set<N>!, map: Map) {
class func optionalObjectSet<N: BaseMappable>(inout field: Set<N>!, map: Map) {
field = Mapper(context: map.context).mapSet(map.currentValue)
}

Expand Down
8 changes: 4 additions & 4 deletions ObjectMapper/Core/Map.swift
Expand Up @@ -38,7 +38,7 @@ public protocol MapContext {
public final class Map {
public let mappingType: MappingType

public internal(set) var JSONDictionary: [String : AnyObject] = [:]
public internal(set) var JSONDictionary: [String: AnyObject] = [:]
public internal(set) var isKeyPresent = false
public var currentValue: AnyObject?
public var context: MapContext?
Expand All @@ -50,7 +50,7 @@ public final class Map {
/// Counter for failing cases of deserializing values to `let` properties.
private var failedCount: Int = 0

public init(mappingType: MappingType, JSONDictionary: [String : AnyObject], toObject: Bool = false, context: MapContext? = nil) {
public init(mappingType: MappingType, JSONDictionary: [String: AnyObject], toObject: Bool = false, context: MapContext? = nil) {
self.mappingType = mappingType
self.JSONDictionary = JSONDictionary
self.toObject = toObject
Expand Down Expand Up @@ -142,7 +142,7 @@ private func valueFor(keyPathComponents: ArraySlice<String>, dictionary: [String
let object = dictionary[keyPath]
if object is NSNull {
return (true, nil)
} else if let dict = object as? [String : AnyObject] where keyPathComponents.count > 1 {
} else if let dict = object as? [String: AnyObject] where keyPathComponents.count > 1 {
let tail = keyPathComponents.dropFirst()
return valueFor(tail, dictionary: dict)
} else if let array = object as? [AnyObject] where keyPathComponents.count > 1 {
Expand Down Expand Up @@ -175,7 +175,7 @@ private func valueFor(keyPathComponents: ArraySlice<String>, array: [AnyObject])
} else if let array = object as? [AnyObject] where keyPathComponents.count > 1 {
let tail = keyPathComponents.dropFirst()
return valueFor(tail, array: array)
} else if let dict = object as? [String : AnyObject] where keyPathComponents.count > 1 {
} else if let dict = object as? [String: AnyObject] where keyPathComponents.count > 1 {
let tail = keyPathComponents.dropFirst()
return valueFor(tail, dictionary: dict)
} else {
Expand Down
30 changes: 17 additions & 13 deletions ObjectMapper/Core/Mappable.swift
Expand Up @@ -8,21 +8,25 @@

import Foundation

public protocol Mappable {
/// This function can be used to validate JSON prior to mapping. Return nil to cancel mapping at this point
init?(_ map: Map)
/// BaseMappable should not be implemented directly. Mappable or StaticMappable should be used instead
public protocol BaseMappable {
/// This function is where all variable mappings should occur. It is executed by Mapper during the mapping (serialization and deserialization) process.
mutating func mapping(map: Map)
}

public protocol StaticMappable: Mappable {
public protocol Mappable: BaseMappable {
/// This function can be used to validate JSON prior to mapping. Return nil to cancel mapping at this point
init?(_ map: Map)
}

public protocol StaticMappable: BaseMappable {
/// This is function that can be used to:
/// 1) provide an existing cached object to be used for mapping
/// 2) return an object of another class (which conforms to Mappable) to be used for mapping. For instance, you may inspect the JSON to infer the type of object that should be used for any given mapping
static func objectForMapping(map: Map) -> Mappable?
static func objectForMapping(map: Map) -> BaseMappable?
}

public extension Mappable {
public extension BaseMappable {

/// Initializes object from a JSON String
public init?(JSONString: String) {
Expand All @@ -34,7 +38,7 @@ public extension Mappable {
}

/// Initializes object from a JSON Dictionary
public init?(JSON: [String : AnyObject]) {
public init?(JSON: [String: AnyObject]) {
if let obj: Self = Mapper().map(JSON) {
self = obj
} else {
Expand All @@ -53,7 +57,7 @@ public extension Mappable {
}
}

public extension Array where Element: Mappable {
public extension Array where Element: BaseMappable {

/// Initialize Array from a JSON String
public init?(JSONString: String) {
Expand All @@ -65,7 +69,7 @@ public extension Array where Element: Mappable {
}

/// Initialize Array from a JSON Array
public init?(JSONArray: [[String : AnyObject]]) {
public init?(JSONArray: [[String: AnyObject]]) {
if let obj: [Element] = Mapper().mapArray(JSONArray) {
self = obj
} else {
Expand All @@ -74,7 +78,7 @@ public extension Array where Element: Mappable {
}

/// Returns the JSON Array
public func toJSON() -> [[String : AnyObject]] {
public func toJSON() -> [[String: AnyObject]] {
return Mapper().toJSONArray(self)
}

Expand All @@ -84,7 +88,7 @@ public extension Array where Element: Mappable {
}
}

public extension Set where Element: Mappable {
public extension Set where Element: BaseMappable {

/// Initializes a set from a JSON String
public init?(JSONString: String) {
Expand All @@ -96,7 +100,7 @@ public extension Set where Element: Mappable {
}

/// Initializes a set from JSON
public init?(JSONArray: [[String : AnyObject]]) {
public init?(JSONArray: [[String: AnyObject]]) {
if let obj: Set<Element> = Mapper().mapSet(JSONArray) {
self = obj
} else {
Expand All @@ -105,7 +109,7 @@ public extension Set where Element: Mappable {
}

/// Returns the JSON Set
public func toJSON() -> [[String : AnyObject]] {
public func toJSON() -> [[String: AnyObject]] {
return Mapper().toJSONSet(self)
}

Expand Down

0 comments on commit 4c9d0c2

Please sign in to comment.