DocumentID.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. /// A type that can initialize itself from a Firestore `DocumentReference`,
  18. /// which makes it suitable for use with the `@DocumentID` property wrapper.
  19. ///
  20. /// Firestore includes extensions that make `String` and `DocumentReference`
  21. /// conform to `DocumentIDWrappable`.
  22. ///
  23. /// Note that Firestore ignores fields annotated with `@DocumentID` when writing
  24. /// so there is no requirement to convert from the wrapped type back to a
  25. /// `DocumentReference`.
  26. public protocol DocumentIDWrappable {
  27. /// Creates a new instance by converting from the given `DocumentReference`.
  28. static func wrap(_ documentReference: DocumentReference) throws -> Self
  29. }
  30. extension String: DocumentIDWrappable {
  31. public static func wrap(_ documentReference: DocumentReference) throws -> Self {
  32. return documentReference.documentID
  33. }
  34. }
  35. extension DocumentReference: DocumentIDWrappable {
  36. public static func wrap(_ documentReference: DocumentReference) throws -> Self {
  37. // Swift complains that values of type DocumentReference cannot be returned
  38. // as Self which is nonsensical. The cast forces this to work.
  39. return documentReference as! Self
  40. }
  41. }
  42. /// An internal protocol that allows Firestore.Decoder to test if a type is a
  43. /// DocumentID of some kind without knowing the specific generic parameter that
  44. /// the user actually used.
  45. ///
  46. /// This is required because Swift does not define an existential type for all
  47. /// instances of a generic class--that is, it has no wildcard or raw type that
  48. /// matches a generic without any specific parameter. Swift does define an
  49. /// existential type for protocols though, so this protocol (to which DocumentID
  50. /// conforms) indirectly makes it possible to test for and act on any
  51. /// `DocumentID<Value>`.
  52. internal protocol DocumentIDProtocol {
  53. /// Initializes the DocumentID from a DocumentReference.
  54. init(from documentReference: DocumentReference?) throws
  55. }
  56. /// A value that is populated in Codable objects with the `DocumentReference`
  57. /// of the current document by the Firestore.Decoder when a document is read.
  58. ///
  59. /// If the field name used for this type conflicts with a read document field,
  60. /// an error is thrown. For example, if a custom object has a field `firstName`
  61. /// annotated with `@DocumentID`, and there is a property from the document
  62. /// named `firstName` as well, an error is thrown when you try to read the
  63. /// document.
  64. ///
  65. /// When writing a Codable object containing an `@DocumentID` annotated field,
  66. /// its value is ignored. This allows you to read a document from one path and
  67. /// write it into another without adjusting the value here.
  68. ///
  69. /// NOTE: Trying to encode/decode this type using encoders/decoders other than
  70. /// Firestore.Encoder leads to an error.
  71. @propertyWrapper
  72. public struct DocumentID<Value: DocumentIDWrappable & Codable>:
  73. DocumentIDProtocol, Codable {
  74. var value: Value?
  75. public init(wrappedValue value: Value?) {
  76. self.value = value
  77. }
  78. public var wrappedValue: Value? {
  79. get { value }
  80. set { value = newValue }
  81. }
  82. // MARK: - `DocumentIDProtocol` conformance
  83. public init(from documentReference: DocumentReference?) throws {
  84. if let documentReference = documentReference {
  85. value = try Value.wrap(documentReference)
  86. } else {
  87. value = nil
  88. }
  89. }
  90. // MARK: - `Codable` implementation.
  91. public init(from decoder: Decoder) throws {
  92. throw FirestoreDecodingError.decodingIsNotSupported(
  93. "DocumentID values can only be decoded with Firestore.Decoder"
  94. )
  95. }
  96. public func encode(to encoder: Encoder) throws {
  97. throw FirestoreEncodingError.encodingIsNotSupported(
  98. "DocumentID values can only be encoded with Firestore.Encoder"
  99. )
  100. }
  101. }
  102. extension DocumentID: Equatable where Value: Equatable {}
  103. extension DocumentID: Hashable where Value: Hashable {}