FIRCollectionReference.mm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #import "Firestore/Source/API/FIRDocumentReference+Internal.h"
  19. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  20. #import "Firestore/Source/API/FIRQuery+Internal.h"
  21. #import "Firestore/Source/API/FSTUserDataReader.h"
  22. #include "Firestore/core/src/api/collection_reference.h"
  23. #include "Firestore/core/src/api/document_reference.h"
  24. #include "Firestore/core/src/core/user_data.h"
  25. #include "Firestore/core/src/model/resource_path.h"
  26. #include "Firestore/core/src/util/error_apple.h"
  27. #include "Firestore/core/src/util/exception.h"
  28. #include "Firestore/core/src/util/string_apple.h"
  29. using firebase::firestore::api::CollectionReference;
  30. using firebase::firestore::api::DocumentReference;
  31. using firebase::firestore::core::ParsedSetData;
  32. using firebase::firestore::model::ResourcePath;
  33. using firebase::firestore::util::MakeCallback;
  34. using firebase::firestore::util::MakeNSString;
  35. using firebase::firestore::util::MakeString;
  36. using firebase::firestore::util::ThrowInvalidArgument;
  37. NS_ASSUME_NONNULL_BEGIN
  38. @implementation FIRCollectionReference
  39. - (instancetype)initWithReference:(CollectionReference &&)reference {
  40. return [super initWithQuery:std::move(reference)];
  41. }
  42. - (instancetype)initWithPath:(ResourcePath)path
  43. firestore:(std::shared_ptr<api::Firestore>)firestore {
  44. CollectionReference ref(std::move(path), std::move(firestore));
  45. return [self initWithReference:std::move(ref)];
  46. }
  47. // Override the designated initializer from the super class.
  48. - (instancetype)initWithQuery:(__unused api::Query &&)query {
  49. HARD_FAIL("Use FIRCollectionReference initWithPath: initializer.");
  50. }
  51. // NSObject Methods
  52. - (BOOL)isEqual:(nullable id)other {
  53. if (other == self) return YES;
  54. if (![[other class] isEqual:[self class]]) return NO;
  55. return [self isEqualToReference:other];
  56. }
  57. - (BOOL)isEqualToReference:(nullable FIRCollectionReference *)otherReference {
  58. if (self == otherReference) return YES;
  59. if (otherReference == nil) return NO;
  60. return self.reference == otherReference.reference;
  61. }
  62. - (NSUInteger)hash {
  63. return self.reference.Hash();
  64. }
  65. - (const CollectionReference &)reference {
  66. // TODO(wilhuff): Use some alternate method for doing this.
  67. //
  68. // Casting from Query& to CollectionReference& when the value is actually a
  69. // Query violates aliasing rules and is technically undefined behavior.
  70. // Nevertheless this works on Clang so this is good enough for now.
  71. return static_cast<const CollectionReference &>(self.apiQuery);
  72. }
  73. - (NSString *)collectionID {
  74. return MakeNSString(self.reference.collection_id());
  75. }
  76. - (FIRDocumentReference *_Nullable)parent {
  77. absl::optional<DocumentReference> parent = self.reference.parent();
  78. if (!parent) {
  79. return nil;
  80. }
  81. return [[FIRDocumentReference alloc] initWithReference:std::move(*parent)];
  82. }
  83. - (NSString *)path {
  84. return MakeNSString(self.reference.path());
  85. }
  86. - (FIRDocumentReference *)documentWithPath:(NSString *)documentPath {
  87. if (!documentPath) {
  88. ThrowInvalidArgument("Document path cannot be nil.");
  89. }
  90. if (!documentPath.length) {
  91. ThrowInvalidArgument("Document path cannot be empty.");
  92. }
  93. DocumentReference child = self.reference.Document(MakeString(documentPath));
  94. return [[FIRDocumentReference alloc] initWithReference:std::move(child)];
  95. }
  96. - (FIRDocumentReference *)addDocumentWithData:(NSDictionary<NSString *, id> *)data {
  97. return [self addDocumentWithData:data completion:nil];
  98. }
  99. - (FIRDocumentReference *)addDocumentWithData:(NSDictionary<NSString *, id> *)data
  100. completion:
  101. (nullable void (^)(NSError *_Nullable error))completion {
  102. ParsedSetData parsed = [self.firestore.dataReader parsedSetData:data];
  103. DocumentReference docRef =
  104. self.reference.AddDocument(std::move(parsed), MakeCallback(completion));
  105. return [[FIRDocumentReference alloc] initWithReference:std::move(docRef)];
  106. }
  107. - (FIRDocumentReference *)documentWithAutoID {
  108. return [[FIRDocumentReference alloc] initWithReference:self.reference.Document()];
  109. }
  110. @end
  111. NS_ASSUME_NONNULL_END