FIRDocumentSnapshot.mm 9.2 KB

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