2017-10-03 08:55:22 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright 2017 Google
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#import "FIRCollectionReference.h"
|
2019-03-15 19:10:32 -04:00
|
|
|
|
|
|
|
|
#include <utility>
|
2017-10-03 08:55:22 -07:00
|
|
|
|
2017-11-16 20:52:01 -08:00
|
|
|
#import "Firestore/Source/API/FIRDocumentReference+Internal.h"
|
2019-03-15 19:10:32 -04:00
|
|
|
#import "Firestore/Source/API/FIRFirestore+Internal.h"
|
2017-11-16 20:52:01 -08:00
|
|
|
#import "Firestore/Source/API/FIRQuery+Internal.h"
|
2021-07-22 12:44:17 -06:00
|
|
|
#import "Firestore/Source/API/FSTUserDataReader.h"
|
2017-10-03 08:55:22 -07:00
|
|
|
|
2020-04-20 14:57:34 -07:00
|
|
|
#include "Firestore/core/src/api/collection_reference.h"
|
|
|
|
|
#include "Firestore/core/src/api/document_reference.h"
|
|
|
|
|
#include "Firestore/core/src/core/user_data.h"
|
|
|
|
|
#include "Firestore/core/src/model/resource_path.h"
|
|
|
|
|
#include "Firestore/core/src/util/error_apple.h"
|
|
|
|
|
#include "Firestore/core/src/util/exception.h"
|
|
|
|
|
#include "Firestore/core/src/util/string_apple.h"
|
2018-03-05 11:30:59 -05:00
|
|
|
|
2019-06-04 18:10:17 -07:00
|
|
|
using firebase::firestore::api::CollectionReference;
|
|
|
|
|
using firebase::firestore::api::DocumentReference;
|
|
|
|
|
using firebase::firestore::core::ParsedSetData;
|
2018-03-05 11:30:59 -05:00
|
|
|
using firebase::firestore::model::ResourcePath;
|
2021-09-24 11:11:41 -04:00
|
|
|
using firebase::firestore::util::MakeCallback;
|
|
|
|
|
using firebase::firestore::util::MakeNSString;
|
|
|
|
|
using firebase::firestore::util::MakeString;
|
2019-10-18 12:58:45 -07:00
|
|
|
using firebase::firestore::util::ThrowInvalidArgument;
|
2017-11-30 12:26:16 -08:00
|
|
|
|
2017-10-03 08:55:22 -07:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
|
|
@implementation FIRCollectionReference
|
|
|
|
|
|
2019-06-04 18:10:17 -07:00
|
|
|
- (instancetype)initWithReference:(CollectionReference &&)reference {
|
|
|
|
|
return [super initWithQuery:std::move(reference)];
|
|
|
|
|
}
|
2019-05-14 14:04:35 -07:00
|
|
|
|
2019-06-04 18:10:17 -07:00
|
|
|
- (instancetype)initWithPath:(ResourcePath)path
|
|
|
|
|
firestore:(std::shared_ptr<api::Firestore>)firestore {
|
|
|
|
|
CollectionReference ref(std::move(path), std::move(firestore));
|
|
|
|
|
return [self initWithReference:std::move(ref)];
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Override the designated initializer from the super class.
|
2020-04-20 14:23:36 -07:00
|
|
|
- (instancetype)initWithQuery:(__unused api::Query &&)query {
|
2018-05-24 11:00:23 -07:00
|
|
|
HARD_FAIL("Use FIRCollectionReference initWithPath: initializer.");
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
2017-12-19 13:47:04 -05:00
|
|
|
// NSObject Methods
|
|
|
|
|
- (BOOL)isEqual:(nullable id)other {
|
|
|
|
|
if (other == self) return YES;
|
|
|
|
|
if (![[other class] isEqual:[self class]]) return NO;
|
|
|
|
|
|
|
|
|
|
return [self isEqualToReference:other];
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 18:10:17 -07:00
|
|
|
- (BOOL)isEqualToReference:(nullable FIRCollectionReference *)otherReference {
|
|
|
|
|
if (self == otherReference) return YES;
|
|
|
|
|
if (otherReference == nil) return NO;
|
|
|
|
|
return self.reference == otherReference.reference;
|
2017-12-19 13:47:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSUInteger)hash {
|
2019-06-04 18:10:17 -07:00
|
|
|
return self.reference.Hash();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const CollectionReference &)reference {
|
|
|
|
|
// TODO(wilhuff): Use some alternate method for doing this.
|
|
|
|
|
//
|
|
|
|
|
// Casting from Query& to CollectionReference& when the value is actually a
|
|
|
|
|
// Query violates aliasing rules and is technically undefined behavior.
|
|
|
|
|
// Nevertheless this works on Clang so this is good enough for now.
|
|
|
|
|
return static_cast<const CollectionReference &>(self.apiQuery);
|
2017-12-19 13:47:04 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-03 08:55:22 -07:00
|
|
|
- (NSString *)collectionID {
|
2021-09-24 11:11:41 -04:00
|
|
|
return MakeNSString(self.reference.collection_id());
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (FIRDocumentReference *_Nullable)parent {
|
2019-06-04 18:10:17 -07:00
|
|
|
absl::optional<DocumentReference> parent = self.reference.parent();
|
|
|
|
|
if (!parent) {
|
2017-10-03 08:55:22 -07:00
|
|
|
return nil;
|
|
|
|
|
}
|
2019-06-04 18:10:17 -07:00
|
|
|
return [[FIRDocumentReference alloc] initWithReference:std::move(*parent)];
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString *)path {
|
2021-09-24 11:11:41 -04:00
|
|
|
return MakeNSString(self.reference.path());
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (FIRDocumentReference *)documentWithPath:(NSString *)documentPath {
|
|
|
|
|
if (!documentPath) {
|
2019-03-28 13:19:28 -07:00
|
|
|
ThrowInvalidArgument("Document path cannot be nil.");
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
2021-06-14 18:23:11 -04:00
|
|
|
if (!documentPath.length) {
|
|
|
|
|
ThrowInvalidArgument("Document path cannot be empty.");
|
|
|
|
|
}
|
2021-09-24 11:11:41 -04:00
|
|
|
DocumentReference child = self.reference.Document(MakeString(documentPath));
|
2019-06-04 18:10:17 -07:00
|
|
|
return [[FIRDocumentReference alloc] initWithReference:std::move(child)];
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (FIRDocumentReference *)addDocumentWithData:(NSDictionary<NSString *, id> *)data {
|
|
|
|
|
return [self addDocumentWithData:data completion:nil];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (FIRDocumentReference *)addDocumentWithData:(NSDictionary<NSString *, id> *)data
|
|
|
|
|
completion:
|
|
|
|
|
(nullable void (^)(NSError *_Nullable error))completion {
|
2021-07-22 12:44:17 -06:00
|
|
|
ParsedSetData parsed = [self.firestore.dataReader parsedSetData:data];
|
2019-06-04 18:10:17 -07:00
|
|
|
DocumentReference docRef =
|
2021-09-24 11:11:41 -04:00
|
|
|
self.reference.AddDocument(std::move(parsed), MakeCallback(completion));
|
2019-06-04 18:10:17 -07:00
|
|
|
return [[FIRDocumentReference alloc] initWithReference:std::move(docRef)];
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (FIRDocumentReference *)documentWithAutoID {
|
2019-06-04 18:10:17 -07:00
|
|
|
return [[FIRDocumentReference alloc] initWithReference:self.reference.Document()];
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|