IntegerOperators.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // IntegerOperators.swift
  3. // ObjectMapper
  4. //
  5. // Created by Suyeol Jeon on 17/02/2017.
  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. import Foundation
  29. // MARK: - Signed Integer
  30. /// SignedInteger mapping
  31. public func <- <T: SignedInteger>(left: inout T, right: Map) {
  32. switch right.mappingType {
  33. case .fromJSON where right.isKeyPresent:
  34. let value: T = toSignedInteger(right.currentValue) ?? 0
  35. FromJSON.basicType(&left, object: value)
  36. case .toJSON:
  37. left >>> right
  38. default: ()
  39. }
  40. }
  41. /// Optional SignedInteger mapping
  42. public func <- <T: SignedInteger>(left: inout T?, right: Map) {
  43. switch right.mappingType {
  44. case .fromJSON where right.isKeyPresent:
  45. let value: T? = toSignedInteger(right.currentValue)
  46. FromJSON.basicType(&left, object: value)
  47. case .toJSON:
  48. left >>> right
  49. default: ()
  50. }
  51. }
  52. // Code targeting the Swift 4.1 compiler and below.
  53. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
  54. /// ImplicitlyUnwrappedOptional SignedInteger mapping
  55. public func <- <T: SignedInteger>(left: inout T!, right: Map) {
  56. switch right.mappingType {
  57. case .fromJSON where right.isKeyPresent:
  58. let value: T! = toSignedInteger(right.currentValue)
  59. FromJSON.basicType(&left, object: value)
  60. case .toJSON:
  61. left >>> right
  62. default: ()
  63. }
  64. }
  65. #endif
  66. // MARK: - Unsigned Integer
  67. /// UnsignedInteger mapping
  68. public func <- <T: UnsignedInteger>(left: inout T, right: Map) {
  69. switch right.mappingType {
  70. case .fromJSON where right.isKeyPresent:
  71. let value: T = toUnsignedInteger(right.currentValue) ?? 0
  72. FromJSON.basicType(&left, object: value)
  73. case .toJSON:
  74. left >>> right
  75. default: ()
  76. }
  77. }
  78. /// Optional UnsignedInteger mapping
  79. public func <- <T: UnsignedInteger>(left: inout T?, right: Map) {
  80. switch right.mappingType {
  81. case .fromJSON where right.isKeyPresent:
  82. let value: T? = toUnsignedInteger(right.currentValue)
  83. FromJSON.basicType(&left, object: value)
  84. case .toJSON:
  85. left >>> right
  86. default: ()
  87. }
  88. }
  89. // Code targeting the Swift 4.1 compiler and below.
  90. #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
  91. /// ImplicitlyUnwrappedOptional UnsignedInteger mapping
  92. public func <- <T: UnsignedInteger>(left: inout T!, right: Map) {
  93. switch right.mappingType {
  94. case .fromJSON where right.isKeyPresent:
  95. let value: T! = toUnsignedInteger(right.currentValue)
  96. FromJSON.basicType(&left, object: value)
  97. case .toJSON:
  98. left >>> right
  99. default: ()
  100. }
  101. }
  102. #endif
  103. // MARK: - Casting Utils
  104. /// Convert any value to `SignedInteger`.
  105. private func toSignedInteger<T: SignedInteger>(_ value: Any?) -> T? {
  106. guard
  107. let value = value,
  108. case let number as NSNumber = value
  109. else {
  110. return nil
  111. }
  112. if T.self == Int.self, let x = Int(exactly: number.int64Value) {
  113. return T.init(x)
  114. }
  115. if T.self == Int8.self, let x = Int8(exactly: number.int64Value) {
  116. return T.init(x)
  117. }
  118. if T.self == Int16.self, let x = Int16(exactly: number.int64Value) {
  119. return T.init(x)
  120. }
  121. if T.self == Int32.self, let x = Int32(exactly: number.int64Value) {
  122. return T.init(x)
  123. }
  124. if T.self == Int64.self, let x = Int64(exactly: number.int64Value) {
  125. return T.init(x)
  126. }
  127. return nil
  128. }
  129. /// Convert any value to `UnsignedInteger`.
  130. private func toUnsignedInteger<T: UnsignedInteger>(_ value: Any?) -> T? {
  131. guard
  132. let value = value,
  133. case let number as NSNumber = value
  134. else {
  135. return nil
  136. }
  137. if T.self == UInt.self, let x = UInt(exactly: number.uint64Value) {
  138. return T.init(x)
  139. }
  140. if T.self == UInt8.self, let x = UInt8(exactly: number.uint64Value) {
  141. return T.init(x)
  142. }
  143. if T.self == UInt16.self, let x = UInt16(exactly: number.uint64Value) {
  144. return T.init(x)
  145. }
  146. if T.self == UInt32.self, let x = UInt32(exactly: number.uint64Value) {
  147. return T.init(x)
  148. }
  149. if T.self == UInt64.self, let x = UInt64(exactly: number.uint64Value) {
  150. return T.init(x)
  151. }
  152. return nil
  153. }