Skip to content

Commit

Permalink
fix: Build with ObjectMapper 3.5.2
Browse files Browse the repository at this point in the history
tristanhimmelman/ObjectMapper#1079 has moved
the initializers of `BaseMappable` to `Mappable`. As a workaround we're
defining them once again for `BaseMappable`.

refs KDEV-509
  • Loading branch information
mbektchiev committed Apr 2, 2020
1 parent 7ed934b commit 08f35ee
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 36 deletions.
20 changes: 10 additions & 10 deletions Cartfile.resolved
@@ -1,12 +1,12 @@
github "Hearst-DD/ObjectMapper" "3.5.1"
github "Quick/Nimble" "v8.0.4"
github "Hearst-DD/ObjectMapper" "3.5.2"
github "Quick/Nimble" "v8.0.7"
github "Quick/Quick" "v2.2.0"
github "SwiftyBeaver/SwiftyBeaver" "1.8.1"
github "httpswift/swifter" "70355c4e414e93a0589a0d5d4ade342534bbab38"
github "kif-framework/KIF" "v3.7.8"
github "kishikawakatsumi/KeychainAccess" "v3.2.0"
github "mxcl/PromiseKit" "6.11.0"
github "pubnub/objective-c" "v4.10.1"
github "realm/realm-cocoa" "v3.19.0"
github "SwiftyBeaver/SwiftyBeaver" "1.9.0"
github "httpswift/swifter" "fbffd02ab42306be4748ec4b8ad58e6c77e09e84"
github "kif-framework/KIF" "v3.7.9"
github "kishikawakatsumi/KeychainAccess" "v3.2.1"
github "mxcl/PromiseKit" "6.13.1"
github "pubnub/objective-c" "v4.13.1"
github "realm/realm-cocoa" "v3.21.0"
github "tjboneman/NSPredicate-MongoDB-Adaptor" "fce0cd01913bd4393db0c3dd33404cb7e9ebec88"
github "weichsel/ZIPFoundation" "0.9.9"
github "weichsel/ZIPFoundation" "0.9.11"
76 changes: 50 additions & 26 deletions Kinvey/Kinvey/ObjectMapperSupport.swift
Expand Up @@ -30,22 +30,46 @@ public typealias BaseMappable = ObjectMapper.BaseMappable
@available(*, deprecated, message: "Deprecated in version 3.18.0. Please use Swift.Codable instead")
public typealias MapContext = ObjectMapper.MapContext

// Workaround regression introduced with https://github.com/tristanhimmelman/ObjectMapper/pull/1079
// These extension methods have been moved to Mappable which breaks our code because we need to call
// them on Mappable instances (e.g. StaticMappable doesn't conform to Mappable, but only to BaseMappable)
public extension BaseMappable {

/// Initializes object from a JSON String
init?(JSONString: String, context: MapContext? = nil) {
if let obj: Self = Mapper(context: context).map(JSONString: JSONString) {
self = obj
} else {
return nil
}
}

/// Initializes object from a JSON Dictionary
init?(JSON: [String: Any], context: MapContext? = nil) {
if let obj: Self = Mapper(context: context).map(JSON: JSON) {
self = obj
} else {
return nil
}
}
}

@available(*, deprecated, message: "Deprecated in version 3.18.0. Please use Swift.Codable instead")
extension Map {

@available(*, deprecated, message: "Deprecated in version 3.18.0. Please use Swift.Codable instead")
public subscript<Key: RawRepresentable>(key: Key) -> Map where Key.RawValue == String {
return self[key.rawValue]
}

}

extension JSONDecodable where Self: BaseMappable {

public mutating func refreshMappable(from json: [String : Any]) throws {
mapping(map: ObjectMapper.Map(mappingType: .fromJSON, JSON: json))
}

public static func decodeMappable(from data: Data) throws -> Self {
guard let jsonObject = try JSONSerialization.jsonObject(with: data) as? [String : Any] else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "Data can't be converted to a Dictionary"))
Expand All @@ -55,29 +79,29 @@ extension JSONDecodable where Self: BaseMappable {
}
return _self
}

public static func decodeMappableArray(from data: Data) throws -> [Any] {
guard let jsonObjectArray = try JSONSerialization.jsonObject(with: data) as? [[String : Any]] else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "Data can't be converted to a Array of Dictionaries"))
}
return [Self](JSONArray: jsonObjectArray)
}

public static func decodeMappable(from dictionary: [String : Any]) throws -> Self {
guard let _self = Self(JSON: dictionary) else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "Data can't be converted to \(Self.self)"))
}
return _self
}

}

extension JSONEncodable where Self: BaseMappable {

func encodeMappable() throws -> [String : Any] {
return self.toJSON()
}

}

/// Override operator used during the `propertyMapping(_:)` method.
Expand Down Expand Up @@ -270,12 +294,12 @@ public func <- (left: List<BoolValue>, right: (String, Map)) {

@available(*, deprecated, message: "Deprecated in version 3.18.0. Please use Swift.Codable instead")
extension NSPredicate: StaticMappable {

@available(*, deprecated, message: "Deprecated in version 3.18.0. Please use Swift.Codable instead")
public static func objectForMapping(map: Map) -> BaseMappable? {
return nil
}

@available(*, deprecated, message: "Deprecated in version 3.18.0. Please use Swift.Codable instead")
public func mapping(map: Map) {
if let json = mongoDBQuery {
Expand All @@ -284,14 +308,14 @@ extension NSPredicate: StaticMappable {
}
}
}

}

@available(*, deprecated, message: "Deprecated in version 3.18.0. Please use Swift.Codable instead")
class GeoPointTransform: TransformOf<GeoPoint, [CLLocationDegrees]> {

static let shared = GeoPointTransform()

init() {
super.init(fromJSON: { (array) -> GeoPoint? in
if let array = array, array.count == 2 {
Expand All @@ -305,12 +329,12 @@ class GeoPointTransform: TransformOf<GeoPoint, [CLLocationDegrees]> {
return nil
})
}

}

@available(*, deprecated, message: "Deprecated in version 3.18.0. Please use Swift.Codable instead")
class ListValueTransform<T: RealmSwift.Object>: TransformOf<List<T>, [JsonDictionary]> where T: BaseMappable {

init(_ list: List<T>) {
super.init(fromJSON: { (array) -> List<T>? in
if let array = array {
Expand All @@ -330,7 +354,7 @@ class ListValueTransform<T: RealmSwift.Object>: TransformOf<List<T>, [JsonDictio
return nil
})
}

}

// MARK: String Value Transform
Expand Down Expand Up @@ -449,10 +473,10 @@ class BoolValueTransform: TransformOf<List<BoolValue>, [Bool]> {
}

class AclTransformType {

typealias Object = [String]
typealias JSON = String

func transformFromJSON(_ value: Any?) -> [String]? {
if let value = value as? String,
let data = value.data(using: String.Encoding.utf8),
Expand All @@ -463,7 +487,7 @@ class AclTransformType {
}
return nil
}

func transformToJSON(_ value: [String]?) -> String? {
if let value = value,
let data = try? JSONSerialization.data(withJSONObject: value),
Expand All @@ -473,25 +497,25 @@ class AclTransformType {
}
return nil
}

}

struct AnyTransform: ObjectMapper.TransformType {

private let _transformFromJSON: (Any?) -> Any?
private let _transformToJSON: (Any?) -> Any?

init<Transform: ObjectMapper.TransformType>(_ transform: Transform) {
_transformFromJSON = { transform.transformFromJSON($0) }
_transformToJSON = { transform.transformToJSON($0 as? Transform.Object) }
}

func transformFromJSON(_ value: Any?) -> Any? {
return _transformFromJSON(value)
}

func transformToJSON(_ value: Any?) -> Any? {
return _transformToJSON(value)
}

}

0 comments on commit 08f35ee

Please sign in to comment.