FIRDocumentSnapshot.mm 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright 2017 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 "FIRDocumentSnapshot.h"
  17. #include <utility>
  18. #include "Firestore/core/src/firebase/firestore/util/warnings.h"
  19. #import "FIRFirestore.h"
  20. #import "FIRFirestoreSettings.h"
  21. #import "Firestore/Source/API/FIRDocumentReference+Internal.h"
  22. #import "Firestore/Source/API/FIRFieldPath+Internal.h"
  23. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  24. #import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h"
  25. #import "Firestore/Source/Model/FSTDocument.h"
  26. #import "Firestore/Source/Model/FSTFieldValue.h"
  27. #import "Firestore/Source/Util/FSTUsageValidation.h"
  28. #include "Firestore/core/src/firebase/firestore/api/document_snapshot.h"
  29. #include "Firestore/core/src/firebase/firestore/model/database_id.h"
  30. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  31. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  32. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  33. namespace util = firebase::firestore::util;
  34. using firebase::firestore::api::DocumentSnapshot;
  35. using firebase::firestore::model::DatabaseId;
  36. using firebase::firestore::model::DocumentKey;
  37. using firebase::firestore::util::WrapNSString;
  38. NS_ASSUME_NONNULL_BEGIN
  39. namespace {
  40. /**
  41. * Converts a public FIRServerTimestampBehavior into its internal equivalent.
  42. */
  43. ServerTimestampBehavior InternalServerTimestampBehavior(FIRServerTimestampBehavior behavior) {
  44. switch (behavior) {
  45. case FIRServerTimestampBehaviorNone:
  46. return ServerTimestampBehavior::None;
  47. case FIRServerTimestampBehaviorEstimate:
  48. return ServerTimestampBehavior::Estimate;
  49. case FIRServerTimestampBehaviorPrevious:
  50. return ServerTimestampBehavior::Previous;
  51. default:
  52. HARD_FAIL("Unexpected server timestamp option: %s", behavior);
  53. }
  54. }
  55. } // namespace
  56. @interface FIRDocumentSnapshot ()
  57. - (instancetype)initWithSnapshot:(DocumentSnapshot &&)snapshot;
  58. @end
  59. @implementation FIRDocumentSnapshot (Internal)
  60. + (instancetype)snapshotWithFirestore:(FIRFirestore *)firestore
  61. documentKey:(DocumentKey)documentKey
  62. document:(nullable FSTDocument *)document
  63. fromCache:(BOOL)fromCache
  64. hasPendingWrites:(BOOL)pendingWrites {
  65. DocumentSnapshot underlyingSnapshot{firestore, documentKey, document,
  66. static_cast<bool>(fromCache),
  67. static_cast<bool>(pendingWrites)};
  68. return [[[self class] alloc] initWithSnapshot:std::move(underlyingSnapshot)];
  69. }
  70. @end
  71. @implementation FIRDocumentSnapshot {
  72. DocumentSnapshot _snapshot;
  73. }
  74. - (instancetype)initWithSnapshot:(DocumentSnapshot &&)snapshot {
  75. if (self = [super init]) {
  76. _snapshot = std::move(snapshot);
  77. }
  78. return self;
  79. }
  80. // NSObject Methods
  81. - (BOOL)isEqual:(nullable id)other {
  82. if (other == self) return YES;
  83. // self class could be FIRDocumentSnapshot or subtype. So we compare with base type explicitly.
  84. if (![other isKindOfClass:[FIRDocumentSnapshot class]]) return NO;
  85. return _snapshot == static_cast<FIRDocumentSnapshot *>(other)->_snapshot;
  86. }
  87. - (NSUInteger)hash {
  88. return _snapshot.Hash();
  89. }
  90. @dynamic exists;
  91. - (BOOL)exists {
  92. return _snapshot.exists();
  93. }
  94. - (FSTDocument *)internalDocument {
  95. return _snapshot.internal_document();
  96. }
  97. - (FIRDocumentReference *)reference {
  98. return _snapshot.CreateReference();
  99. }
  100. - (NSString *)documentID {
  101. return WrapNSString(_snapshot.document_id());
  102. }
  103. @dynamic metadata;
  104. - (FIRSnapshotMetadata *)metadata {
  105. return _snapshot.GetMetadata();
  106. }
  107. - (nullable NSDictionary<NSString *, id> *)data {
  108. return [self dataWithServerTimestampBehavior:FIRServerTimestampBehaviorNone];
  109. }
  110. - (nullable NSDictionary<NSString *, id> *)dataWithServerTimestampBehavior:
  111. (FIRServerTimestampBehavior)serverTimestampBehavior {
  112. FSTFieldValueOptions *options = [self optionsForServerTimestampBehavior:serverTimestampBehavior];
  113. FSTObjectValue *data = _snapshot.GetData();
  114. return data == nil ? nil : [self convertedObject:data options:options];
  115. }
  116. - (nullable id)valueForField:(id)field {
  117. return [self valueForField:field serverTimestampBehavior:FIRServerTimestampBehaviorNone];
  118. }
  119. - (nullable id)valueForField:(id)field
  120. serverTimestampBehavior:(FIRServerTimestampBehavior)serverTimestampBehavior {
  121. FIRFieldPath *fieldPath;
  122. if ([field isKindOfClass:[NSString class]]) {
  123. fieldPath = [FIRFieldPath pathWithDotSeparatedString:field];
  124. } else if ([field isKindOfClass:[FIRFieldPath class]]) {
  125. fieldPath = field;
  126. } else {
  127. FSTThrowInvalidArgument(@"Subscript key must be an NSString or FIRFieldPath.");
  128. }
  129. FSTFieldValue *fieldValue = _snapshot.GetValue(fieldPath.internalValue);
  130. FSTFieldValueOptions *options = [self optionsForServerTimestampBehavior:serverTimestampBehavior];
  131. return fieldValue == nil ? nil : [self convertedValue:fieldValue options:options];
  132. }
  133. - (nullable id)objectForKeyedSubscript:(id)key {
  134. return [self valueForField:key];
  135. }
  136. - (FSTFieldValueOptions *)optionsForServerTimestampBehavior:
  137. (FIRServerTimestampBehavior)serverTimestampBehavior {
  138. SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN()
  139. return [[FSTFieldValueOptions alloc]
  140. initWithServerTimestampBehavior:InternalServerTimestampBehavior(serverTimestampBehavior)
  141. timestampsInSnapshotsEnabled:_snapshot.firestore().settings.timestampsInSnapshotsEnabled];
  142. SUPPRESS_END()
  143. }
  144. - (id)convertedValue:(FSTFieldValue *)value options:(FSTFieldValueOptions *)options {
  145. if ([value isKindOfClass:[FSTObjectValue class]]) {
  146. return [self convertedObject:(FSTObjectValue *)value options:options];
  147. } else if ([value isKindOfClass:[FSTArrayValue class]]) {
  148. return [self convertedArray:(FSTArrayValue *)value options:options];
  149. } else if ([value isKindOfClass:[FSTReferenceValue class]]) {
  150. FSTReferenceValue *ref = (FSTReferenceValue *)value;
  151. const DatabaseId *refDatabase = ref.databaseID;
  152. const DatabaseId *database = _snapshot.firestore().databaseID;
  153. if (*refDatabase != *database) {
  154. // TODO(b/32073923): Log this as a proper warning.
  155. NSLog(@"WARNING: Document %@ contains a document reference within a different database "
  156. "(%s/%s) which is not supported. It will be treated as a reference within the "
  157. "current database (%s/%s) instead.",
  158. self.reference.path, refDatabase->project_id().c_str(),
  159. refDatabase->database_id().c_str(), database->project_id().c_str(),
  160. database->database_id().c_str());
  161. }
  162. DocumentKey key = [[ref valueWithOptions:options] key];
  163. return [FIRDocumentReference referenceWithKey:key firestore:_snapshot.firestore()];
  164. } else {
  165. return [value valueWithOptions:options];
  166. }
  167. }
  168. - (NSDictionary<NSString *, id> *)convertedObject:(FSTObjectValue *)objectValue
  169. options:(FSTFieldValueOptions *)options {
  170. NSMutableDictionary *result = [NSMutableDictionary dictionary];
  171. [objectValue.internalValue
  172. enumerateKeysAndObjectsUsingBlock:^(NSString *key, FSTFieldValue *value, BOOL *stop) {
  173. result[key] = [self convertedValue:value options:options];
  174. }];
  175. return result;
  176. }
  177. - (NSArray<id> *)convertedArray:(FSTArrayValue *)arrayValue
  178. options:(FSTFieldValueOptions *)options {
  179. NSArray<FSTFieldValue *> *internalValue = arrayValue.internalValue;
  180. NSMutableArray *result = [NSMutableArray arrayWithCapacity:internalValue.count];
  181. [internalValue enumerateObjectsUsingBlock:^(id value, NSUInteger idx, BOOL *stop) {
  182. [result addObject:[self convertedValue:value options:options]];
  183. }];
  184. return result;
  185. }
  186. @end
  187. @interface FIRQueryDocumentSnapshot ()
  188. - (instancetype)initWithSnapshot:(DocumentSnapshot &&)snapshot NS_DESIGNATED_INITIALIZER;
  189. @end
  190. @implementation FIRQueryDocumentSnapshot
  191. - (instancetype)initWithSnapshot:(DocumentSnapshot &&)snapshot {
  192. return [super initWithSnapshot:std::move(snapshot)];
  193. }
  194. - (NSDictionary<NSString *, id> *)data {
  195. NSDictionary<NSString *, id> *data = [super data];
  196. HARD_ASSERT(data, "Document in a QueryDocumentSnapshot should exist");
  197. return data;
  198. }
  199. - (NSDictionary<NSString *, id> *)dataWithServerTimestampBehavior:
  200. (FIRServerTimestampBehavior)serverTimestampBehavior {
  201. NSDictionary<NSString *, id> *data =
  202. [super dataWithServerTimestampBehavior:serverTimestampBehavior];
  203. HARD_ASSERT(data, "Document in a QueryDocumentSnapshot should exist");
  204. return data;
  205. }
  206. @end
  207. NS_ASSUME_NONNULL_END