ObjectiveCSupport+BSON.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2020 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. import Realm
  19. /**
  20. :nodoc:
  21. **/
  22. public extension ObjectiveCSupport {
  23. /// Convert an `AnyBSON` to a `RLMBSON`.
  24. static func convert(object: AnyBSON?) -> RLMBSON? {
  25. guard let object = object else {
  26. return nil
  27. }
  28. switch object {
  29. case .int32(let val):
  30. return val as NSNumber
  31. case .int64(let val):
  32. return val as NSNumber
  33. case .double(let val):
  34. return val as NSNumber
  35. case .string(let val):
  36. return val as NSString
  37. case .binary(let val):
  38. return val as NSData
  39. case .datetime(let val):
  40. return val as NSDate
  41. case .decimal128(let val):
  42. return val as RLMDecimal128
  43. case .objectId(let val):
  44. return val as RLMObjectId
  45. case .document(let val):
  46. return val.reduce(into: Dictionary<String, RLMBSON?>()) { (result: inout [String: RLMBSON?], kvp) in
  47. result[kvp.key] = convert(object: kvp.value) ?? NSNull()
  48. } as NSDictionary
  49. case .array(let val):
  50. return val.map(convert) as NSArray
  51. case .maxKey:
  52. return MaxKey()
  53. case .minKey:
  54. return MinKey()
  55. case .regex(let val):
  56. return val
  57. case .bool(let val):
  58. return val as NSNumber
  59. default:
  60. return nil
  61. }
  62. }
  63. /// Convert a `RLMBSON` to an `AnyBSON`.
  64. static func convert(object: RLMBSON?) -> AnyBSON? {
  65. guard let bson = object else {
  66. return nil
  67. }
  68. switch bson.__bsonType {
  69. case .null:
  70. return nil
  71. case .int32:
  72. guard let val = bson as? NSNumber else {
  73. return nil
  74. }
  75. return .int32(Int32(val.intValue))
  76. case .int64:
  77. guard let val = bson as? NSNumber else {
  78. return nil
  79. }
  80. return .int64(Int64(val.int64Value))
  81. case .bool:
  82. guard let val = bson as? NSNumber else {
  83. return nil
  84. }
  85. return .bool(val.boolValue)
  86. case .double:
  87. guard let val = bson as? NSNumber else {
  88. return nil
  89. }
  90. return .double(val.doubleValue)
  91. case .string:
  92. guard let val = bson as? NSString else {
  93. return nil
  94. }
  95. return .string(val as String)
  96. case .binary:
  97. guard let val = bson as? NSData else {
  98. return nil
  99. }
  100. return .binary(val as Data)
  101. case .timestamp:
  102. guard let val = bson as? NSDate else {
  103. return nil
  104. }
  105. return .timestamp(val as Date)
  106. case .datetime:
  107. guard let val = bson as? NSDate else {
  108. return nil
  109. }
  110. return .datetime(val as Date)
  111. case .objectId:
  112. guard let val = bson as? RLMObjectId,
  113. let oid = try? ObjectId(string: val.stringValue) else {
  114. return nil
  115. }
  116. return .objectId(oid)
  117. case .decimal128:
  118. guard let val = bson as? RLMDecimal128 else {
  119. return nil
  120. }
  121. return .decimal128(Decimal128(stringLiteral: val.stringValue))
  122. case .regularExpression:
  123. guard let val = bson as? NSRegularExpression else {
  124. return nil
  125. }
  126. return .regex(val)
  127. case .maxKey:
  128. return .maxKey
  129. case .minKey:
  130. return .minKey
  131. case .document:
  132. guard let val = bson as? Dictionary<String, RLMBSON?> else {
  133. return nil
  134. }
  135. return .document(val.reduce(into: Dictionary<String, AnyBSON?>()) { (result: inout [String: AnyBSON?], kvp) in
  136. result[kvp.key] = convert(object: kvp.value)
  137. })
  138. case .array:
  139. guard let val = bson as? Array<RLMBSON?> else {
  140. return nil
  141. }
  142. return .array(val.map(convert))
  143. default:
  144. return nil
  145. }
  146. }
  147. }