DocumentID.swift 4.5 KB

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