You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

376 lines
17 KiB

  1. //
  2. // ImmutableMappble.swift
  3. // ObjectMapper
  4. //
  5. // Created by Suyeol Jeon on 23/09/2016.
  6. //
  7. // The MIT License (MIT)
  8. //
  9. // Copyright (c) 2014-2018 Tristan Himmelman
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy
  12. // of this software and associated documentation files (the "Software"), to deal
  13. // in the Software without restriction, including without limitation the rights
  14. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. // copies of the Software, and to permit persons to whom the Software is
  16. // furnished to do so, subject to the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be included in
  19. // all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. // THE SOFTWARE.
  28. public protocol ImmutableMappable: BaseMappable {
  29. init(map: Map) throws
  30. }
  31. public extension ImmutableMappable {
  32. /// Implement this method to support object -> JSON transform.
  33. func mapping(map: Map) {}
  34. /// Initializes object from a JSON String
  35. init(JSONString: String, context: MapContext? = nil) throws {
  36. self = try Mapper(context: context).map(JSONString: JSONString)
  37. }
  38. /// Initializes object from a JSON Dictionary
  39. init(JSON: [String: Any], context: MapContext? = nil) throws {
  40. self = try Mapper(context: context).map(JSON: JSON)
  41. }
  42. /// Initializes object from a JSONObject
  43. init(JSONObject: Any, context: MapContext? = nil) throws {
  44. self = try Mapper(context: context).map(JSONObject: JSONObject)
  45. }
  46. }
  47. public extension Map {
  48. fileprivate func currentValue(for key: String, nested: Bool? = nil, delimiter: String = ".") -> Any? {
  49. let isNested = nested ?? key.contains(delimiter)
  50. return self[key, nested: isNested, delimiter: delimiter].currentValue
  51. }
  52. // MARK: Basic
  53. /// Returns a value or throws an error.
  54. func value<T>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> T {
  55. let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
  56. guard let value = currentValue as? T else {
  57. throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '\(T.self)'", file: file, function: function, line: line)
  58. }
  59. return value
  60. }
  61. /// Returns a transformed value or throws an error.
  62. func value<Transform: TransformType>(_ key: String, nested: Bool? = nil, delimiter: String = ".", using transform: Transform, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> Transform.Object {
  63. let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
  64. guard let value = transform.transformFromJSON(currentValue) else {
  65. throw MapError(key: key, currentValue: currentValue, reason: "Cannot transform to '\(Transform.Object.self)' using \(transform)", file: file, function: function, line: line)
  66. }
  67. return value
  68. }
  69. /// Returns a RawRepresentable type or throws an error.
  70. func value<T: RawRepresentable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> T {
  71. return try self.value(key, nested: nested, delimiter: delimiter, using: EnumTransform(), file: file, function: function, line: line)
  72. }
  73. /// Returns a RawRepresentable type or throws an error.
  74. func value<T: RawRepresentable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> T? {
  75. return try self.value(key, nested: nested, delimiter: delimiter, using: EnumTransform(), file: file, function: function, line: line)
  76. }
  77. /// Returns a `[RawRepresentable]` type or throws an error.
  78. func value<T: RawRepresentable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [T] {
  79. return try self.value(key, nested: nested, delimiter: delimiter, using: EnumTransform(), file: file, function: function, line: line)
  80. }
  81. /// Returns a `[RawRepresentable]` type or throws an error.
  82. func value<T: RawRepresentable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [T]? {
  83. return try self.value(key, nested: nested, delimiter: delimiter, using: EnumTransform(), file: file, function: function, line: line)
  84. }
  85. // MARK: BaseMappable
  86. /// Returns a `BaseMappable` object or throws an error.
  87. func value<T: BaseMappable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> T {
  88. let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
  89. guard let JSONObject = currentValue else {
  90. throw MapError(key: key, currentValue: currentValue, reason: "Found unexpected nil value", file: file, function: function, line: line)
  91. }
  92. return try Mapper<T>(context: context).mapOrFail(JSONObject: JSONObject)
  93. }
  94. /// Returns a `BaseMappable` object boxed in `Optional` or throws an error.
  95. func value<T: BaseMappable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> T? {
  96. let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
  97. guard let JSONObject = currentValue else {
  98. throw MapError(key: key, currentValue: currentValue, reason: "Found unexpected nil value", file: file, function: function, line: line)
  99. }
  100. return try Mapper<T>(context: context).mapOrFail(JSONObject: JSONObject)
  101. }
  102. // MARK: [BaseMappable]
  103. /// Returns a `[BaseMappable]` or throws an error.
  104. func value<T: BaseMappable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [T] {
  105. let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
  106. guard let jsonArray = currentValue as? [Any] else {
  107. throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[Any]'", file: file, function: function, line: line)
  108. }
  109. return try jsonArray.map { JSONObject -> T in
  110. return try Mapper<T>(context: context).mapOrFail(JSONObject: JSONObject)
  111. }
  112. }
  113. /// Returns a `[BaseMappable]` boxed in `Optional` or throws an error.
  114. func value<T: BaseMappable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [T]? {
  115. let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
  116. guard let jsonArray = currentValue as? [Any] else {
  117. throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[Any]'", file: file, function: function, line: line)
  118. }
  119. return try jsonArray.map { JSONObject -> T in
  120. return try Mapper<T>(context: context).mapOrFail(JSONObject: JSONObject)
  121. }
  122. }
  123. /// Returns a `[BaseMappable]` using transform or throws an error.
  124. func value<Transform: TransformType>(_ key: String, nested: Bool? = nil, delimiter: String = ".", using transform: Transform, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [Transform.Object] {
  125. let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
  126. guard let jsonArray = currentValue as? [Any] else {
  127. throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[Any]'", file: file, function: function, line: line)
  128. }
  129. return try jsonArray.map { json -> Transform.Object in
  130. guard let object = transform.transformFromJSON(json) else {
  131. throw MapError(key: "\(key)", currentValue: json, reason: "Cannot transform to '\(Transform.Object.self)' using \(transform)", file: file, function: function, line: line)
  132. }
  133. return object
  134. }
  135. }
  136. // MARK: [String: BaseMappable]
  137. /// Returns a `[String: BaseMappable]` or throws an error.
  138. func value<T: BaseMappable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [String: T] {
  139. let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
  140. guard let jsonDictionary = currentValue as? [String: Any] else {
  141. throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[String: Any]'", file: file, function: function, line: line)
  142. }
  143. return try jsonDictionary.mapValues { json in
  144. return try Mapper<T>(context: context).mapOrFail(JSONObject: json)
  145. }
  146. }
  147. /// Returns a `[String: BaseMappable]` boxed in `Optional` or throws an error.
  148. func value<T: BaseMappable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [String: T]? {
  149. let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
  150. guard let jsonDictionary = currentValue as? [String: Any] else {
  151. throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[String: Any]'", file: file, function: function, line: line)
  152. }
  153. var value: [String: T] = [:]
  154. for (key, json) in jsonDictionary {
  155. value[key] = try Mapper<T>(context: context).mapOrFail(JSONObject: json)
  156. }
  157. return value
  158. }
  159. /// Returns a `[String: BaseMappable]` using transform or throws an error.
  160. func value<Transform: TransformType>(_ key: String, nested: Bool? = nil, delimiter: String = ".", using transform: Transform, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [String: Transform.Object] {
  161. let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
  162. guard let jsonDictionary = currentValue as? [String: Any] else {
  163. throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[String: Any]'", file: file, function: function, line: line)
  164. }
  165. return try jsonDictionary.mapValues { json in
  166. guard let object = transform.transformFromJSON(json) else {
  167. throw MapError(key: key, currentValue: json, reason: "Cannot transform to '\(Transform.Object.self)' using \(transform)", file: file, function: function, line: line)
  168. }
  169. return object
  170. }
  171. }
  172. /// Returns a `[String: BaseMappable]` using transform or throws an error.
  173. func value<T: BaseMappable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [[T]]? {
  174. let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
  175. guard let json2DArray = currentValue as? [[Any]] else {
  176. throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[[Any]]'", file: file, function: function, line: line)
  177. }
  178. return try json2DArray.map { jsonArray in
  179. try jsonArray.map { jsonObject -> T in
  180. return try Mapper<T>(context: context).mapOrFail(JSONObject: jsonObject)
  181. }
  182. }
  183. }
  184. // MARK: [[BaseMappable]]
  185. /// Returns a `[[BaseMappable]]` or throws an error.
  186. func value<T: BaseMappable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [[T]] {
  187. let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
  188. guard let json2DArray = currentValue as? [[Any]] else {
  189. throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[[Any]]'", file: file, function: function, line: line)
  190. }
  191. return try json2DArray.map { jsonArray in
  192. try jsonArray.map { jsonObject -> T in
  193. return try Mapper<T>(context: context).mapOrFail(JSONObject: jsonObject)
  194. }
  195. }
  196. }
  197. /// Returns a `[[BaseMappable]]` using transform or throws an error.
  198. func value<Transform: TransformType>(_ key: String, nested: Bool? = nil, delimiter: String = ".", using transform: Transform, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [[Transform.Object]] {
  199. let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
  200. guard let json2DArray = currentValue as? [[Any]] else {
  201. throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[[Any]]'",
  202. file: file, function: function, line: line)
  203. }
  204. return try json2DArray.map { jsonArray in
  205. try jsonArray.map { json -> Transform.Object in
  206. guard let object = transform.transformFromJSON(json) else {
  207. throw MapError(key: "\(key)", currentValue: json, reason: "Cannot transform to '\(Transform.Object.self)' using \(transform)", file: file, function: function, line: line)
  208. }
  209. return object
  210. }
  211. }
  212. }
  213. }
  214. public extension Mapper where N: ImmutableMappable {
  215. func map(JSON: [String: Any]) throws -> N {
  216. return try self.mapOrFail(JSON: JSON)
  217. }
  218. func map(JSONString: String) throws -> N {
  219. return try mapOrFail(JSONString: JSONString)
  220. }
  221. func map(JSONObject: Any) throws -> N {
  222. return try mapOrFail(JSONObject: JSONObject)
  223. }
  224. // MARK: Array mapping functions
  225. func mapArray(JSONArray: [[String: Any]]) throws -> [N] {
  226. #if swift(>=4.1)
  227. return try JSONArray.compactMap(mapOrFail)
  228. #else
  229. return try JSONArray.flatMap(mapOrFail)
  230. #endif
  231. }
  232. func mapArray(JSONString: String) throws -> [N] {
  233. guard let JSONObject = Mapper.parseJSONString(JSONString: JSONString) else {
  234. throw MapError(key: nil, currentValue: JSONString, reason: "Cannot convert string into Any'")
  235. }
  236. return try mapArray(JSONObject: JSONObject)
  237. }
  238. func mapArray(JSONObject: Any) throws -> [N] {
  239. guard let JSONArray = JSONObject as? [[String: Any]] else {
  240. throw MapError(key: nil, currentValue: JSONObject, reason: "Cannot cast to '[[String: Any]]'")
  241. }
  242. return try mapArray(JSONArray: JSONArray)
  243. }
  244. // MARK: Dictionary mapping functions
  245. func mapDictionary(JSONString: String) throws -> [String: N] {
  246. guard let JSONObject = Mapper.parseJSONString(JSONString: JSONString) else {
  247. throw MapError(key: nil, currentValue: JSONString, reason: "Cannot convert string into Any'")
  248. }
  249. return try mapDictionary(JSONObject: JSONObject)
  250. }
  251. func mapDictionary(JSONObject: Any?) throws -> [String: N] {
  252. guard let JSON = JSONObject as? [String: [String: Any]] else {
  253. throw MapError(key: nil, currentValue: JSONObject, reason: "Cannot cast to '[String: [String: Any]]''")
  254. }
  255. return try mapDictionary(JSON: JSON)
  256. }
  257. func mapDictionary(JSON: [String: [String: Any]]) throws -> [String: N] {
  258. return try JSON.filterMap(mapOrFail)
  259. }
  260. // MARK: Dictinoary of arrays mapping functions
  261. func mapDictionaryOfArrays(JSONObject: Any?) throws -> [String: [N]] {
  262. guard let JSON = JSONObject as? [String: [[String: Any]]] else {
  263. throw MapError(key: nil, currentValue: JSONObject, reason: "Cannot cast to '[String: [String: Any]]''")
  264. }
  265. return try mapDictionaryOfArrays(JSON: JSON)
  266. }
  267. func mapDictionaryOfArrays(JSON: [String: [[String: Any]]]) throws -> [String: [N]] {
  268. return try JSON.filterMap { array -> [N] in
  269. try mapArray(JSONArray: array)
  270. }
  271. }
  272. // MARK: 2 dimentional array mapping functions
  273. func mapArrayOfArrays(JSONObject: Any?) throws -> [[N]] {
  274. guard let JSONArray = JSONObject as? [[[String: Any]]] else {
  275. throw MapError(key: nil, currentValue: JSONObject, reason: "Cannot cast to '[[[String: Any]]]''")
  276. }
  277. return try JSONArray.map(mapArray)
  278. }
  279. }
  280. internal extension Mapper {
  281. func mapOrFail(JSON: [String: Any]) throws -> N {
  282. let map = Map(mappingType: .fromJSON, JSON: JSON, context: context, shouldIncludeNilValues: shouldIncludeNilValues)
  283. // Check if object is ImmutableMappable, if so use ImmutableMappable protocol for mapping
  284. if let klass = N.self as? ImmutableMappable.Type,
  285. var object = try klass.init(map: map) as? N {
  286. object.mapping(map: map)
  287. return object
  288. }
  289. // If not, map the object the standard way
  290. guard let value = self.map(JSON: JSON) else {
  291. throw MapError(key: nil, currentValue: JSON, reason: "Cannot map to '\(N.self)'")
  292. }
  293. return value
  294. }
  295. func mapOrFail(JSONString: String) throws -> N {
  296. guard let JSON = Mapper.parseJSONStringIntoDictionary(JSONString: JSONString) else {
  297. throw MapError(key: nil, currentValue: JSONString, reason: "Cannot parse into '[String: Any]'")
  298. }
  299. return try mapOrFail(JSON: JSON)
  300. }
  301. func mapOrFail(JSONObject: Any) throws -> N {
  302. guard let JSON = JSONObject as? [String: Any] else {
  303. throw MapError(key: nil, currentValue: JSONObject, reason: "Cannot cast to '[String: Any]'")
  304. }
  305. return try mapOrFail(JSON: JSON)
  306. }
  307. }