-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #608 from Hearst-DD/immutable-mappable
Immutable mappable
- Loading branch information
Showing
43 changed files
with
1,976 additions
and
779 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
// | ||
// ImmutableMappble.swift | ||
// ObjectMapper | ||
// | ||
// Created by Suyeol Jeon on 23/09/2016. | ||
// | ||
// The MIT License (MIT) | ||
// | ||
// Copyright (c) 2014-2016 Hearst | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
public protocol ImmutableMappable: BaseMappable { | ||
init(map: Map) throws | ||
} | ||
|
||
public extension ImmutableMappable { | ||
|
||
/// Implement this method to support object -> JSON transform. | ||
public func mapping(map: Map) {} | ||
|
||
/// Initializes object from a JSON String | ||
public init(JSONString: String, context: MapContext? = nil) throws { | ||
self = try Mapper(context: context).map(JSONString: JSONString) | ||
} | ||
|
||
/// Initializes object from a JSON Dictionary | ||
public init(JSON: [String: Any], context: MapContext? = nil) throws { | ||
self = try Mapper(context: context).map(JSON: JSON) | ||
} | ||
|
||
} | ||
|
||
public extension Map { | ||
|
||
fileprivate func currentValue(for key: String) -> Any? { | ||
return self[key].currentValue | ||
} | ||
|
||
// MARK: Basic | ||
|
||
/// Returns a value or throws an error. | ||
public func value<T>(_ key: String, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> T { | ||
let currentValue = self.currentValue(for: key) | ||
guard let value = currentValue as? T else { | ||
throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '\(T.self)'", file: file, function: function, line: line) | ||
} | ||
return value | ||
} | ||
|
||
/// Returns a transformed value or throws an error. | ||
public func value<Transform: TransformType>(_ key: String, using transform: Transform, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> Transform.Object { | ||
let currentValue = self.currentValue(for: key) | ||
guard let value = transform.transformFromJSON(currentValue) else { | ||
throw MapError(key: key, currentValue: currentValue, reason: "Cannot transform to '\(Transform.Object.self)' using \(transform)", file: file, function: function, line: line) | ||
} | ||
return value | ||
} | ||
|
||
// MARK: BaseMappable | ||
|
||
/// Returns a `BaseMappable` object or throws an error. | ||
public func value<T: BaseMappable>(_ key: String) throws -> T { | ||
let currentValue = self.currentValue(for: key) | ||
return try Mapper<T>().mapOrFail(JSONObject: currentValue) | ||
} | ||
|
||
// MARK: [BaseMappable] | ||
|
||
/// Returns a `[BaseMappable]` or throws an error. | ||
public func value<T: BaseMappable>(_ key: String, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [T] { | ||
let currentValue = self.currentValue(for: key) | ||
guard let jsonArray = currentValue as? [Any] else { | ||
throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[Any]'", file: file, function: function, line: line) | ||
} | ||
return try jsonArray.enumerated().map { i, JSONObject -> T in | ||
return try Mapper<T>().mapOrFail(JSONObject: JSONObject) | ||
} | ||
} | ||
|
||
/// Returns a `[BaseMappable]` using transform or throws an error. | ||
public func value<Transform: TransformType>(_ key: String, using transform: Transform, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [Transform.Object] { | ||
let currentValue = self.currentValue(for: key) | ||
guard let jsonArray = currentValue as? [Any] else { | ||
throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[Any]'", file: file, function: function, line: line) | ||
} | ||
return try jsonArray.enumerated().map { i, json -> Transform.Object in | ||
guard let object = transform.transformFromJSON(json) else { | ||
throw MapError(key: "\(key)[\(i)]", currentValue: json, reason: "Cannot transform to '\(Transform.Object.self)' using \(transform)", file: file, function: function, line: line) | ||
} | ||
return object | ||
} | ||
} | ||
|
||
// MARK: [String: BaseMappable] | ||
|
||
/// Returns a `[String: BaseMappable]` or throws an error. | ||
public func value<T: BaseMappable>(_ key: String, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [String: T] { | ||
let currentValue = self.currentValue(for: key) | ||
guard let jsonDictionary = currentValue as? [String: Any] else { | ||
throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[String: Any]'", file: file, function: function, line: line) | ||
} | ||
var value: [String: T] = [:] | ||
for (key, json) in jsonDictionary { | ||
value[key] = try Mapper<T>().mapOrFail(JSONObject: json) | ||
} | ||
return value | ||
} | ||
|
||
/// Returns a `[String: BaseMappable]` using transform or throws an error. | ||
public func value<Transform: TransformType>(_ key: String, using transform: Transform, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [String: Transform.Object] { | ||
let currentValue = self.currentValue(for: key) | ||
guard let jsonDictionary = currentValue as? [String: Any] else { | ||
throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[String: Any]'", file: file, function: function, line: line) | ||
} | ||
var value: [String: Transform.Object] = [:] | ||
for (key, json) in jsonDictionary { | ||
guard let object = transform.transformFromJSON(json) else { | ||
throw MapError(key: key, currentValue: json, reason: "Cannot transform to '\(Transform.Object.self)' using \(transform)", file: file, function: function, line: line) | ||
} | ||
value[key] = object | ||
} | ||
return value | ||
} | ||
|
||
} | ||
|
||
public extension Mapper where N: ImmutableMappable { | ||
|
||
public func map(JSON: [String: Any]) throws -> N { | ||
return try self.mapOrFail(JSON: JSON) | ||
} | ||
|
||
public func map(JSONString: String) throws -> N { | ||
return try mapOrFail(JSONString: JSONString) | ||
} | ||
|
||
public func map(JSONObject: Any?) throws -> N { | ||
return try mapOrFail(JSONObject: JSONObject) | ||
} | ||
|
||
} | ||
|
||
internal extension Mapper where N: BaseMappable { | ||
|
||
internal func mapOrFail(JSON: [String: Any]) throws -> N { | ||
let map = Map(mappingType: .fromJSON, JSON: JSON, context: context) | ||
|
||
// Check if object is ImmutableMappable, if so use ImmutableMappable protocol for mapping | ||
if let klass = N.self as? ImmutableMappable.Type, | ||
var object = try klass.init(map: map) as? N { | ||
object.mapping(map: map) | ||
return object | ||
} | ||
|
||
// If not, map the object the standard way | ||
guard let value = self.map(JSON: JSON) else { | ||
throw MapError(key: nil, currentValue: JSON, reason: "Cannot map to '\(N.self)'") | ||
} | ||
return value | ||
} | ||
|
||
internal func mapOrFail(JSONString: String) throws -> N { | ||
guard let JSON = Mapper.parseJSONStringIntoDictionary(JSONString: JSONString) else { | ||
throw MapError(key: nil, currentValue: JSONString, reason: "Cannot parse into '[String: Any]'") | ||
} | ||
return try mapOrFail(JSON: JSON) | ||
} | ||
|
||
internal func mapOrFail(JSONObject: Any?) throws -> N { | ||
guard let JSON = JSONObject as? [String: Any] else { | ||
throw MapError(key: nil, currentValue: JSONObject, reason: "Cannot cast to '[String: Any]'") | ||
} | ||
return try mapOrFail(JSON: JSON) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// MapError.swift | ||
// ObjectMapper | ||
// | ||
// Created by Tristan Himmelman on 2016-09-26. | ||
// | ||
// The MIT License (MIT) | ||
// | ||
// Copyright (c) 2014-2016 Hearst | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
import Foundation | ||
|
||
public struct MapError: Error { | ||
public var key: String? | ||
public var currentValue: Any? | ||
public var reason: String? | ||
public var file: StaticString? | ||
public var function: StaticString? | ||
public var line: UInt? | ||
|
||
init(key: String?, currentValue: Any?, reason: String?, file: StaticString? = nil, function: StaticString? = nil, line: UInt? = nil) { | ||
self.key = key | ||
self.currentValue = currentValue | ||
self.reason = reason | ||
self.file = file | ||
self.function = function | ||
self.line = line | ||
} | ||
} | ||
|
||
extension MapError: CustomStringConvertible { | ||
|
||
private var location: String? { | ||
guard let file = file, let function = function, let line = line else { return nil } | ||
let fileName = ((String(describing: file).components(separatedBy: "/").last ?? "").components(separatedBy: ".").first ?? "") | ||
return "\(fileName).\(function):\(line)" | ||
} | ||
|
||
public var description: String { | ||
let info: [(String, Any?)] = [ | ||
("- reason", reason), | ||
("- location", location), | ||
("- key", key), | ||
("- currentValue", currentValue), | ||
] | ||
let infoString = info.map { "\($0): \($1 ?? "nil")" }.joined(separator: "\n") | ||
return "Got an error while mapping.\n\(infoString)" | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.