2017-10-03 08:55:22 -07:00
|
|
|
/*
|
2020-02-10 14:44:10 -08:00
|
|
|
* Copyright 2017 Google LLC
|
2017-10-03 08:55:22 -07:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-04-15 09:06:10 -07:00
|
|
|
#import "FIRDocumentReference+Internal.h"
|
2017-10-03 08:55:22 -07:00
|
|
|
|
2018-03-27 14:33:39 -04:00
|
|
|
#include <memory>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
2017-11-16 20:52:01 -08:00
|
|
|
#import "FIRFirestoreErrors.h"
|
|
|
|
|
#import "Firestore/Source/API/FIRCollectionReference+Internal.h"
|
|
|
|
|
#import "Firestore/Source/API/FIRDocumentReference+Internal.h"
|
|
|
|
|
#import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
|
|
|
|
|
#import "Firestore/Source/API/FIRFirestore+Internal.h"
|
2019-04-09 07:07:20 -07:00
|
|
|
#import "Firestore/Source/API/FIRFirestoreSource+Internal.h"
|
2017-11-16 20:52:01 -08:00
|
|
|
#import "Firestore/Source/API/FIRListenerRegistration+Internal.h"
|
|
|
|
|
#import "Firestore/Source/API/FSTUserDataConverter.h"
|
|
|
|
|
|
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/api/document_snapshot.h"
|
|
|
|
|
#include "Firestore/core/src/api/source.h"
|
|
|
|
|
#include "Firestore/core/src/core/event_listener.h"
|
|
|
|
|
#include "Firestore/core/src/core/listen_options.h"
|
|
|
|
|
#include "Firestore/core/src/core/user_data.h"
|
|
|
|
|
#include "Firestore/core/src/model/document_key.h"
|
|
|
|
|
#include "Firestore/core/src/model/document_set.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/status.h"
|
|
|
|
|
#include "Firestore/core/src/util/statusor.h"
|
|
|
|
|
#include "Firestore/core/src/util/string_apple.h"
|
2018-03-06 14:28:04 -05:00
|
|
|
|
|
|
|
|
namespace util = firebase::firestore::util;
|
2019-06-04 18:10:17 -07:00
|
|
|
using firebase::firestore::api::CollectionReference;
|
2019-03-06 14:17:52 -05:00
|
|
|
using firebase::firestore::api::DocumentReference;
|
2019-03-15 19:10:32 -04:00
|
|
|
using firebase::firestore::api::DocumentSnapshot;
|
2020-02-10 14:44:10 -08:00
|
|
|
using firebase::firestore::api::DocumentSnapshotListener;
|
2019-03-15 19:10:32 -04:00
|
|
|
using firebase::firestore::api::Firestore;
|
2019-04-12 17:03:26 -07:00
|
|
|
using firebase::firestore::api::ListenerRegistration;
|
2019-04-09 07:07:20 -07:00
|
|
|
using firebase::firestore::api::Source;
|
|
|
|
|
using firebase::firestore::api::MakeSource;
|
2019-03-26 11:54:03 -07:00
|
|
|
using firebase::firestore::core::EventListener;
|
2019-03-26 07:11:02 -07:00
|
|
|
using firebase::firestore::core::ListenOptions;
|
2018-09-18 09:19:11 -07:00
|
|
|
using firebase::firestore::core::ParsedSetData;
|
|
|
|
|
using firebase::firestore::core::ParsedUpdateData;
|
2018-03-27 14:33:39 -04:00
|
|
|
using firebase::firestore::model::DocumentKey;
|
2018-03-06 14:28:04 -05:00
|
|
|
using firebase::firestore::model::ResourcePath;
|
2019-03-15 19:10:32 -04:00
|
|
|
using firebase::firestore::util::Status;
|
|
|
|
|
using firebase::firestore::util::StatusOr;
|
|
|
|
|
using firebase::firestore::util::StatusOrCallback;
|
2019-10-18 12:58:45 -07:00
|
|
|
using firebase::firestore::util::ThrowInvalidArgument;
|
2018-03-06 14:28:04 -05:00
|
|
|
|
2017-10-03 08:55:22 -07:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
|
|
#pragma mark - FIRDocumentReference
|
|
|
|
|
|
2018-03-27 14:33:39 -04:00
|
|
|
@implementation FIRDocumentReference {
|
2019-03-06 14:17:52 -05:00
|
|
|
DocumentReference _documentReference;
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
2019-03-06 14:17:52 -05:00
|
|
|
- (instancetype)initWithReference:(DocumentReference &&)reference {
|
2017-10-03 08:55:22 -07:00
|
|
|
if (self = [super init]) {
|
2019-03-06 14:17:52 -05:00
|
|
|
_documentReference = std::move(reference);
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-15 09:06:10 -07:00
|
|
|
- (instancetype)initWithPath:(ResourcePath)path firestore:(std::shared_ptr<Firestore>)firestore {
|
2019-03-15 19:10:32 -04:00
|
|
|
if (path.size() % 2 != 0) {
|
2019-03-28 13:19:28 -07:00
|
|
|
ThrowInvalidArgument("Invalid document reference. Document references must have an even "
|
|
|
|
|
"number of segments, but %s has %s",
|
|
|
|
|
path.CanonicalString(), path.size());
|
2019-03-15 19:10:32 -04:00
|
|
|
}
|
|
|
|
|
return [self initWithKey:DocumentKey{std::move(path)} firestore:firestore];
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-15 09:06:10 -07:00
|
|
|
- (instancetype)initWithKey:(DocumentKey)key firestore:(std::shared_ptr<Firestore>)firestore {
|
2019-03-15 19:10:32 -04:00
|
|
|
DocumentReference delegate{std::move(key), firestore};
|
|
|
|
|
return [self initWithReference:std::move(delegate)];
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-19 08:23:25 -07:00
|
|
|
#pragma mark - NSObject Methods
|
|
|
|
|
|
|
|
|
|
- (BOOL)isEqual:(nullable id)other {
|
|
|
|
|
if (other == self) return YES;
|
2017-12-19 13:47:04 -05:00
|
|
|
if (![[other class] isEqual:[self class]]) return NO;
|
2017-10-19 08:23:25 -07:00
|
|
|
|
2019-03-06 14:17:52 -05:00
|
|
|
return _documentReference == static_cast<FIRDocumentReference *>(other)->_documentReference;
|
2017-10-19 08:23:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSUInteger)hash {
|
2019-03-06 14:17:52 -05:00
|
|
|
return _documentReference.Hash();
|
2017-10-19 08:23:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - Public Methods
|
|
|
|
|
|
2019-03-06 14:17:52 -05:00
|
|
|
@dynamic firestore;
|
|
|
|
|
|
|
|
|
|
- (FIRFirestore *)firestore {
|
2019-03-15 19:10:32 -04:00
|
|
|
return [FIRFirestore recoverFromFirestore:_documentReference.firestore()];
|
2019-03-06 14:17:52 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-03 08:55:22 -07:00
|
|
|
- (NSString *)documentID {
|
2019-06-27 17:31:17 -07:00
|
|
|
return util::MakeNSString(_documentReference.document_id());
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (FIRCollectionReference *)parent {
|
2019-06-04 18:10:17 -07:00
|
|
|
return [[FIRCollectionReference alloc] initWithReference:_documentReference.Parent()];
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString *)path {
|
2019-06-27 17:31:17 -07:00
|
|
|
return util::MakeNSString(_documentReference.Path());
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (FIRCollectionReference *)collectionWithPath:(NSString *)collectionPath {
|
|
|
|
|
if (!collectionPath) {
|
2019-03-28 13:19:28 -07:00
|
|
|
ThrowInvalidArgument("Collection path cannot be nil.");
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
2019-03-15 19:10:32 -04:00
|
|
|
|
2019-06-04 18:10:17 -07:00
|
|
|
CollectionReference child =
|
|
|
|
|
_documentReference.GetCollectionReference(util::MakeString(collectionPath));
|
|
|
|
|
return [[FIRCollectionReference alloc] initWithReference:std::move(child)];
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)setData:(NSDictionary<NSString *, id> *)documentData {
|
2019-03-06 14:17:52 -05:00
|
|
|
[self setData:documentData merge:NO completion:nil];
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-15 13:06:06 -07:00
|
|
|
- (void)setData:(NSDictionary<NSString *, id> *)documentData merge:(BOOL)merge {
|
2019-03-06 14:17:52 -05:00
|
|
|
[self setData:documentData merge:merge completion:nil];
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-02 11:10:19 -07:00
|
|
|
- (void)setData:(NSDictionary<NSString *, id> *)documentData
|
|
|
|
|
mergeFields:(NSArray<id> *)mergeFields {
|
2019-03-06 14:17:52 -05:00
|
|
|
[self setData:documentData mergeFields:mergeFields completion:nil];
|
2018-05-02 11:10:19 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-03 08:55:22 -07:00
|
|
|
- (void)setData:(NSDictionary<NSString *, id> *)documentData
|
|
|
|
|
completion:(nullable void (^)(NSError *_Nullable error))completion {
|
2019-03-06 14:17:52 -05:00
|
|
|
[self setData:documentData merge:NO completion:completion];
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)setData:(NSDictionary<NSString *, id> *)documentData
|
2018-04-15 13:06:06 -07:00
|
|
|
merge:(BOOL)merge
|
2017-10-03 08:55:22 -07:00
|
|
|
completion:(nullable void (^)(NSError *_Nullable error))completion {
|
2019-03-15 19:10:32 -04:00
|
|
|
auto dataConverter = self.firestore.dataConverter;
|
|
|
|
|
ParsedSetData parsed = merge ? [dataConverter parsedMergeData:documentData fieldMask:nil]
|
|
|
|
|
: [dataConverter parsedSetData:documentData];
|
2019-04-17 14:50:33 -07:00
|
|
|
_documentReference.SetData(std::move(parsed), util::MakeCallback(completion));
|
2018-05-02 11:10:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)setData:(NSDictionary<NSString *, id> *)documentData
|
|
|
|
|
mergeFields:(NSArray<id> *)mergeFields
|
|
|
|
|
completion:(nullable void (^)(NSError *_Nullable error))completion {
|
2019-03-15 19:10:32 -04:00
|
|
|
ParsedSetData parsed = [self.firestore.dataConverter parsedMergeData:documentData
|
|
|
|
|
fieldMask:mergeFields];
|
2019-04-17 14:50:33 -07:00
|
|
|
_documentReference.SetData(std::move(parsed), util::MakeCallback(completion));
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)updateData:(NSDictionary<id, id> *)fields {
|
2019-03-06 14:17:52 -05:00
|
|
|
[self updateData:fields completion:nil];
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)updateData:(NSDictionary<id, id> *)fields
|
|
|
|
|
completion:(nullable void (^)(NSError *_Nullable error))completion {
|
2019-03-15 19:10:32 -04:00
|
|
|
ParsedUpdateData parsed = [self.firestore.dataConverter parsedUpdateData:fields];
|
2019-04-17 14:50:33 -07:00
|
|
|
_documentReference.UpdateData(std::move(parsed), util::MakeCallback(completion));
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)deleteDocument {
|
2019-03-06 14:17:52 -05:00
|
|
|
[self deleteDocumentWithCompletion:nil];
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)deleteDocumentWithCompletion:(nullable void (^)(NSError *_Nullable error))completion {
|
2019-04-16 17:37:38 -07:00
|
|
|
_documentReference.DeleteDocument(util::MakeCallback(completion));
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
2019-03-15 19:10:32 -04:00
|
|
|
- (void)getDocumentWithCompletion:(FIRDocumentSnapshotBlock)completion {
|
2019-04-09 07:07:20 -07:00
|
|
|
_documentReference.GetDocument(Source::Default, [self wrapDocumentSnapshotBlock:completion]);
|
2018-04-18 10:30:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)getDocumentWithSource:(FIRFirestoreSource)source
|
2019-03-15 19:10:32 -04:00
|
|
|
completion:(FIRDocumentSnapshotBlock)completion {
|
2019-04-09 07:07:20 -07:00
|
|
|
_documentReference.GetDocument(MakeSource(source), [self wrapDocumentSnapshotBlock:completion]);
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id<FIRListenerRegistration>)addSnapshotListener:(FIRDocumentSnapshotBlock)listener {
|
2018-04-14 17:19:06 -07:00
|
|
|
return [self addSnapshotListenerWithIncludeMetadataChanges:NO listener:listener];
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-14 17:19:06 -07:00
|
|
|
- (id<FIRListenerRegistration>)
|
2018-08-13 14:18:02 -07:00
|
|
|
addSnapshotListenerWithIncludeMetadataChanges:(BOOL)includeMetadataChanges
|
|
|
|
|
listener:(FIRDocumentSnapshotBlock)listener {
|
2019-03-25 20:01:20 -07:00
|
|
|
ListenOptions options = ListenOptions::FromIncludeMetadataChanges(includeMetadataChanges);
|
2018-04-14 17:19:06 -07:00
|
|
|
return [self addSnapshotListenerInternalWithOptions:options listener:listener];
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
2019-03-25 20:01:20 -07:00
|
|
|
- (id<FIRListenerRegistration>)addSnapshotListenerInternalWithOptions:(ListenOptions)internalOptions
|
|
|
|
|
listener:(FIRDocumentSnapshotBlock)
|
|
|
|
|
listener {
|
2019-09-12 16:44:14 -07:00
|
|
|
std::unique_ptr<ListenerRegistration> result = _documentReference.AddSnapshotListener(
|
2019-03-26 11:54:03 -07:00
|
|
|
std::move(internalOptions), [self wrapDocumentSnapshotBlock:listener]);
|
2019-03-26 08:29:17 -07:00
|
|
|
return [[FSTListenerRegistration alloc] initWithRegistration:std::move(result)];
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
2020-02-10 14:44:10 -08:00
|
|
|
- (DocumentSnapshotListener)wrapDocumentSnapshotBlock:(FIRDocumentSnapshotBlock)block {
|
2019-03-26 11:54:03 -07:00
|
|
|
class Converter : public EventListener<DocumentSnapshot> {
|
|
|
|
|
public:
|
|
|
|
|
explicit Converter(FIRDocumentSnapshotBlock block) : block_(block) {
|
2019-03-15 19:10:32 -04:00
|
|
|
}
|
2019-03-26 11:54:03 -07:00
|
|
|
|
|
|
|
|
void OnEvent(StatusOr<DocumentSnapshot> maybe_snapshot) override {
|
|
|
|
|
if (maybe_snapshot.ok()) {
|
|
|
|
|
FIRDocumentSnapshot *result =
|
|
|
|
|
[[FIRDocumentSnapshot alloc] initWithSnapshot:std::move(maybe_snapshot).ValueOrDie()];
|
|
|
|
|
block_(result, nil);
|
|
|
|
|
} else {
|
|
|
|
|
block_(nil, util::MakeNSError(maybe_snapshot.status()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
FIRDocumentSnapshotBlock block_;
|
2019-03-15 19:10:32 -04:00
|
|
|
};
|
2019-03-26 11:54:03 -07:00
|
|
|
return absl::make_unique<Converter>(block);
|
2019-03-15 19:10:32 -04:00
|
|
|
}
|
|
|
|
|
|
2017-10-03 08:55:22 -07:00
|
|
|
@end
|
|
|
|
|
|
2018-03-27 14:33:39 -04:00
|
|
|
#pragma mark - FIRDocumentReference (Internal)
|
|
|
|
|
|
|
|
|
|
@implementation FIRDocumentReference (Internal)
|
|
|
|
|
|
2019-04-18 10:34:53 -07:00
|
|
|
- (const api::DocumentReference &)internalReference {
|
|
|
|
|
return _documentReference;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-06 14:17:52 -05:00
|
|
|
- (const DocumentKey &)key {
|
|
|
|
|
return _documentReference.key();
|
2018-03-27 14:33:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
2017-10-03 08:55:22 -07:00
|
|
|
NS_ASSUME_NONNULL_END
|