FIRDocumentSnapshot.mm 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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::remote::Serializer;
  54. using firebase::firestore::nanopb::MakeNSData;
  55. using firebase::firestore::util::MakeNSString;
  56. using firebase::firestore::util::MakeString;
  57. using firebase::firestore::util::ThrowInvalidArgument;
  58. using firebase::firestore::google_firestore_v1_Value;
  59. NS_ASSUME_NONNULL_BEGIN
  60. @implementation FIRDocumentSnapshot {
  61. DocumentSnapshot _snapshot;
  62. std::unique_ptr<Serializer> _serializer;
  63. FIRSnapshotMetadata *_cachedMetadata;
  64. }
  65. - (instancetype)initWithSnapshot:(DocumentSnapshot &&)snapshot {
  66. if (self = [super init]) {
  67. _snapshot = std::move(snapshot);
  68. _serializer.reset(new Serializer(_snapshot.firestore()->database_id()));
  69. }
  70. return self;
  71. }
  72. - (instancetype)initWithFirestore:(FIRFirestore *)firestore
  73. documentKey:(DocumentKey)documentKey
  74. document:(const absl::optional<Document> &)document
  75. metadata:(SnapshotMetadata)metadata {
  76. DocumentSnapshot wrapped;
  77. if (document.has_value()) {
  78. wrapped =
  79. DocumentSnapshot::FromDocument(firestore.wrapped, document.value(), std::move(metadata));
  80. } else {
  81. wrapped = DocumentSnapshot::FromNoDocument(firestore.wrapped, std::move(documentKey),
  82. std::move(metadata));
  83. }
  84. _serializer.reset(new Serializer(firestore.databaseID));
  85. return [self initWithSnapshot:std::move(wrapped)];
  86. }
  87. - (instancetype)initWithFirestore:(FIRFirestore *)firestore
  88. documentKey:(DocumentKey)documentKey
  89. document:(const absl::optional<Document> &)document
  90. fromCache:(bool)fromCache
  91. hasPendingWrites:(bool)hasPendingWrites {
  92. return [self initWithFirestore:firestore
  93. documentKey:std::move(documentKey)
  94. document:document
  95. metadata:SnapshotMetadata(hasPendingWrites, fromCache)];
  96. }
  97. // NSObject Methods
  98. - (BOOL)isEqual:(nullable id)other {
  99. if (other == self) return YES;
  100. // self class could be FIRDocumentSnapshot or subtype. So we compare with base type explicitly.
  101. if (![other isKindOfClass:[FIRDocumentSnapshot class]]) return NO;
  102. return _snapshot == static_cast<FIRDocumentSnapshot *>(other)->_snapshot;
  103. }
  104. - (NSUInteger)hash {
  105. return _snapshot.Hash();
  106. }
  107. @dynamic exists;
  108. - (BOOL)exists {
  109. return _snapshot.exists();
  110. }
  111. - (const absl::optional<Document> &)internalDocument {
  112. return _snapshot.internal_document();
  113. }
  114. - (FIRDocumentReference *)reference {
  115. return [[FIRDocumentReference alloc] initWithReference:_snapshot.CreateReference()];
  116. }
  117. - (NSString *)documentID {
  118. return MakeNSString(_snapshot.document_id());
  119. }
  120. @dynamic metadata;
  121. - (FIRSnapshotMetadata *)metadata {
  122. if (!_cachedMetadata) {
  123. _cachedMetadata = [[FIRSnapshotMetadata alloc] initWithMetadata:_snapshot.metadata()];
  124. }
  125. return _cachedMetadata;
  126. }
  127. - (nullable NSDictionary<NSString *, id> *)data {
  128. return [self dataWithServerTimestampBehavior:FIRServerTimestampBehaviorNone];
  129. }
  130. - (nullable NSDictionary<NSString *, id> *)dataWithServerTimestampBehavior:
  131. (FIRServerTimestampBehavior)serverTimestampBehavior {
  132. absl::optional<google_firestore_v1_Value> data = _snapshot.GetValue(FieldPath::EmptyPath());
  133. if (!data) return nil;
  134. FSTUserDataWriter *dataWriter =
  135. [[FSTUserDataWriter alloc] initWithFirestore:_snapshot.firestore()
  136. serverTimestampBehavior:serverTimestampBehavior];
  137. return [dataWriter convertedValue:*data];
  138. }
  139. - (nullable id)valueForField:(id)field {
  140. return [self valueForField:field serverTimestampBehavior:FIRServerTimestampBehaviorNone];
  141. }
  142. - (nullable id)valueForField:(id)field
  143. serverTimestampBehavior:(FIRServerTimestampBehavior)serverTimestampBehavior {
  144. FieldPath fieldPath;
  145. if ([field isKindOfClass:[NSString class]]) {
  146. fieldPath = FieldPath::FromDotSeparatedString(MakeString(field));
  147. } else if ([field isKindOfClass:[FIRFieldPath class]]) {
  148. fieldPath = ((FIRFieldPath *)field).internalValue;
  149. } else {
  150. ThrowInvalidArgument("Subscript key must be an NSString or FIRFieldPath.");
  151. }
  152. absl::optional<google_firestore_v1_Value> fieldValue = _snapshot.GetValue(fieldPath);
  153. if (!fieldValue) return nil;
  154. FSTUserDataWriter *dataWriter =
  155. [[FSTUserDataWriter alloc] initWithFirestore:_snapshot.firestore()
  156. serverTimestampBehavior:serverTimestampBehavior];
  157. return [dataWriter convertedValue:*fieldValue];
  158. }
  159. - (nullable id)objectForKeyedSubscript:(id)key {
  160. return [self valueForField:key];
  161. }
  162. @end
  163. @implementation FIRQueryDocumentSnapshot
  164. - (NSDictionary<NSString *, id> *)data {
  165. NSDictionary<NSString *, id> *data = [super data];
  166. HARD_ASSERT(data, "Document in a QueryDocumentSnapshot should exist");
  167. return data;
  168. }
  169. - (NSDictionary<NSString *, id> *)dataWithServerTimestampBehavior:
  170. (FIRServerTimestampBehavior)serverTimestampBehavior {
  171. NSDictionary<NSString *, id> *data =
  172. [super dataWithServerTimestampBehavior:serverTimestampBehavior];
  173. HARD_ASSERT(data, "Document in a QueryDocumentSnapshot should exist");
  174. return data;
  175. }
  176. @end
  177. NS_ASSUME_NONNULL_END