FIRDocumentSnapshot.mm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright 2017 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 "FIRDocumentSnapshot+Internal.h"
  17. #include <utility>
  18. #include <vector>
  19. #include "Firestore/core/src/util/warnings.h"
  20. #import "Firestore/Source/API/FIRDocumentReference+Internal.h"
  21. #import "Firestore/Source/API/FIRFieldPath+Internal.h"
  22. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  23. #import "Firestore/Source/API/FIRGeoPoint+Internal.h"
  24. #import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h"
  25. #import "Firestore/Source/API/FIRTimestamp+Internal.h"
  26. #import "Firestore/Source/API/FSTUserDataWriter.h"
  27. #import "Firestore/Source/API/converters.h"
  28. #include "Firestore/Protos/nanopb/google/firestore/v1/document.nanopb.h"
  29. #include "Firestore/core/src/api/document_reference.h"
  30. #include "Firestore/core/src/api/document_snapshot.h"
  31. #include "Firestore/core/src/api/firestore.h"
  32. #include "Firestore/core/src/api/settings.h"
  33. #include "Firestore/core/src/model/database_id.h"
  34. #include "Firestore/core/src/model/document_key.h"
  35. #include "Firestore/core/src/model/field_path.h"
  36. #include "Firestore/core/src/nanopb/nanopb_util.h"
  37. #include "Firestore/core/src/remote/serializer.h"
  38. #include "Firestore/core/src/util/exception.h"
  39. #include "Firestore/core/src/util/hard_assert.h"
  40. #include "Firestore/core/src/util/log.h"
  41. #include "Firestore/core/src/util/string_apple.h"
  42. using firebase::firestore::google_firestore_v1_Value;
  43. using firebase::firestore::api::DocumentSnapshot;
  44. using firebase::firestore::api::Firestore;
  45. using firebase::firestore::api::MakeFIRGeoPoint;
  46. using firebase::firestore::api::MakeFIRTimestamp;
  47. using firebase::firestore::api::SnapshotMetadata;
  48. using firebase::firestore::model::DatabaseId;
  49. using firebase::firestore::model::Document;
  50. using firebase::firestore::model::DocumentKey;
  51. using firebase::firestore::model::FieldPath;
  52. using firebase::firestore::model::ObjectValue;
  53. using firebase::firestore::nanopb::MakeNSData;
  54. using firebase::firestore::remote::Serializer;
  55. using firebase::firestore::util::MakeNSString;
  56. using firebase::firestore::util::MakeString;
  57. using firebase::firestore::util::ThrowInvalidArgument;
  58. NS_ASSUME_NONNULL_BEGIN
  59. @implementation FIRDocumentSnapshot {
  60. DocumentSnapshot _snapshot;
  61. std::unique_ptr<Serializer> _serializer;
  62. FIRSnapshotMetadata *_cachedMetadata;
  63. }
  64. - (instancetype)initWithSnapshot:(DocumentSnapshot &&)snapshot {
  65. if (self = [super init]) {
  66. _snapshot = std::move(snapshot);
  67. _serializer.reset(new Serializer(_snapshot.firestore()->database_id()));
  68. }
  69. return self;
  70. }
  71. - (instancetype)initWithFirestore:(FIRFirestore *)firestore
  72. documentKey:(DocumentKey)documentKey
  73. document:(const absl::optional<Document> &)document
  74. metadata:(SnapshotMetadata)metadata {
  75. DocumentSnapshot wrapped;
  76. if (document.has_value()) {
  77. wrapped =
  78. DocumentSnapshot::FromDocument(firestore.wrapped, document.value(), std::move(metadata));
  79. } else {
  80. wrapped = DocumentSnapshot::FromNoDocument(firestore.wrapped, std::move(documentKey),
  81. std::move(metadata));
  82. }
  83. _serializer.reset(new Serializer(firestore.databaseID));
  84. return [self initWithSnapshot:std::move(wrapped)];
  85. }
  86. - (instancetype)initWithFirestore:(FIRFirestore *)firestore
  87. documentKey:(DocumentKey)documentKey
  88. document:(const absl::optional<Document> &)document
  89. fromCache:(bool)fromCache
  90. hasPendingWrites:(bool)hasPendingWrites {
  91. return [self initWithFirestore:firestore
  92. documentKey:std::move(documentKey)
  93. document:document
  94. metadata:SnapshotMetadata(hasPendingWrites, fromCache)];
  95. }
  96. // NSObject Methods
  97. - (BOOL)isEqual:(nullable id)other {
  98. if (other == self) return YES;
  99. // self class could be FIRDocumentSnapshot or subtype. So we compare with base type explicitly.
  100. if (![other isKindOfClass:[FIRDocumentSnapshot class]]) return NO;
  101. return _snapshot == static_cast<FIRDocumentSnapshot *>(other)->_snapshot;
  102. }
  103. - (NSUInteger)hash {
  104. return _snapshot.Hash();
  105. }
  106. @dynamic exists;
  107. - (BOOL)exists {
  108. return _snapshot.exists();
  109. }
  110. - (const absl::optional<Document> &)internalDocument {
  111. return _snapshot.internal_document();
  112. }
  113. - (FIRDocumentReference *)reference {
  114. return [[FIRDocumentReference alloc] initWithReference:_snapshot.CreateReference()];
  115. }
  116. - (NSString *)documentID {
  117. return MakeNSString(_snapshot.document_id());
  118. }
  119. @dynamic metadata;
  120. - (FIRSnapshotMetadata *)metadata {
  121. if (!_cachedMetadata) {
  122. _cachedMetadata = [[FIRSnapshotMetadata alloc] initWithMetadata:_snapshot.metadata()];
  123. }
  124. return _cachedMetadata;
  125. }
  126. - (nullable NSDictionary<NSString *, id> *)data {
  127. return [self dataWithServerTimestampBehavior:FIRServerTimestampBehaviorNone];
  128. }
  129. - (nullable NSDictionary<NSString *, id> *)dataWithServerTimestampBehavior:
  130. (FIRServerTimestampBehavior)serverTimestampBehavior {
  131. absl::optional<google_firestore_v1_Value> data = _snapshot.GetValue(FieldPath::EmptyPath());
  132. if (!data) return nil;
  133. FSTUserDataWriter *dataWriter =
  134. [[FSTUserDataWriter alloc] initWithFirestore:_snapshot.firestore()
  135. serverTimestampBehavior:serverTimestampBehavior];
  136. return [dataWriter convertedValue:*data];
  137. }
  138. - (nullable id)valueForField:(id)field {
  139. return [self valueForField:field serverTimestampBehavior:FIRServerTimestampBehaviorNone];
  140. }
  141. - (nullable id)valueForField:(id)field
  142. serverTimestampBehavior:(FIRServerTimestampBehavior)serverTimestampBehavior {
  143. FieldPath fieldPath;
  144. if ([field isKindOfClass:[NSString class]]) {
  145. fieldPath = FieldPath::FromDotSeparatedString(MakeString(field));
  146. } else if ([field isKindOfClass:[FIRFieldPath class]]) {
  147. fieldPath = ((FIRFieldPath *)field).internalValue;
  148. } else {
  149. ThrowInvalidArgument("Subscript key must be an NSString or FIRFieldPath.");
  150. }
  151. absl::optional<google_firestore_v1_Value> fieldValue = _snapshot.GetValue(fieldPath);
  152. if (!fieldValue) return nil;
  153. FSTUserDataWriter *dataWriter =
  154. [[FSTUserDataWriter alloc] initWithFirestore:_snapshot.firestore()
  155. serverTimestampBehavior:serverTimestampBehavior];
  156. return [dataWriter convertedValue:*fieldValue];
  157. }
  158. - (nullable id)objectForKeyedSubscript:(id)key {
  159. return [self valueForField:key];
  160. }
  161. @end
  162. @implementation FIRQueryDocumentSnapshot
  163. - (NSDictionary<NSString *, id> *)data {
  164. NSDictionary<NSString *, id> *data = [super data];
  165. HARD_ASSERT(data, "Document in a QueryDocumentSnapshot should exist");
  166. return data;
  167. }
  168. - (NSDictionary<NSString *, id> *)dataWithServerTimestampBehavior:
  169. (FIRServerTimestampBehavior)serverTimestampBehavior {
  170. NSDictionary<NSString *, id> *data =
  171. [super dataWithServerTimestampBehavior:serverTimestampBehavior];
  172. HARD_ASSERT(data, "Document in a QueryDocumentSnapshot should exist");
  173. return data;
  174. }
  175. @end
  176. NS_ASSUME_NONNULL_END