FIRCollectionReference.mm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. HARD_FAIL("Use FIRCollectionReference initWithPath: initializer.");
  48. }
  49. // NSObject Methods
  50. - (BOOL)isEqual:(nullable id)other {
  51. if (other == self) return YES;
  52. if (![[other class] isEqual:[self class]]) return NO;
  53. return [self isEqualToReference:other];
  54. }
  55. - (BOOL)isEqualToReference:(nullable FIRCollectionReference *)otherReference {
  56. if (self == otherReference) return YES;
  57. if (otherReference == nil) return NO;
  58. return self.reference == otherReference.reference;
  59. }
  60. - (NSUInteger)hash {
  61. return self.reference.Hash();
  62. }
  63. - (const CollectionReference &)reference {
  64. // TODO(wilhuff): Use some alternate method for doing this.
  65. //
  66. // Casting from Query& to CollectionReference& when the value is actually a
  67. // Query violates aliasing rules and is technically undefined behavior.
  68. // Nevertheless this works on Clang so this is good enough for now.
  69. return static_cast<const CollectionReference &>(self.apiQuery);
  70. }
  71. - (NSString *)collectionID {
  72. return util::MakeNSString(self.reference.collection_id());
  73. }
  74. - (FIRDocumentReference *_Nullable)parent {
  75. absl::optional<DocumentReference> parent = self.reference.parent();
  76. if (!parent) {
  77. return nil;
  78. }
  79. return [[FIRDocumentReference alloc] initWithReference:std::move(*parent)];
  80. }
  81. - (NSString *)path {
  82. return util::MakeNSString(self.reference.path());
  83. }
  84. - (FIRDocumentReference *)documentWithPath:(NSString *)documentPath {
  85. if (!documentPath) {
  86. ThrowInvalidArgument("Document path cannot be nil.");
  87. }
  88. DocumentReference child = self.reference.Document(util::MakeString(documentPath));
  89. return [[FIRDocumentReference alloc] initWithReference:std::move(child)];
  90. }
  91. - (FIRDocumentReference *)addDocumentWithData:(NSDictionary<NSString *, id> *)data {
  92. return [self addDocumentWithData:data completion:nil];
  93. }
  94. - (FIRDocumentReference *)addDocumentWithData:(NSDictionary<NSString *, id> *)data
  95. completion:
  96. (nullable void (^)(NSError *_Nullable error))completion {
  97. ParsedSetData parsed = [self.firestore.dataConverter parsedSetData:data];
  98. DocumentReference docRef =
  99. self.reference.AddDocument(std::move(parsed), util::MakeCallback(completion));
  100. return [[FIRDocumentReference alloc] initWithReference:std::move(docRef)];
  101. }
  102. - (FIRDocumentReference *)documentWithAutoID {
  103. return [[FIRDocumentReference alloc] initWithReference:self.reference.Document()];
  104. }
  105. @end
  106. NS_ASSUME_NONNULL_END