ExplicitNull.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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: Encodable where Value: Encodable {
  37. public func encode(to encoder: Encoder) throws {
  38. var container = encoder.singleValueContainer()
  39. if let value = value {
  40. try container.encode(value)
  41. } else {
  42. try container.encodeNil()
  43. }
  44. }
  45. }
  46. extension ExplicitNull: Decodable where Value: Decodable {
  47. public init(from decoder: Decoder) throws {
  48. let container = try decoder.singleValueContainer()
  49. if container.decodeNil() {
  50. value = nil
  51. } else {
  52. value = try container.decode(Value.self)
  53. }
  54. }
  55. }
  56. #endif // compiler(>=5.1)
  57. /// A compatibility version of `ExplicitNull` that does not use property
  58. /// wrappers, suitable for use in older versions of Swift.
  59. ///
  60. /// Wraps an `Optional` field in a `Codable` object such that when the field
  61. /// has a `nil` value it will encode to a null value in Firestore. Normally,
  62. /// optional fields are omitted from the encoded document.
  63. ///
  64. /// This is useful for ensuring a field is present in a Firestore document,
  65. /// even when there is no associated value.
  66. @available(swift, deprecated: 5.1)
  67. public enum Swift4ExplicitNull<Wrapped> {
  68. case none
  69. case some(Wrapped)
  70. /// Create a `ExplicitNull` object from `Optional`.
  71. public init(_ optional: Wrapped?) {
  72. switch optional {
  73. case .none:
  74. self = .none
  75. case let .some(wrapped):
  76. self = .some(wrapped)
  77. }
  78. }
  79. /// Returns this value as an `Optional<Wrapped>`.
  80. public var optionalValue: Wrapped? {
  81. switch self {
  82. case .none:
  83. return .none
  84. case let .some(wrapped):
  85. return .some(wrapped)
  86. }
  87. }
  88. }
  89. @available(swift, deprecated: 5.1)
  90. extension Swift4ExplicitNull: Equatable where Wrapped: Equatable {}
  91. @available(swift, deprecated: 5.1)
  92. extension Swift4ExplicitNull: Encodable where Wrapped: Encodable {
  93. public func encode(to encoder: Encoder) throws {
  94. var container = encoder.singleValueContainer()
  95. switch self {
  96. case .none:
  97. try container.encodeNil()
  98. case let .some(wrapped):
  99. try container.encode(wrapped)
  100. }
  101. }
  102. }
  103. @available(swift, deprecated: 5.1)
  104. extension Swift4ExplicitNull: Decodable where Wrapped: Decodable {
  105. public init(from decoder: Decoder) throws {
  106. let container = try decoder.singleValueContainer()
  107. if container.decodeNil() {
  108. self = .none
  109. } else {
  110. self = .some(try container.decode(Wrapped.self))
  111. }
  112. }
  113. }