FIRCollectionReference.mm 4.6 KB

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