FIRCollectionReference.mm 4.5 KB

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