FIRDocumentSnapshot.mm 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #import "Firestore/Source/API/FIRDocumentReference+Internal.h"
  18. #import "Firestore/Source/API/FIRFieldPath+Internal.h"
  19. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  20. #import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h"
  21. #import "Firestore/Source/API/FIRSnapshotOptions+Internal.h"
  22. #import "Firestore/Source/Model/FSTDocument.h"
  23. #import "Firestore/Source/Model/FSTDocumentKey.h"
  24. #import "Firestore/Source/Model/FSTFieldValue.h"
  25. #import "Firestore/Source/Model/FSTPath.h"
  26. #import "Firestore/Source/Util/FSTAssert.h"
  27. #import "Firestore/Source/Util/FSTUsageValidation.h"
  28. #include "Firestore/core/src/firebase/firestore/model/database_id.h"
  29. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  30. namespace util = firebase::firestore::util;
  31. using firebase::firestore::model::DatabaseId;
  32. NS_ASSUME_NONNULL_BEGIN
  33. @interface FIRDocumentSnapshot ()
  34. - (instancetype)initWithFirestore:(FIRFirestore *)firestore
  35. documentKey:(FSTDocumentKey *)documentKey
  36. document:(nullable FSTDocument *)document
  37. fromCache:(BOOL)fromCache NS_DESIGNATED_INITIALIZER;
  38. @property(nonatomic, strong, readonly) FIRFirestore *firestore;
  39. @property(nonatomic, strong, readonly) FSTDocumentKey *internalKey;
  40. @property(nonatomic, strong, readonly, nullable) FSTDocument *internalDocument;
  41. @property(nonatomic, assign, readonly) BOOL fromCache;
  42. @end
  43. @implementation FIRDocumentSnapshot (Internal)
  44. + (instancetype)snapshotWithFirestore:(FIRFirestore *)firestore
  45. documentKey:(FSTDocumentKey *)documentKey
  46. document:(nullable FSTDocument *)document
  47. fromCache:(BOOL)fromCache {
  48. return [[[self class] alloc] initWithFirestore:firestore
  49. documentKey:documentKey
  50. document:document
  51. fromCache:fromCache];
  52. }
  53. @end
  54. @implementation FIRDocumentSnapshot {
  55. FIRSnapshotMetadata *_cachedMetadata;
  56. }
  57. @dynamic metadata;
  58. - (instancetype)initWithFirestore:(FIRFirestore *)firestore
  59. documentKey:(FSTDocumentKey *)documentKey
  60. document:(nullable FSTDocument *)document
  61. fromCache:(BOOL)fromCache {
  62. if (self = [super init]) {
  63. _firestore = firestore;
  64. _internalKey = documentKey;
  65. _internalDocument = document;
  66. _fromCache = fromCache;
  67. }
  68. return self;
  69. }
  70. // NSObject Methods
  71. - (BOOL)isEqual:(nullable id)other {
  72. if (other == self) return YES;
  73. // self class could be FIRDocumentSnapshot or subtype. So we compare with base type explicitly.
  74. if (![other isKindOfClass:[FIRDocumentSnapshot class]]) return NO;
  75. return [self isEqualToSnapshot:other];
  76. }
  77. - (BOOL)isEqualToSnapshot:(nullable FIRDocumentSnapshot *)snapshot {
  78. if (self == snapshot) return YES;
  79. if (snapshot == nil) return NO;
  80. return [self.firestore isEqual:snapshot.firestore] &&
  81. [self.internalKey isEqual:snapshot.internalKey] &&
  82. (self.internalDocument == snapshot.internalDocument ||
  83. [self.internalDocument isEqual:snapshot.internalDocument]) &&
  84. self.fromCache == snapshot.fromCache;
  85. }
  86. - (NSUInteger)hash {
  87. NSUInteger hash = [self.firestore hash];
  88. hash = hash * 31u + [self.internalKey hash];
  89. hash = hash * 31u + [self.internalDocument hash];
  90. hash = hash * 31u + (self.fromCache ? 1 : 0);
  91. return hash;
  92. }
  93. @dynamic exists;
  94. - (BOOL)exists {
  95. return _internalDocument != nil;
  96. }
  97. - (FIRDocumentReference *)reference {
  98. return [FIRDocumentReference referenceWithKey:self.internalKey firestore:self.firestore];
  99. }
  100. - (NSString *)documentID {
  101. return [self.internalKey.path lastSegment];
  102. }
  103. - (FIRSnapshotMetadata *)metadata {
  104. if (!_cachedMetadata) {
  105. _cachedMetadata = [FIRSnapshotMetadata
  106. snapshotMetadataWithPendingWrites:self.internalDocument.hasLocalMutations
  107. fromCache:self.fromCache];
  108. }
  109. return _cachedMetadata;
  110. }
  111. - (nullable NSDictionary<NSString *, id> *)data {
  112. return [self dataWithOptions:[FIRSnapshotOptions defaultOptions]];
  113. }
  114. - (nullable NSDictionary<NSString *, id> *)dataWithOptions:(FIRSnapshotOptions *)options {
  115. return self.internalDocument == nil
  116. ? nil
  117. : [self convertedObject:[self.internalDocument data]
  118. options:[FSTFieldValueOptions optionsForSnapshotOptions:options]];
  119. }
  120. - (nullable id)valueForField:(id)field {
  121. return [self valueForField:field options:[FIRSnapshotOptions defaultOptions]];
  122. }
  123. - (nullable id)valueForField:(id)field options:(FIRSnapshotOptions *)options {
  124. FIRFieldPath *fieldPath;
  125. if ([field isKindOfClass:[NSString class]]) {
  126. fieldPath = [FIRFieldPath pathWithDotSeparatedString:field];
  127. } else if ([field isKindOfClass:[FIRFieldPath class]]) {
  128. fieldPath = field;
  129. } else {
  130. FSTThrowInvalidArgument(@"Subscript key must be an NSString or FIRFieldPath.");
  131. }
  132. FSTFieldValue *fieldValue = [[self.internalDocument data] valueForPath:fieldPath.internalValue];
  133. return fieldValue == nil
  134. ? nil
  135. : [self convertedValue:fieldValue
  136. options:[FSTFieldValueOptions optionsForSnapshotOptions:options]];
  137. }
  138. - (nullable id)objectForKeyedSubscript:(id)key {
  139. return [self valueForField:key];
  140. }
  141. - (id)convertedValue:(FSTFieldValue *)value options:(FSTFieldValueOptions *)options {
  142. if ([value isKindOfClass:[FSTObjectValue class]]) {
  143. return [self convertedObject:(FSTObjectValue *)value options:options];
  144. } else if ([value isKindOfClass:[FSTArrayValue class]]) {
  145. return [self convertedArray:(FSTArrayValue *)value options:options];
  146. } else if ([value isKindOfClass:[FSTReferenceValue class]]) {
  147. FSTReferenceValue *ref = (FSTReferenceValue *)value;
  148. const DatabaseId *refDatabase = ref.databaseID;
  149. const DatabaseId *database = self.firestore.databaseID;
  150. if (*refDatabase != *database) {
  151. // TODO(b/32073923): Log this as a proper warning.
  152. NSLog(
  153. @"WARNING: Document %@ contains a document reference within a different database "
  154. "(%@/%@) which is not supported. It will be treated as a reference within the "
  155. "current database (%@/%@) instead.",
  156. self.reference.path, util::WrapNSStringNoCopy(refDatabase->project_id()),
  157. util::WrapNSStringNoCopy(refDatabase->database_id()),
  158. util::WrapNSStringNoCopy(database->project_id()),
  159. util::WrapNSStringNoCopy(database->database_id()));
  160. }
  161. return [FIRDocumentReference referenceWithKey:[ref valueWithOptions:options]
  162. firestore:self.firestore];
  163. } else {
  164. return [value valueWithOptions:options];
  165. }
  166. }
  167. - (NSDictionary<NSString *, id> *)convertedObject:(FSTObjectValue *)objectValue
  168. options:(FSTFieldValueOptions *)options {
  169. NSMutableDictionary *result = [NSMutableDictionary dictionary];
  170. [objectValue.internalValue
  171. enumerateKeysAndObjectsUsingBlock:^(NSString *key, FSTFieldValue *value, BOOL *stop) {
  172. result[key] = [self convertedValue:value options:options];
  173. }];
  174. return result;
  175. }
  176. - (NSArray<id> *)convertedArray:(FSTArrayValue *)arrayValue
  177. options:(FSTFieldValueOptions *)options {
  178. NSArray<FSTFieldValue *> *internalValue = arrayValue.internalValue;
  179. NSMutableArray *result = [NSMutableArray arrayWithCapacity:internalValue.count];
  180. [internalValue enumerateObjectsUsingBlock:^(id value, NSUInteger idx, BOOL *stop) {
  181. [result addObject:[self convertedValue:value options:options]];
  182. }];
  183. return result;
  184. }
  185. @end
  186. @interface FIRQueryDocumentSnapshot ()
  187. - (instancetype)initWithFirestore:(FIRFirestore *)firestore
  188. documentKey:(FSTDocumentKey *)documentKey
  189. document:(FSTDocument *)document
  190. fromCache:(BOOL)fromCache NS_DESIGNATED_INITIALIZER;
  191. @end
  192. @implementation FIRQueryDocumentSnapshot
  193. - (instancetype)initWithFirestore:(FIRFirestore *)firestore
  194. documentKey:(FSTDocumentKey *)documentKey
  195. document:(FSTDocument *)document
  196. fromCache:(BOOL)fromCache {
  197. self = [super initWithFirestore:firestore
  198. documentKey:documentKey
  199. document:document
  200. fromCache:fromCache];
  201. return self;
  202. }
  203. - (NSDictionary<NSString *, id> *)data {
  204. NSDictionary<NSString *, id> *data = [super data];
  205. FSTAssert(data, @"Document in a QueryDocumentSnapshot should exist");
  206. return data;
  207. }
  208. - (NSDictionary<NSString *, id> *)dataWithOptions:(FIRSnapshotOptions *)options {
  209. NSDictionary<NSString *, id> *data = [super dataWithOptions:options];
  210. FSTAssert(data, @"Document in a QueryDocumentSnapshot should exist");
  211. return data;
  212. }
  213. @end
  214. NS_ASSUME_NONNULL_END