FieldValue+Encodable.swift 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. /** Extends FieldValue to conform to Encodable. */
  18. extension FieldValue: Encodable {
  19. /// Encoding a FieldValue will throw by default unless the encoder implementation
  20. /// explicitly handles it, which is what FirestoreEncoder does.
  21. public func encode(to encoder: Encoder) throws {
  22. throw FirestoreEncodingError.encodingIsNotSupported
  23. }
  24. }
  25. /** Swift enums providing alternatives to direct usages of FieldValue. */
  26. /// Wraps around Timestamp and FieldValue.serverTimestamp to support modeling
  27. /// timestamps in custom classes.
  28. ///
  29. /// Example:
  30. /// struct CustomModel {
  31. /// var ts: ServerTimestamp
  32. /// }
  33. /// Then `CustomModel(ts: .pending)` will tell server to fill `ts` with current
  34. /// timestamp.
  35. public enum ServerTimestamp: Codable, Equatable {
  36. /// When being read (decoded) from Firestore, NSNull values will be mapped to `pending`.
  37. /// When being written (encoded) to Firestore, `pending` means requesting server to
  38. /// set timestamp on the field (essentially setting value
  39. /// to FieldValue.serverTimestamp()).
  40. case pending
  41. /// When being read (decoded) from Firestore, non-nil Timestamp will be mapped to
  42. /// `resolved`.
  43. /// When being written (encoded) to Firestore, `resolved(stamp)` will set the field
  44. /// value to `stamp`.
  45. case resolved(Timestamp)
  46. public init(from decoder: Decoder) throws {
  47. let container = try decoder.singleValueContainer()
  48. if container.decodeNil() {
  49. self = .pending
  50. } else {
  51. let value = try container.decode(Timestamp.self)
  52. self = .resolved(value)
  53. }
  54. }
  55. public func encode(to encoder: Encoder) throws {
  56. var container = encoder.singleValueContainer()
  57. switch self {
  58. case .pending:
  59. try container.encode(FieldValue.serverTimestamp())
  60. case let .resolved(value: value):
  61. try container.encode(value)
  62. }
  63. }
  64. }