Files
firebase-ios-sdk/Firestore/Source/API/FIRCollectionReference.mm
T

128 lines
4.4 KiB
Plaintext
Raw Normal View History

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"
#include <utility>
2017-10-03 08:55:22 -07:00
#import "Firestore/Source/API/FIRDocumentReference+Internal.h"
#import "Firestore/Source/API/FIRFirestore+Internal.h"
#import "Firestore/Source/API/FIRQuery+Internal.h"
2019-06-04 18:10:17 -07:00
#import "Firestore/Source/API/FSTUserDataConverter.h"
2017-10-03 08:55:22 -07:00
2019-06-04 18:10:17 -07:00
#include "Firestore/core/src/firebase/firestore/api/collection_reference.h"
#include "Firestore/core/src/firebase/firestore/model/resource_path.h"
2019-06-04 18:10:17 -07:00
#include "Firestore/core/src/firebase/firestore/util/error_apple.h"
#include "Firestore/core/src/firebase/firestore/util/exception.h"
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"
namespace util = firebase::firestore::util;
2019-06-04 18:10:17 -07:00
using firebase::firestore::api::CollectionReference;
using firebase::firestore::api::DocumentReference;
using firebase::firestore::core::ParsedSetData;
using firebase::firestore::model::ResourcePath;
using firebase::firestore::util::ThrowInvalidArgument;
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.
2019-05-14 14:04:35 -07:00
- (instancetype)initWithQuery:(api::Query &&)query {
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 {
2019-06-27 17:31:17 -07:00
return util::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 {
2019-06-27 17:31:17 -07:00
return util::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
}
2019-06-04 18:10:17 -07:00
DocumentReference child = self.reference.Document(util::MakeString(documentPath));
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 {
2019-06-04 18:10:17 -07:00
ParsedSetData parsed = [self.firestore.dataConverter parsedSetData:data];
DocumentReference docRef =
self.reference.AddDocument(std::move(parsed), util::MakeCallback(completion));
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