ServerTimestamp.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright 2019 Google LLC
  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. #if SWIFT_PACKAGE
  17. @_exported import FirebaseFirestoreInternalWrapper
  18. #else
  19. @_exported import FirebaseFirestoreInternal
  20. #endif // SWIFT_PACKAGE
  21. /// A type that can initialize itself from a Firestore Timestamp, which makes
  22. /// it suitable for use with the `@ServerTimestamp` property wrapper.
  23. ///
  24. /// Firestore includes extensions that make `Timestamp` and `Date` conform to
  25. /// `ServerTimestampWrappable`.
  26. public protocol ServerTimestampWrappable {
  27. /// Creates a new instance by converting from the given `Timestamp`.
  28. ///
  29. /// - Parameter timestamp: The timestamp from which to convert.
  30. static func wrap(_ timestamp: Timestamp) throws -> Self
  31. /// Converts this value into a Firestore `Timestamp`.
  32. ///
  33. /// - Returns: A `Timestamp` representation of this value.
  34. static func unwrap(_ value: Self) throws -> Timestamp
  35. }
  36. extension Date: ServerTimestampWrappable {
  37. public static func wrap(_ timestamp: Timestamp) throws -> Self {
  38. return timestamp.dateValue()
  39. }
  40. public static func unwrap(_ value: Self) throws -> Timestamp {
  41. return Timestamp(date: value)
  42. }
  43. }
  44. extension Timestamp: ServerTimestampWrappable {
  45. public static func wrap(_ timestamp: Timestamp) throws -> Self {
  46. return timestamp as! Self
  47. }
  48. public static func unwrap(_ value: Timestamp) throws -> Timestamp {
  49. return value
  50. }
  51. }
  52. /// A property wrapper that marks an `Optional<Timestamp>` field to be
  53. /// populated with a server timestamp. If a `Codable` object being written
  54. /// contains a `nil` for an `@ServerTimestamp`-annotated field, it will be
  55. /// replaced with `FieldValue.serverTimestamp()` as it is sent.
  56. ///
  57. /// Example:
  58. /// ```
  59. /// struct CustomModel {
  60. /// @ServerTimestamp var ts: Timestamp?
  61. /// }
  62. /// ```
  63. ///
  64. /// Then writing `CustomModel(ts: nil)` will tell server to fill `ts` with
  65. /// current timestamp.
  66. @propertyWrapper
  67. public struct ServerTimestamp<Value>: Codable
  68. where Value: ServerTimestampWrappable & Codable {
  69. var value: Value?
  70. public init(wrappedValue value: Value?) {
  71. self.value = value
  72. }
  73. public var wrappedValue: Value? {
  74. get { value }
  75. set { value = newValue }
  76. }
  77. // MARK: Codable
  78. public init(from decoder: Decoder) throws {
  79. let container = try decoder.singleValueContainer()
  80. if container.decodeNil() {
  81. value = nil
  82. } else {
  83. value = try Value.wrap(container.decode(Timestamp.self))
  84. }
  85. }
  86. public func encode(to encoder: Encoder) throws {
  87. var container = encoder.singleValueContainer()
  88. if let value = value {
  89. try container.encode(Value.unwrap(value))
  90. } else {
  91. try container.encode(FieldValue.serverTimestamp())
  92. }
  93. }
  94. }
  95. extension ServerTimestamp: Equatable where Value: Equatable {}
  96. extension ServerTimestamp: Hashable where Value: Hashable {}