ExplicitNull.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import FirebaseFirestore
  17. #if compiler(>=5.1)
  18. /// Wraps an `Optional` field in a `Codable` object such that when the field
  19. /// has a `nil` value it will encode to a null value in Firestore. Normally,
  20. /// optional fields are omitted from the encoded document.
  21. ///
  22. /// This is useful for ensuring a field is present in a Firestore document,
  23. /// even when there is no associated value.
  24. @propertyWrapper
  25. public struct ExplicitNull<Value> {
  26. var value: Value?
  27. public init(wrappedValue value: Value?) {
  28. self.value = value
  29. }
  30. public var wrappedValue: Value? {
  31. get { value }
  32. set { value = newValue }
  33. }
  34. }
  35. extension ExplicitNull: Equatable where Value: Equatable {}
  36. extension ExplicitNull: Hashable where Value: Hashable {}
  37. extension ExplicitNull: Encodable where Value: Encodable {
  38. public func encode(to encoder: Encoder) throws {
  39. var container = encoder.singleValueContainer()
  40. if let value = value {
  41. try container.encode(value)
  42. } else {
  43. try container.encodeNil()
  44. }
  45. }
  46. }
  47. extension ExplicitNull: Decodable where Value: Decodable {
  48. public init(from decoder: Decoder) throws {
  49. let container = try decoder.singleValueContainer()
  50. if container.decodeNil() {
  51. value = nil
  52. } else {
  53. value = try container.decode(Value.self)
  54. }
  55. }
  56. }
  57. #endif // compiler(>=5.1)
  58. /// A compatibility version of `ExplicitNull` that does not use property
  59. /// wrappers, suitable for use in older versions of Swift.
  60. ///
  61. /// Wraps an `Optional` field in a `Codable` object such that when the field
  62. /// has a `nil` value it will encode to a null value in Firestore. Normally,
  63. /// optional fields are omitted from the encoded document.
  64. ///
  65. /// This is useful for ensuring a field is present in a Firestore document,
  66. /// even when there is no associated value.
  67. @available(swift, deprecated: 5.1)
  68. public enum Swift4ExplicitNull<Wrapped> {
  69. case none
  70. case some(Wrapped)
  71. /// Create a `ExplicitNull` object from `Optional`.
  72. public init(_ optional: Wrapped?) {
  73. switch optional {
  74. case .none:
  75. self = .none
  76. case let .some(wrapped):
  77. self = .some(wrapped)
  78. }
  79. }
  80. /// Returns this value as an `Optional<Wrapped>`.
  81. public var optionalValue: Wrapped? {
  82. switch self {
  83. case .none:
  84. return .none
  85. case let .some(wrapped):
  86. return .some(wrapped)
  87. }
  88. }
  89. }
  90. @available(swift, deprecated: 5.1)
  91. extension Swift4ExplicitNull: Equatable where Wrapped: Equatable {}
  92. @available(swift, deprecated: 5.1)
  93. extension Swift4ExplicitNull: Encodable where Wrapped: Encodable {
  94. public func encode(to encoder: Encoder) throws {
  95. var container = encoder.singleValueContainer()
  96. switch self {
  97. case .none:
  98. try container.encodeNil()
  99. case let .some(wrapped):
  100. try container.encode(wrapped)
  101. }
  102. }
  103. }
  104. @available(swift, deprecated: 5.1)
  105. extension Swift4ExplicitNull: Decodable where Wrapped: Decodable {
  106. public init(from decoder: Decoder) throws {
  107. let container = try decoder.singleValueContainer()
  108. if container.decodeNil() {
  109. self = .none
  110. } else {
  111. self = .some(try container.decode(Wrapped.self))
  112. }
  113. }
  114. }