diff --git a/Sources/EnumOperators.swift b/Sources/EnumOperators.swift index 5a1a667a..e4b3a38d 100644 --- a/Sources/EnumOperators.swift +++ b/Sources/EnumOperators.swift @@ -31,10 +31,13 @@ public func >>> (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 <- (left: inout T!, right: Map) { left <- (right, EnumTransform()) } +#endif // MARK:- Arrays of Raw Representable type @@ -58,10 +61,13 @@ public func >>> (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 <- (left: inout [T]!, right: Map) { left <- (right, EnumTransform()) } +#endif // MARK:- Dictionaries of Raw Representable type @@ -85,7 +91,10 @@ public func >>> (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 <- (left: inout [String: T]!, right: Map) { left <- (right, EnumTransform()) } +#endif diff --git a/Sources/FromJSON.swift b/Sources/FromJSON.swift index 952b42b4..c5a28983 100755 --- a/Sources/FromJSON.swift +++ b/Sources/FromJSON.swift @@ -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(_ field: inout FieldType!, object: FieldType?) { field = object } + #endif /// Mappable object class func object(_ field: inout N, map: Map) { @@ -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(_ field: inout N!, map: Map) { if let f = field , map.toObject && map.currentValue != nil { @@ -72,6 +77,7 @@ internal final class FromJSON { field = Mapper(context: map.context).map(JSONObject: map.currentValue) } } + #endif /// mappable object array class func objectArray(_ field: inout Array, map: Map) { @@ -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(_ field: inout Array!, map: Map) { if let objects: Array = Mapper(context: map.context).mapArray(JSONObject: map.currentValue) { @@ -98,6 +106,7 @@ internal final class FromJSON { field = nil } } + #endif /// mappable object array class func twoDimensionalObjectArray(_ field: inout Array>, map: Map) { @@ -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(_ field: inout Array>!, map: Map) { field = Mapper(context: map.context).mapArrayOfArrays(JSONObject: map.currentValue) } + #endif /// Dctionary containing Mappable objects class func objectDictionary(_ field: inout Dictionary, map: Map) { @@ -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(_ field: inout Dictionary!, map: Map) { if let f = field , map.toObject && map.currentValue != nil { @@ -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(_ field: inout Dictionary, map: Map) { @@ -157,10 +172,13 @@ internal final class FromJSON { field = Mapper(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(_ field: inout Dictionary!, map: Map) { field = Mapper(context: map.context).mapDictionaryOfArrays(JSONObject: map.currentValue) } + #endif /// mappable object Set class func objectSet(_ field: inout Set, map: Map) { @@ -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(_ field: inout Set!, map: Map) { field = Mapper(context: map.context).mapSet(JSONObject: map.currentValue) - } + } + #endif } diff --git a/Sources/ImmutableMappable.swift b/Sources/ImmutableMappable.swift index 589ea107..27b6315c 100644 --- a/Sources/ImmutableMappable.swift +++ b/Sources/ImmutableMappable.swift @@ -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] { diff --git a/Sources/IntegerOperators.swift b/Sources/IntegerOperators.swift index a4191e60..cd80334b 100644 --- a/Sources/IntegerOperators.swift +++ b/Sources/IntegerOperators.swift @@ -34,6 +34,8 @@ public func <- (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 <- (left: inout T!, right: Map) { switch right.mappingType { @@ -45,6 +47,7 @@ public func <- (left: inout T!, right: Map) { default: () } } +#endif // MARK: - Unsigned Integer @@ -74,6 +77,8 @@ public func <- (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 <- (left: inout T!, right: Map) { switch right.mappingType { @@ -85,6 +90,7 @@ public func <- (left: inout T!, right: Map) { default: () } } +#endif // MARK: - Casting Utils diff --git a/Sources/Map.swift b/Sources/Map.swift index d1ea3462..cec8925a 100644 --- a/Sources/Map.swift +++ b/Sources/Map.swift @@ -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] { diff --git a/Sources/Mapper.swift b/Sources/Mapper.swift index 134d9f4e..ffbae4f4 100755 --- a/Sources/Mapper.swift +++ b/Sources/Mapper.swift @@ -159,7 +159,11 @@ public final class Mapper { /// 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 } @@ -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 { // 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]] diff --git a/Sources/Operators.swift b/Sources/Operators.swift index eb497350..30c2f120 100755 --- a/Sources/Operators.swift +++ b/Sources/Operators.swift @@ -76,6 +76,8 @@ public func >>> (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 <- (left: inout T!, right: Map) { switch right.mappingType { @@ -86,6 +88,7 @@ public func <- (left: inout T!, right: Map) { default: () } } +#endif // MARK:- Mappable Objects - @@ -124,6 +127,8 @@ public func >>> (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 <- (left: inout T!, right: Map) { switch right.mappingType { @@ -134,6 +139,7 @@ public func <- (left: inout T!, right: Map) { default: () } } +#endif // MARK:- Dictionary of Mappable objects - Dictionary @@ -173,6 +179,8 @@ public func >>> (left: Dictionary?, 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 public func <- (left: inout Dictionary!, right: Map) { switch right.mappingType { @@ -183,6 +191,7 @@ public func <- (left: inout Dictionary!, right: Map) default: () } } +#endif /// Dictionary of Mappable objects public func <- (left: inout Dictionary, right: Map) { @@ -219,6 +228,8 @@ public func >>> (left: Dictionary?, 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 public func <- (left: inout Dictionary!, right: Map) { switch right.mappingType { @@ -229,6 +240,7 @@ public func <- (left: inout Dictionary!, right: Ma default: () } } +#endif // MARK:- Array of Mappable objects - Array @@ -267,6 +279,8 @@ public func >>> (left: Array?, 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 <- (left: inout Array!, right: Map) { switch right.mappingType { @@ -277,6 +291,7 @@ public func <- (left: inout Array!, right: Map) { default: () } } +#endif // MARK:- Array of Array of Mappable objects - Array> @@ -316,6 +331,8 @@ public func >>> (left: Array>?, 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 <- (left: inout Array>!, right: Map) { switch right.mappingType { @@ -326,6 +343,7 @@ public func <- (left: inout Array>!, right: Map) { default: () } } +#endif // MARK:- Set of Mappable objects - Set @@ -365,6 +383,8 @@ public func >>> (left: Set?, 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 <- (left: inout Set!, right: Map) { switch right.mappingType { @@ -375,3 +395,4 @@ public func <- (left: inout Set!, right: Map) { default: () } } +#endif diff --git a/Sources/TransformOperators.swift b/Sources/TransformOperators.swift index a0208b56..bd139518 100644 --- a/Sources/TransformOperators.swift +++ b/Sources/TransformOperators.swift @@ -54,6 +54,8 @@ public func >>> (left: Transform.Object?, 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 with Transform public func <- (left: inout Transform.Object!, right: (Map, Transform)) { let (map, transform) = right @@ -66,6 +68,7 @@ public func <- (left: inout Transform.Object!, right: default: () } } +#endif /// Array of Basic type with Transform public func <- (left: inout [Transform.Object], right: (Map, Transform)) { @@ -111,6 +114,8 @@ public func >>> (left: [Transform.Object]?, right: (Ma } +// 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 Basic type with Transform public func <- (left: inout [Transform.Object]!, right: (Map, Transform)) { let (map, transform) = right @@ -123,6 +128,7 @@ public func <- (left: inout [Transform.Object]!, right default: () } } +#endif /// Dictionary of Basic type with Transform public func <- (left: inout [String: Transform.Object], right: (Map, Transform)) { @@ -168,6 +174,8 @@ public func >>> (left: [String: Transform.Object]?, ri } +// 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 Basic type with Transform public func <- (left: inout [String: Transform.Object]!, right: (Map, Transform)) { let (map, transform) = right @@ -180,6 +188,7 @@ public func <- (left: inout [String: Transform.Object] default: () } } +#endif // MARK:- Transforms of Mappable Objects - @@ -227,6 +236,8 @@ public func >>> (left: Transform.Object?, 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 that have transforms public func <- (left: inout Transform.Object!, right: (Map, Transform)) where Transform.Object: BaseMappable { let (map, transform) = right @@ -239,6 +250,7 @@ public func <- (left: inout Transform.Object!, right: default: () } } +#endif // MARK:- Dictionary of Mappable objects with a transform - Dictionary @@ -284,6 +296,8 @@ public func >>> (left: Dictionary=4.1.50) || (swift(>=3.4) && !swift(>=4.0))) /// Implicitly unwrapped Optional Dictionary of Mappable object with a transform public func <- (left: inout Dictionary!, right: (Map, Transform)) where Transform.Object: BaseMappable { let (map, transform) = right @@ -294,6 +308,7 @@ public func <- (left: inout Dictionary>> right } } +#endif /// Dictionary of Mappable objects with a transform public func <- (left: inout Dictionary, right: (Map, Transform)) where Transform.Object: BaseMappable { @@ -367,6 +382,8 @@ public func >>> (left: Dictionary=4.1.50) || (swift(>=3.4) && !swift(>=4.0))) /// Implicitly unwrapped Optional Dictionary of Mappable object with a transform public func <- (left: inout Dictionary!, right: (Map, Transform)) where Transform.Object: BaseMappable { let (map, transform) = right @@ -387,6 +404,7 @@ public func <- (left: inout Dictionary>> right } } +#endif // MARK:- Array of Mappable objects with transforms - Array @@ -435,6 +453,8 @@ public func >>> (left: Array?, right } +// 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 <- (left: inout Array!, right: (Map, Transform)) where Transform.Object: BaseMappable { let (map, transform) = right @@ -447,6 +467,7 @@ public func <- (left: inout Array!, default: () } } +#endif // MARK:- Array of Array of objects - Array>> with transforms @@ -458,9 +479,15 @@ public func <- (left: inout [[Transform.Object]], righ left >>> right case .fromJSON where map.isKeyPresent: guard let original2DArray = map.currentValue as? [[Any]] else { break } + #if swift(>=4.1) let transformed2DArray = original2DArray.compactMap { values in fromJSONArrayWithTransform(values as Any?, transform: transform) } + #else + let transformed2DArray = original2DArray.flatMap { values in + fromJSONArrayWithTransform(values as Any?, transform: transform) + } + #endif FromJSON.basicType(&left, object: transformed2DArray) default: break @@ -470,9 +497,15 @@ public func <- (left: inout [[Transform.Object]], righ public func >>> (left: [[Transform.Object]], right: (Map, Transform)) { let (map, transform) = right if map.mappingType == .toJSON{ + #if swift(>=4.1) let transformed2DArray = left.compactMap { values in toJSONArrayWithTransform(values, transform: transform) } + #else + let transformed2DArray = left.flatMap { values in + toJSONArrayWithTransform(values, transform: transform) + } + #endif ToJSON.basicType(transformed2DArray, map: map) } } @@ -485,9 +518,15 @@ public func <- (left: inout [[Transform.Object]]?, rig left >>> right case .fromJSON where map.isKeyPresent: guard let original2DArray = map.currentValue as? [[Any]] else { break } + #if swift(>=4.1) let transformed2DArray = original2DArray.compactMap { values in fromJSONArrayWithTransform(values as Any?, transform: transform) } + #else + let transformed2DArray = original2DArray.flatMap { values in + fromJSONArrayWithTransform(values as Any?, transform: transform) + } + #endif FromJSON.optionalBasicType(&left, object: transformed2DArray) default: break @@ -497,14 +536,22 @@ public func <- (left: inout [[Transform.Object]]?, rig public func >>> (left: [[Transform.Object]]?, right: (Map, Transform)) { let (map, transform) = right if map.mappingType == .toJSON { + #if swift(>=4.1) let transformed2DArray = left?.compactMap { values in toJSONArrayWithTransform(values, transform: transform) } + #else + let transformed2DArray = left?.flatMap { values in + toJSONArrayWithTransform(values, transform: transform) + } + #endif ToJSON.optionalBasicType(transformed2DArray, map: 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 array of objects with transform public func <- (left: inout [[Transform.Object]]!, right: (Map, Transform)) { let (map, transform) = right @@ -513,14 +560,21 @@ public func <- (left: inout [[Transform.Object]]!, rig left >>> right case .fromJSON where map.isKeyPresent: guard let original2DArray = map.currentValue as? [[Any]] else { break } + #if swift(>=4.1) let transformed2DArray = original2DArray.compactMap { values in fromJSONArrayWithTransform(values as Any?, transform: transform) } + #else + let transformed2DArray = original2DArray.flatMap { values in + fromJSONArrayWithTransform(values as Any?, transform: transform) + } + #endif FromJSON.optionalBasicType(&left, object: transformed2DArray) default: break } } +#endif // MARK:- Set of Mappable objects with a transform - Set @@ -572,6 +626,8 @@ public func >>> (left: Set?, right: } +// 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 with transform public func <- (left: inout Set!, right: (Map, Transform)) where Transform.Object: BaseMappable { let (map, transform) = right @@ -585,13 +641,20 @@ public func <- (left: inout Set!, ri default: () } } +#endif private func fromJSONArrayWithTransform(_ input: Any?, transform: Transform) -> [Transform.Object]? { if let values = input as? [Any] { + #if swift(>=4.1) + return values.compactMap { value in + return transform.transformFromJSON(value) + } + #else return values.flatMap { value in return transform.transformFromJSON(value) } + #endif } else { return nil } @@ -608,9 +671,15 @@ private func fromJSONDictionaryWithTransform(_ input: } private func toJSONArrayWithTransform(_ input: [Transform.Object]?, transform: Transform) -> [Transform.JSON]? { + #if swift(>=4.1) + return input?.compactMap { value in + return transform.transformToJSON(value) + } + #else return input?.flatMap { value in return transform.transformToJSON(value) } + #endif } private func toJSONDictionaryWithTransform(_ input: [String: Transform.Object]?, transform: Transform) -> [String: Transform.JSON]? {