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