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.

398 lines
11 KiB

  1. //
  2. // Operators.swift
  3. // ObjectMapper
  4. //
  5. // Created by Tristan Himmelman on 2014-10-09.
  6. //
  7. // The MIT License (MIT)
  8. //
  9. // Copyright (c) 2014-2016 Hearst
  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. /**
  29. * This file defines a new operator which is used to create a mapping between an object and a JSON key value.
  30. * There is an overloaded operator definition for each type of object that is supported in ObjectMapper.
  31. * This provides a way to add custom logic to handle specific types of objects
  32. */
  33. /// Operator used for defining mappings to and from JSON
  34. infix operator <-
  35. /// Operator used to define mappings to JSON
  36. infix operator >>>
  37. // MARK:- Objects with Basic types
  38. /// Object of Basic type
  39. public func <- <T>(left: inout T, right: Map) {
  40. switch right.mappingType {
  41. case .fromJSON where right.isKeyPresent:
  42. FromJSON.basicType(&left, object: right.value())
  43. case .toJSON:
  44. left >>> right
  45. default: ()
  46. }
  47. }
  48. public func >>> <T>(left: T, right: Map) {
  49. if right.mappingType == .toJSON {
  50. ToJSON.basicType(left, map: right)
  51. }
  52. }
  53. /// Optional object of basic type
  54. public func <- <T>(left: inout T?, right: Map) {
  55. switch right.mappingType {
  56. case .fromJSON where right.isKeyPresent:
  57. FromJSON.optionalBasicType(&left, object: right.value())
  58. case .toJSON:
  59. left >>> right
  60. default: ()
  61. }
  62. }
  63. public func >>> <T>(left: T?, right: Map) {
  64. if right.mappingType == .toJSON {
  65. ToJSON.optionalBasicType(left, map: right)
  66. }
  67. }
  68. // Code targeting the Swift 4.1 compiler and below.
  69. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
  70. /// Implicitly unwrapped optional object of basic type
  71. public func <- <T>(left: inout T!, right: Map) {
  72. switch right.mappingType {
  73. case .fromJSON where right.isKeyPresent:
  74. FromJSON.optionalBasicType(&left, object: right.value())
  75. case .toJSON:
  76. left >>> right
  77. default: ()
  78. }
  79. }
  80. #endif
  81. // MARK:- Mappable Objects - <T: BaseMappable>
  82. /// Object conforming to Mappable
  83. public func <- <T: BaseMappable>(left: inout T, right: Map) {
  84. switch right.mappingType {
  85. case .fromJSON:
  86. FromJSON.object(&left, map: right)
  87. case .toJSON:
  88. left >>> right
  89. }
  90. }
  91. public func >>> <T: BaseMappable>(left: T, right: Map) {
  92. if right.mappingType == .toJSON {
  93. ToJSON.object(left, map: right)
  94. }
  95. }
  96. /// Optional Mappable objects
  97. public func <- <T: BaseMappable>(left: inout T?, right: Map) {
  98. switch right.mappingType {
  99. case .fromJSON where right.isKeyPresent:
  100. FromJSON.optionalObject(&left, map: right)
  101. case .toJSON:
  102. left >>> right
  103. default: ()
  104. }
  105. }
  106. public func >>> <T: BaseMappable>(left: T?, right: Map) {
  107. if right.mappingType == .toJSON {
  108. ToJSON.optionalObject(left, map: right)
  109. }
  110. }
  111. // Code targeting the Swift 4.1 compiler and below.
  112. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
  113. /// Implicitly unwrapped optional Mappable objects
  114. public func <- <T: BaseMappable>(left: inout T!, right: Map) {
  115. switch right.mappingType {
  116. case .fromJSON where right.isKeyPresent:
  117. FromJSON.optionalObject(&left, map: right)
  118. case .toJSON:
  119. left >>> right
  120. default: ()
  121. }
  122. }
  123. #endif
  124. // MARK:- Dictionary of Mappable objects - Dictionary<String, T: BaseMappable>
  125. /// Dictionary of Mappable objects <String, T: Mappable>
  126. public func <- <T: BaseMappable>(left: inout Dictionary<String, T>, right: Map) {
  127. switch right.mappingType {
  128. case .fromJSON where right.isKeyPresent:
  129. FromJSON.objectDictionary(&left, map: right)
  130. case .toJSON:
  131. left >>> right
  132. default: ()
  133. }
  134. }
  135. public func >>> <T: BaseMappable>(left: Dictionary<String, T>, right: Map) {
  136. if right.mappingType == .toJSON {
  137. ToJSON.objectDictionary(left, map: right)
  138. }
  139. }
  140. /// Optional Dictionary of Mappable object <String, T: Mappable>
  141. public func <- <T: BaseMappable>(left: inout Dictionary<String, T>?, right: Map) {
  142. switch right.mappingType {
  143. case .fromJSON where right.isKeyPresent:
  144. FromJSON.optionalObjectDictionary(&left, map: right)
  145. case .toJSON:
  146. left >>> right
  147. default: ()
  148. }
  149. }
  150. public func >>> <T: BaseMappable>(left: Dictionary<String, T>?, right: Map) {
  151. if right.mappingType == .toJSON {
  152. ToJSON.optionalObjectDictionary(left, map: right)
  153. }
  154. }
  155. // Code targeting the Swift 4.1 compiler and below.
  156. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
  157. /// Implicitly unwrapped Optional Dictionary of Mappable object <String, T: Mappable>
  158. public func <- <T: BaseMappable>(left: inout Dictionary<String, T>!, right: Map) {
  159. switch right.mappingType {
  160. case .fromJSON where right.isKeyPresent:
  161. FromJSON.optionalObjectDictionary(&left, map: right)
  162. case .toJSON:
  163. left >>> right
  164. default: ()
  165. }
  166. }
  167. #endif
  168. /// Dictionary of Mappable objects <String, T: Mappable>
  169. public func <- <T: BaseMappable>(left: inout Dictionary<String, [T]>, right: Map) {
  170. switch right.mappingType {
  171. case .fromJSON where right.isKeyPresent:
  172. FromJSON.objectDictionaryOfArrays(&left, map: right)
  173. case .toJSON:
  174. left >>> right
  175. default: ()
  176. }
  177. }
  178. public func >>> <T: BaseMappable>(left: Dictionary<String, [T]>, right: Map) {
  179. if right.mappingType == .toJSON {
  180. ToJSON.objectDictionaryOfArrays(left, map: right)
  181. }
  182. }
  183. /// Optional Dictionary of Mappable object <String, T: Mappable>
  184. public func <- <T: BaseMappable>(left: inout Dictionary<String, [T]>?, right: Map) {
  185. switch right.mappingType {
  186. case .fromJSON where right.isKeyPresent:
  187. FromJSON.optionalObjectDictionaryOfArrays(&left, map: right)
  188. case .toJSON:
  189. left >>> right
  190. default: ()
  191. }
  192. }
  193. public func >>> <T: BaseMappable>(left: Dictionary<String, [T]>?, right: Map) {
  194. if right.mappingType == .toJSON {
  195. ToJSON.optionalObjectDictionaryOfArrays(left, map: right)
  196. }
  197. }
  198. // Code targeting the Swift 4.1 compiler and below.
  199. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
  200. /// Implicitly unwrapped Optional Dictionary of Mappable object <String, T: Mappable>
  201. public func <- <T: BaseMappable>(left: inout Dictionary<String, [T]>!, right: Map) {
  202. switch right.mappingType {
  203. case .fromJSON where right.isKeyPresent:
  204. FromJSON.optionalObjectDictionaryOfArrays(&left, map: right)
  205. case .toJSON:
  206. left >>> right
  207. default: ()
  208. }
  209. }
  210. #endif
  211. // MARK:- Array of Mappable objects - Array<T: BaseMappable>
  212. /// Array of Mappable objects
  213. public func <- <T: BaseMappable>(left: inout Array<T>, right: Map) {
  214. switch right.mappingType {
  215. case .fromJSON where right.isKeyPresent:
  216. FromJSON.objectArray(&left, map: right)
  217. case .toJSON:
  218. left >>> right
  219. default: ()
  220. }
  221. }
  222. public func >>> <T: BaseMappable>(left: Array<T>, right: Map) {
  223. if right.mappingType == .toJSON {
  224. ToJSON.objectArray(left, map: right)
  225. }
  226. }
  227. /// Optional array of Mappable objects
  228. public func <- <T: BaseMappable>(left: inout Array<T>?, right: Map) {
  229. switch right.mappingType {
  230. case .fromJSON where right.isKeyPresent:
  231. FromJSON.optionalObjectArray(&left, map: right)
  232. case .toJSON:
  233. left >>> right
  234. default: ()
  235. }
  236. }
  237. public func >>> <T: BaseMappable>(left: Array<T>?, right: Map) {
  238. if right.mappingType == .toJSON {
  239. ToJSON.optionalObjectArray(left, map: right)
  240. }
  241. }
  242. // Code targeting the Swift 4.1 compiler and below.
  243. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
  244. /// Implicitly unwrapped Optional array of Mappable objects
  245. public func <- <T: BaseMappable>(left: inout Array<T>!, right: Map) {
  246. switch right.mappingType {
  247. case .fromJSON where right.isKeyPresent:
  248. FromJSON.optionalObjectArray(&left, map: right)
  249. case .toJSON:
  250. left >>> right
  251. default: ()
  252. }
  253. }
  254. #endif
  255. // MARK:- Array of Array of Mappable objects - Array<Array<T: BaseMappable>>
  256. /// Array of Array Mappable objects
  257. public func <- <T: BaseMappable>(left: inout Array<Array<T>>, right: Map) {
  258. switch right.mappingType {
  259. case .fromJSON where right.isKeyPresent:
  260. FromJSON.twoDimensionalObjectArray(&left, map: right)
  261. case .toJSON:
  262. left >>> right
  263. default: ()
  264. }
  265. }
  266. public func >>> <T: BaseMappable>(left: Array<Array<T>>, right: Map) {
  267. if right.mappingType == .toJSON {
  268. ToJSON.twoDimensionalObjectArray(left, map: right)
  269. }
  270. }
  271. /// Optional array of Mappable objects
  272. public func <- <T: BaseMappable>(left:inout Array<Array<T>>?, right: Map) {
  273. switch right.mappingType {
  274. case .fromJSON where right.isKeyPresent:
  275. FromJSON.optionalTwoDimensionalObjectArray(&left, map: right)
  276. case .toJSON:
  277. left >>> right
  278. default: ()
  279. }
  280. }
  281. public func >>> <T: BaseMappable>(left: Array<Array<T>>?, right: Map) {
  282. if right.mappingType == .toJSON {
  283. ToJSON.optionalTwoDimensionalObjectArray(left, map: right)
  284. }
  285. }
  286. // Code targeting the Swift 4.1 compiler and below.
  287. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
  288. /// Implicitly unwrapped Optional array of Mappable objects
  289. public func <- <T: BaseMappable>(left: inout Array<Array<T>>!, right: Map) {
  290. switch right.mappingType {
  291. case .fromJSON where right.isKeyPresent:
  292. FromJSON.optionalTwoDimensionalObjectArray(&left, map: right)
  293. case .toJSON:
  294. left >>> right
  295. default: ()
  296. }
  297. }
  298. #endif
  299. // MARK:- Set of Mappable objects - Set<T: BaseMappable>
  300. /// Set of Mappable objects
  301. public func <- <T: BaseMappable>(left: inout Set<T>, right: Map) {
  302. switch right.mappingType {
  303. case .fromJSON where right.isKeyPresent:
  304. FromJSON.objectSet(&left, map: right)
  305. case .toJSON:
  306. left >>> right
  307. default: ()
  308. }
  309. }
  310. public func >>> <T: BaseMappable>(left: Set<T>, right: Map) {
  311. if right.mappingType == .toJSON {
  312. ToJSON.objectSet(left, map: right)
  313. }
  314. }
  315. /// Optional Set of Mappable objects
  316. public func <- <T: BaseMappable>(left: inout Set<T>?, right: Map) {
  317. switch right.mappingType {
  318. case .fromJSON where right.isKeyPresent:
  319. FromJSON.optionalObjectSet(&left, map: right)
  320. case .toJSON:
  321. left >>> right
  322. default: ()
  323. }
  324. }
  325. public func >>> <T: BaseMappable>(left: Set<T>?, right: Map) {
  326. if right.mappingType == .toJSON {
  327. ToJSON.optionalObjectSet(left, map: right)
  328. }
  329. }
  330. // Code targeting the Swift 4.1 compiler and below.
  331. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
  332. /// Implicitly unwrapped Optional Set of Mappable objects
  333. public func <- <T: BaseMappable>(left: inout Set<T>!, right: Map) {
  334. switch right.mappingType {
  335. case .fromJSON where right.isKeyPresent:
  336. FromJSON.optionalObjectSet(&left, map: right)
  337. case .toJSON:
  338. left >>> right
  339. default: ()
  340. }
  341. }
  342. #endif