ServerTimestamp.swift 3.2 KB

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