FIRCollectionReference.mm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "FIRCollectionReference.h"
  17. #include <utility>
  18. #include "Firestore/core/src/firebase/firestore/util/autoid.h"
  19. #import "Firestore/Source/API/FIRDocumentReference+Internal.h"
  20. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  21. #import "Firestore/Source/API/FIRQuery+Internal.h"
  22. #import "Firestore/Source/API/FIRQuery_Init.h"
  23. #import "Firestore/Source/Core/FSTQuery.h"
  24. #include "Firestore/core/src/firebase/firestore/api/input_validation.h"
  25. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  26. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  27. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  28. namespace util = firebase::firestore::util;
  29. using firebase::firestore::api::ThrowInvalidArgument;
  30. using firebase::firestore::model::DocumentKey;
  31. using firebase::firestore::model::ResourcePath;
  32. using firebase::firestore::util::CreateAutoId;
  33. NS_ASSUME_NONNULL_BEGIN
  34. @interface FIRCollectionReference ()
  35. - (instancetype)initWithPath:(const ResourcePath &)path
  36. firestore:(FIRFirestore *)firestore NS_DESIGNATED_INITIALIZER;
  37. // Mark the super class designated initializer unavailable.
  38. - (instancetype)initWithQuery:(FSTQuery *)query
  39. firestore:(FIRFirestore *)firestore
  40. __attribute__((unavailable("Use the initWithPath constructor of FIRCollectionReference.")));
  41. @end
  42. @implementation FIRCollectionReference (Internal)
  43. + (instancetype)referenceWithPath:(const ResourcePath &)path firestore:(FIRFirestore *)firestore {
  44. return [[FIRCollectionReference alloc] initWithPath:path firestore:firestore];
  45. }
  46. @end
  47. @implementation FIRCollectionReference
  48. - (instancetype)initWithPath:(const ResourcePath &)path firestore:(FIRFirestore *)firestore {
  49. if (path.size() % 2 != 1) {
  50. ThrowInvalidArgument("Invalid collection reference. Collection references must have an odd "
  51. "number of segments, but %s has %s",
  52. path.CanonicalString(), path.size());
  53. }
  54. self = [super initWithQuery:[FSTQuery queryWithPath:path] firestore:firestore];
  55. return self;
  56. }
  57. // Override the designated initializer from the super class.
  58. - (instancetype)initWithQuery:(FSTQuery *)query firestore:(FIRFirestore *)firestore {
  59. HARD_FAIL("Use FIRCollectionReference initWithPath: initializer.");
  60. }
  61. // NSObject Methods
  62. - (BOOL)isEqual:(nullable id)other {
  63. if (other == self) return YES;
  64. if (![[other class] isEqual:[self class]]) return NO;
  65. return [self isEqualToReference:other];
  66. }
  67. - (BOOL)isEqualToReference:(nullable FIRCollectionReference *)reference {
  68. if (self == reference) return YES;
  69. if (reference == nil) return NO;
  70. return [self.firestore isEqual:reference.firestore] && [self.query isEqual:reference.query];
  71. }
  72. - (NSUInteger)hash {
  73. NSUInteger hash = [self.firestore hash];
  74. hash = hash * 31u + [self.query hash];
  75. return hash;
  76. }
  77. - (NSString *)collectionID {
  78. return util::WrapNSString(self.query.path.last_segment());
  79. }
  80. - (FIRDocumentReference *_Nullable)parent {
  81. const ResourcePath parentPath = self.query.path.PopLast();
  82. if (parentPath.empty()) {
  83. return nil;
  84. } else {
  85. DocumentKey key{parentPath};
  86. return [[FIRDocumentReference alloc] initWithKey:std::move(key)
  87. firestore:self.firestore.wrapped];
  88. }
  89. }
  90. - (NSString *)path {
  91. return util::WrapNSString(self.query.path.CanonicalString());
  92. }
  93. - (FIRDocumentReference *)documentWithPath:(NSString *)documentPath {
  94. if (!documentPath) {
  95. ThrowInvalidArgument("Document path cannot be nil.");
  96. }
  97. const ResourcePath subPath = ResourcePath::FromString(util::MakeString(documentPath));
  98. ResourcePath path = self.query.path.Append(subPath);
  99. return [[FIRDocumentReference alloc] initWithPath:std::move(path)
  100. firestore:self.firestore.wrapped];
  101. }
  102. - (FIRDocumentReference *)addDocumentWithData:(NSDictionary<NSString *, id> *)data {
  103. return [self addDocumentWithData:data completion:nil];
  104. }
  105. - (FIRDocumentReference *)addDocumentWithData:(NSDictionary<NSString *, id> *)data
  106. completion:
  107. (nullable void (^)(NSError *_Nullable error))completion {
  108. FIRDocumentReference *docRef = [self documentWithAutoID];
  109. [docRef setData:data completion:completion];
  110. return docRef;
  111. }
  112. - (FIRDocumentReference *)documentWithAutoID {
  113. DocumentKey key{self.query.path.Append(CreateAutoId())};
  114. return [[FIRDocumentReference alloc] initWithKey:std::move(key) firestore:self.firestore.wrapped];
  115. }
  116. @end
  117. NS_ASSUME_NONNULL_END