Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #980 from gcharita/master
Added support for Swift 4.2 and Xcode 10
  • Loading branch information
tristanhimmelman committed Jun 18, 2018
2 parents 5884088 + 5985794 commit 7de160d
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Sources/EnumOperators.swift
Expand Up @@ -31,10 +31,13 @@ public func >>> <T: RawRepresentable>(left: T?, right: Map) {
}


// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
/// Implicitly Unwrapped Optional Object of Raw Representable type
public func <- <T: RawRepresentable>(left: inout T!, right: Map) {
left <- (right, EnumTransform())
}
#endif

// MARK:- Arrays of Raw Representable type

Expand All @@ -58,10 +61,13 @@ public func >>> <T: RawRepresentable>(left: [T]?, right: Map) {
}


// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
/// Array of Raw Representable object
public func <- <T: RawRepresentable>(left: inout [T]!, right: Map) {
left <- (right, EnumTransform())
}
#endif

// MARK:- Dictionaries of Raw Representable type

Expand All @@ -85,7 +91,10 @@ public func >>> <T: RawRepresentable>(left: [String: T]?, right: Map) {
}


// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
/// Dictionary of Raw Representable object
public func <- <T: RawRepresentable>(left: inout [String: T]!, right: Map) {
left <- (right, EnumTransform())
}
#endif
23 changes: 22 additions & 1 deletion Sources/FromJSON.swift
Expand Up @@ -40,10 +40,13 @@ internal final class FromJSON {
field = object
}

// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
/// Implicitly unwrapped optional basic type
class func optionalBasicType<FieldType>(_ field: inout FieldType!, object: FieldType?) {
field = object
}
#endif

/// Mappable object
class func object<N: BaseMappable>(_ field: inout N, map: Map) {
Expand All @@ -64,6 +67,8 @@ internal final class FromJSON {
}
}

// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
/// Implicitly unwrapped Optional Mappable Object
class func optionalObject<N: BaseMappable>(_ field: inout N!, map: Map) {
if let f = field , map.toObject && map.currentValue != nil {
Expand All @@ -72,6 +77,7 @@ internal final class FromJSON {
field = Mapper(context: map.context).map(JSONObject: map.currentValue)
}
}
#endif

/// mappable object array
class func objectArray<N: BaseMappable>(_ field: inout Array<N>, map: Map) {
Expand All @@ -90,6 +96,8 @@ internal final class FromJSON {
}
}

// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
/// Implicitly unwrapped optional mappable object array
class func optionalObjectArray<N: BaseMappable>(_ field: inout Array<N>!, map: Map) {
if let objects: Array<N> = Mapper(context: map.context).mapArray(JSONObject: map.currentValue) {
Expand All @@ -98,6 +106,7 @@ internal final class FromJSON {
field = nil
}
}
#endif

/// mappable object array
class func twoDimensionalObjectArray<N: BaseMappable>(_ field: inout Array<Array<N>>, map: Map) {
Expand All @@ -111,10 +120,13 @@ internal final class FromJSON {
field = Mapper(context: map.context).mapArrayOfArrays(JSONObject: map.currentValue)
}

// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
/// Implicitly unwrapped optional 2 dimentional mappable object array
class func optionalTwoDimensionalObjectArray<N: BaseMappable>(_ field: inout Array<Array<N>>!, map: Map) {
field = Mapper(context: map.context).mapArrayOfArrays(JSONObject: map.currentValue)
}
#endif

/// Dctionary containing Mappable objects
class func objectDictionary<N: BaseMappable>(_ field: inout Dictionary<String, N>, map: Map) {
Expand All @@ -136,6 +148,8 @@ internal final class FromJSON {
}
}

// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
/// Implicitly unwrapped Dictionary containing Mappable objects
class func optionalObjectDictionary<N: BaseMappable>(_ field: inout Dictionary<String, N>!, map: Map) {
if let f = field , map.toObject && map.currentValue != nil {
Expand All @@ -144,6 +158,7 @@ internal final class FromJSON {
field = Mapper(context: map.context).mapDictionary(JSONObject: map.currentValue)
}
}
#endif

/// Dictionary containing Array of Mappable objects
class func objectDictionaryOfArrays<N: BaseMappable>(_ field: inout Dictionary<String, [N]>, map: Map) {
Expand All @@ -157,10 +172,13 @@ internal final class FromJSON {
field = Mapper<N>(context: map.context).mapDictionaryOfArrays(JSONObject: map.currentValue)
}

// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
/// Implicitly unwrapped Dictionary containing Array of Mappable objects
class func optionalObjectDictionaryOfArrays<N: BaseMappable>(_ field: inout Dictionary<String, [N]>!, map: Map) {
field = Mapper<N>(context: map.context).mapDictionaryOfArrays(JSONObject: map.currentValue)
}
#endif

/// mappable object Set
class func objectSet<N: BaseMappable>(_ field: inout Set<N>, map: Map) {
Expand All @@ -174,8 +192,11 @@ internal final class FromJSON {
field = Mapper(context: map.context).mapSet(JSONObject: map.currentValue)
}

// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
/// Implicitly unwrapped optional mappable object array
class func optionalObjectSet<N: BaseMappable>(_ field: inout Set<N>!, map: Map) {
field = Mapper(context: map.context).mapSet(JSONObject: map.currentValue)
}
}
#endif
}
4 changes: 4 additions & 0 deletions Sources/ImmutableMappable.swift
Expand Up @@ -214,7 +214,11 @@ public extension Mapper where N: ImmutableMappable {
// MARK: Array mapping functions

public func mapArray(JSONArray: [[String: Any]]) throws -> [N] {
#if swift(>=4.1)
return try JSONArray.compactMap(mapOrFail)
#else
return try JSONArray.flatMap(mapOrFail)
#endif
}

public func mapArray(JSONString: String) throws -> [N] {
Expand Down
6 changes: 6 additions & 0 deletions Sources/IntegerOperators.swift
Expand Up @@ -34,6 +34,8 @@ public func <- <T: SignedInteger>(left: inout T?, right: Map) {
}
}

// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
/// ImplicitlyUnwrappedOptional SignedInteger mapping
public func <- <T: SignedInteger>(left: inout T!, right: Map) {
switch right.mappingType {
Expand All @@ -45,6 +47,7 @@ public func <- <T: SignedInteger>(left: inout T!, right: Map) {
default: ()
}
}
#endif


// MARK: - Unsigned Integer
Expand Down Expand Up @@ -74,6 +77,8 @@ public func <- <T: UnsignedInteger>(left: inout T?, right: Map) {
}
}

// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
/// ImplicitlyUnwrappedOptional UnsignedInteger mapping
public func <- <T: UnsignedInteger>(left: inout T!, right: Map) {
switch right.mappingType {
Expand All @@ -85,6 +90,7 @@ public func <- <T: UnsignedInteger>(left: inout T!, right: Map) {
default: ()
}
}
#endif

// MARK: - Casting Utils

Expand Down
4 changes: 4 additions & 0 deletions Sources/Map.swift
Expand Up @@ -131,7 +131,11 @@ public final class Map {
}
} else if value == nil && T.self == [Float].self {
if let v = currentValue as? [Double] {
#if swift(>=4.1)
return v.compactMap{ Float($0) } as? T
#else
return v.flatMap{ Float($0) } as? T
#endif
}
} else if value == nil && T.self == [String:Float].self {
if let v = currentValue as? [String:Double] {
Expand Down
8 changes: 8 additions & 0 deletions Sources/Mapper.swift
Expand Up @@ -159,7 +159,11 @@ public final class Mapper<N: BaseMappable> {
/// Maps an array of JSON dictionary to an array of Mappable objects
public func mapArray(JSONArray: [[String: Any]]) -> [N] {
// map every element in JSON array to type N
#if swift(>=4.1)
let result = JSONArray.compactMap(map)
#else
let result = JSONArray.flatMap(map)
#endif
return result
}

Expand Down Expand Up @@ -425,7 +429,11 @@ extension Mapper where N: Hashable {
/// Maps an Set of JSON dictionary to an array of Mappable objects
public func mapSet(JSONArray: [[String: Any]]) -> Set<N> {
// map every element in JSON array to type N
#if swift(>=4.1)
return Set(JSONArray.compactMap(map))
#else
return Set(JSONArray.flatMap(map))
#endif
}

///Maps a Set of Objects to a Set of JSON dictionaries [[String : Any]]
Expand Down
21 changes: 21 additions & 0 deletions Sources/Operators.swift
Expand Up @@ -76,6 +76,8 @@ public func >>> <T>(left: T?, right: Map) {
}


// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
/// Implicitly unwrapped optional object of basic type
public func <- <T>(left: inout T!, right: Map) {
switch right.mappingType {
Expand All @@ -86,6 +88,7 @@ public func <- <T>(left: inout T!, right: Map) {
default: ()
}
}
#endif

// MARK:- Mappable Objects - <T: BaseMappable>

Expand Down Expand Up @@ -124,6 +127,8 @@ public func >>> <T: BaseMappable>(left: T?, right: Map) {
}


// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
/// Implicitly unwrapped optional Mappable objects
public func <- <T: BaseMappable>(left: inout T!, right: Map) {
switch right.mappingType {
Expand All @@ -134,6 +139,7 @@ public func <- <T: BaseMappable>(left: inout T!, right: Map) {
default: ()
}
}
#endif

// MARK:- Dictionary of Mappable objects - Dictionary<String, T: BaseMappable>

Expand Down Expand Up @@ -173,6 +179,8 @@ public func >>> <T: BaseMappable>(left: Dictionary<String, T>?, right: Map) {
}


// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
/// Implicitly unwrapped Optional Dictionary of Mappable object <String, T: Mappable>
public func <- <T: BaseMappable>(left: inout Dictionary<String, T>!, right: Map) {
switch right.mappingType {
Expand All @@ -183,6 +191,7 @@ public func <- <T: BaseMappable>(left: inout Dictionary<String, T>!, right: Map)
default: ()
}
}
#endif

/// Dictionary of Mappable objects <String, T: Mappable>
public func <- <T: BaseMappable>(left: inout Dictionary<String, [T]>, right: Map) {
Expand Down Expand Up @@ -219,6 +228,8 @@ public func >>> <T: BaseMappable>(left: Dictionary<String, [T]>?, right: Map) {
}


// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
/// Implicitly unwrapped Optional Dictionary of Mappable object <String, T: Mappable>
public func <- <T: BaseMappable>(left: inout Dictionary<String, [T]>!, right: Map) {
switch right.mappingType {
Expand All @@ -229,6 +240,7 @@ public func <- <T: BaseMappable>(left: inout Dictionary<String, [T]>!, right: Ma
default: ()
}
}
#endif

// MARK:- Array of Mappable objects - Array<T: BaseMappable>

Expand Down Expand Up @@ -267,6 +279,8 @@ public func >>> <T: BaseMappable>(left: Array<T>?, right: Map) {
}


// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
/// Implicitly unwrapped Optional array of Mappable objects
public func <- <T: BaseMappable>(left: inout Array<T>!, right: Map) {
switch right.mappingType {
Expand All @@ -277,6 +291,7 @@ public func <- <T: BaseMappable>(left: inout Array<T>!, right: Map) {
default: ()
}
}
#endif

// MARK:- Array of Array of Mappable objects - Array<Array<T: BaseMappable>>

Expand Down Expand Up @@ -316,6 +331,8 @@ public func >>> <T: BaseMappable>(left: Array<Array<T>>?, right: Map) {
}


// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
/// Implicitly unwrapped Optional array of Mappable objects
public func <- <T: BaseMappable>(left: inout Array<Array<T>>!, right: Map) {
switch right.mappingType {
Expand All @@ -326,6 +343,7 @@ public func <- <T: BaseMappable>(left: inout Array<Array<T>>!, right: Map) {
default: ()
}
}
#endif

// MARK:- Set of Mappable objects - Set<T: BaseMappable>

Expand Down Expand Up @@ -365,6 +383,8 @@ public func >>> <T: BaseMappable>(left: Set<T>?, right: Map) {
}


// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
/// Implicitly unwrapped Optional Set of Mappable objects
public func <- <T: BaseMappable>(left: inout Set<T>!, right: Map) {
switch right.mappingType {
Expand All @@ -375,3 +395,4 @@ public func <- <T: BaseMappable>(left: inout Set<T>!, right: Map) {
default: ()
}
}
#endif

0 comments on commit 7de160d

Please sign in to comment.