From 4e7296b080b9c8cea13e5e5eeee65f4312fb5e8a Mon Sep 17 00:00:00 2001 From: zxu Date: Fri, 16 Mar 2018 15:42:05 -0400 Subject: port `DocumentKey` to non-container types of `Model/*` (#930) * naively remove FSTPath import and source/test files. * port FieldPath, part I * port FieldPath, part II * port ResourcePath, part I * port ResourcePath, part II * the grand commit to fix build errors * use testutil:: helper instead of those from FSTHelpers * fix test and lint * use c_str in errmsg directly * fix * fix * make code clean * fix integration test I missed * fix to avoid naming collision in preprocessor * address changes * address changes * address changes * fix: fieldMask are actually shared with different context. * address changes * add converter function between two DocumentKey implementations * add unit test * address changes * fix lint * using DocumentKey in model except for the container types `FSTDocumentDictionary`, `FSTDocumentKeySet`, and `FSTDocumentVersionDictionary` * change other place w.r.t. the use of `DocumentKey` in model * update parameter of test helpers from NSString to string_view * revert a temporary change used in debug * address changes --- Firestore/Source/Model/FSTDocument.h | 9 +++--- Firestore/Source/Model/FSTDocument.mm | 37 ++++++++++++++-------- Firestore/Source/Model/FSTDocumentSet.h | 11 ++++--- Firestore/Source/Model/FSTDocumentSet.mm | 20 +++++++----- Firestore/Source/Model/FSTMutation.h | 18 +++++------ Firestore/Source/Model/FSTMutation.mm | 50 ++++++++++++++++++------------ Firestore/Source/Model/FSTMutationBatch.h | 6 ++-- Firestore/Source/Model/FSTMutationBatch.mm | 11 +++++-- 8 files changed, 99 insertions(+), 63 deletions(-) (limited to 'Firestore/Source/Model') diff --git a/Firestore/Source/Model/FSTDocument.h b/Firestore/Source/Model/FSTDocument.h index 36237fd..47e4d28 100644 --- a/Firestore/Source/Model/FSTDocument.h +++ b/Firestore/Source/Model/FSTDocument.h @@ -16,9 +16,9 @@ #import +#include "Firestore/core/src/firebase/firestore/model/document_key.h" #include "Firestore/core/src/firebase/firestore/model/field_path.h" -@class FSTDocumentKey; @class FSTFieldValue; @class FSTObjectValue; @class FSTSnapshotVersion; @@ -31,14 +31,14 @@ NS_ASSUME_NONNULL_BEGIN */ @interface FSTMaybeDocument : NSObject - (id)init __attribute__((unavailable("Abstract base class"))); +- (const firebase::firestore::model::DocumentKey &)key; -@property(nonatomic, strong, readonly) FSTDocumentKey *key; @property(nonatomic, readonly) FSTSnapshotVersion *version; @end @interface FSTDocument : FSTMaybeDocument + (instancetype)documentWithData:(FSTObjectValue *)data - key:(FSTDocumentKey *)key + key:(firebase::firestore::model::DocumentKey)key version:(FSTSnapshotVersion *)version hasLocalMutations:(BOOL)mutations; @@ -50,7 +50,8 @@ NS_ASSUME_NONNULL_BEGIN @end @interface FSTDeletedDocument : FSTMaybeDocument -+ (instancetype)documentWithKey:(FSTDocumentKey *)key version:(FSTSnapshotVersion *)version; ++ (instancetype)documentWithKey:(firebase::firestore::model::DocumentKey)key + version:(FSTSnapshotVersion *)version; @end /** An NSComparator suitable for comparing docs using only their keys. */ diff --git a/Firestore/Source/Model/FSTDocument.mm b/Firestore/Source/Model/FSTDocument.mm index ca66da1..9898c2a 100644 --- a/Firestore/Source/Model/FSTDocument.mm +++ b/Firestore/Source/Model/FSTDocument.mm @@ -16,33 +16,38 @@ #import "Firestore/Source/Model/FSTDocument.h" +#include + #import "Firestore/Source/Core/FSTSnapshotVersion.h" -#import "Firestore/Source/Model/FSTDocumentKey.h" #import "Firestore/Source/Model/FSTFieldValue.h" #import "Firestore/Source/Util/FSTAssert.h" +#include "Firestore/core/src/firebase/firestore/model/document_key.h" #include "Firestore/core/src/firebase/firestore/model/field_path.h" #include "Firestore/core/src/firebase/firestore/util/string_apple.h" namespace util = firebase::firestore::util; +using firebase::firestore::model::DocumentKey; using firebase::firestore::model::FieldPath; NS_ASSUME_NONNULL_BEGIN @interface FSTMaybeDocument () -- (instancetype)initWithKey:(FSTDocumentKey *)key +- (instancetype)initWithKey:(DocumentKey)key version:(FSTSnapshotVersion *)version NS_DESIGNATED_INITIALIZER; @end -@implementation FSTMaybeDocument +@implementation FSTMaybeDocument { + DocumentKey _key; +} -- (instancetype)initWithKey:(FSTDocumentKey *)key version:(FSTSnapshotVersion *)version { +- (instancetype)initWithKey:(DocumentKey)key version:(FSTSnapshotVersion *)version { FSTAssert(!!version, @"Version must not be nil."); self = [super init]; if (self) { - _key = key; + _key = std::move(key); _version = version; } return self; @@ -53,23 +58,29 @@ NS_ASSUME_NONNULL_BEGIN return self; } +- (const DocumentKey &)key { + return _key; +} + @end @implementation FSTDocument + (instancetype)documentWithData:(FSTObjectValue *)data - key:(FSTDocumentKey *)key + key:(DocumentKey)key version:(FSTSnapshotVersion *)version hasLocalMutations:(BOOL)mutations { - return - [[FSTDocument alloc] initWithData:data key:key version:version hasLocalMutations:mutations]; + return [[FSTDocument alloc] initWithData:data + key:std::move(key) + version:version + hasLocalMutations:mutations]; } - (instancetype)initWithData:(FSTObjectValue *)data - key:(FSTDocumentKey *)key + key:(DocumentKey)key version:(FSTSnapshotVersion *)version hasLocalMutations:(BOOL)mutations { - self = [super initWithKey:key version:version]; + self = [super initWithKey:std::move(key) version:version]; if (self) { _data = data; _localMutations = mutations; @@ -100,7 +111,7 @@ NS_ASSUME_NONNULL_BEGIN - (NSString *)description { return [NSString stringWithFormat:@"", - self.key.path.CanonicalString().c_str(), self.version, + self.key.ToString().c_str(), self.version, self.localMutations ? @"YES" : @"NO", self.data]; } @@ -112,8 +123,8 @@ NS_ASSUME_NONNULL_BEGIN @implementation FSTDeletedDocument -+ (instancetype)documentWithKey:(FSTDocumentKey *)key version:(FSTSnapshotVersion *)version { - return [[FSTDeletedDocument alloc] initWithKey:key version:version]; ++ (instancetype)documentWithKey:(DocumentKey)key version:(FSTSnapshotVersion *)version { + return [[FSTDeletedDocument alloc] initWithKey:std::move(key) version:version]; } - (BOOL)isEqual:(id)other { diff --git a/Firestore/Source/Model/FSTDocumentSet.h b/Firestore/Source/Model/FSTDocumentSet.h index 022e900..b5521e7 100644 --- a/Firestore/Source/Model/FSTDocumentSet.h +++ b/Firestore/Source/Model/FSTDocumentSet.h @@ -18,8 +18,9 @@ #import "Firestore/Source/Model/FSTDocumentDictionary.h" +#include "Firestore/core/src/firebase/firestore/model/document_key.h" + @class FSTDocument; -@class FSTDocumentKey; NS_ASSUME_NONNULL_BEGIN @@ -41,10 +42,10 @@ NS_ASSUME_NONNULL_BEGIN - (BOOL)isEmpty; /** Returns YES if this set contains a document with the given key. */ -- (BOOL)containsKey:(FSTDocumentKey *)key; +- (BOOL)containsKey:(const firebase::firestore::model::DocumentKey &)key; /** Returns the document from this set with the given key if it exists or nil if it doesn't. */ -- (FSTDocument *_Nullable)documentForKey:(FSTDocumentKey *)key; +- (FSTDocument *_Nullable)documentForKey:(const firebase::firestore::model::DocumentKey &)key; /** * Returns the first document in the set according to its built in ordering, or nil if the set @@ -62,7 +63,7 @@ NS_ASSUME_NONNULL_BEGIN * Returns the index of the document with the provided key in the document set. Returns NSNotFound * if the key is not present. */ -- (NSUInteger)indexOfKey:(FSTDocumentKey *)key; +- (NSUInteger)indexOfKey:(const firebase::firestore::model::DocumentKey &)key; - (NSEnumerator *)documentEnumerator; @@ -79,7 +80,7 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)documentSetByAddingDocument:(FSTDocument *_Nullable)document; /** Returns a new FSTDocumentSet that excludes any document associated with the given key. */ -- (instancetype)documentSetByRemovingKey:(FSTDocumentKey *)key; +- (instancetype)documentSetByRemovingKey:(const firebase::firestore::model::DocumentKey &)key; @end NS_ASSUME_NONNULL_END diff --git a/Firestore/Source/Model/FSTDocumentSet.mm b/Firestore/Source/Model/FSTDocumentSet.mm index 6f44799..2f0b42b 100644 --- a/Firestore/Source/Model/FSTDocumentSet.mm +++ b/Firestore/Source/Model/FSTDocumentSet.mm @@ -20,6 +20,10 @@ #import "Firestore/Source/Model/FSTDocumentKey.h" #import "Firestore/third_party/Immutable/FSTImmutableSortedSet.h" +#include "Firestore/core/src/firebase/firestore/model/document_key.h" + +using firebase::firestore::model::DocumentKey; + NS_ASSUME_NONNULL_BEGIN /** @@ -119,12 +123,12 @@ typedef FSTImmutableSortedSet SetType; return [self.index isEmpty]; } -- (BOOL)containsKey:(FSTDocumentKey *)key { - return [self.index objectForKey:key] != nil; +- (BOOL)containsKey:(const DocumentKey &)key { + return [self.index objectForKey:(FSTDocumentKey *)key] != nil; } -- (FSTDocument *_Nullable)documentForKey:(FSTDocumentKey *)key { - return [self.index objectForKey:key]; +- (FSTDocument *_Nullable)documentForKey:(const DocumentKey &)key { + return [self.index objectForKey:(FSTDocumentKey *)key]; } - (FSTDocument *_Nullable)firstDocument { @@ -135,8 +139,8 @@ typedef FSTImmutableSortedSet SetType; return [self.sortedSet lastObject]; } -- (NSUInteger)indexOfKey:(FSTDocumentKey *)key { - FSTDocument *doc = [self.index objectForKey:key]; +- (NSUInteger)indexOfKey:(const DocumentKey &)key { + FSTDocument *doc = [self.index objectForKey:(FSTDocumentKey *)key]; return doc ? [self.sortedSet indexOfObject:doc] : NSNotFound; } @@ -171,8 +175,8 @@ typedef FSTImmutableSortedSet SetType; return [[FSTDocumentSet alloc] initWithIndex:index set:set]; } -- (instancetype)documentSetByRemovingKey:(FSTDocumentKey *)key { - FSTDocument *doc = [self.index objectForKey:key]; +- (instancetype)documentSetByRemovingKey:(const DocumentKey &)key { + FSTDocument *doc = [self.index objectForKey:(FSTDocumentKey *)key]; if (!doc) { return self; } diff --git a/Firestore/Source/Model/FSTMutation.h b/Firestore/Source/Model/FSTMutation.h index 2d29dc9..4e4357d 100644 --- a/Firestore/Source/Model/FSTMutation.h +++ b/Firestore/Source/Model/FSTMutation.h @@ -18,10 +18,10 @@ #include +#include "Firestore/core/src/firebase/firestore/model/document_key.h" #include "Firestore/core/src/firebase/firestore/model/field_path.h" @class FSTDocument; -@class FSTDocumentKey; @class FSTFieldValue; @class FSTMaybeDocument; @class FSTObjectValue; @@ -155,7 +155,7 @@ typedef NS_ENUM(NSUInteger, FSTPreconditionExists) { - (id)init NS_UNAVAILABLE; -- (instancetype)initWithKey:(FSTDocumentKey *)key +- (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key precondition:(FSTPrecondition *)precondition NS_DESIGNATED_INITIALIZER; /** @@ -215,7 +215,7 @@ typedef NS_ENUM(NSUInteger, FSTPreconditionExists) { baseDocument:(nullable FSTMaybeDocument *)baseDoc localWriteTime:(nullable FIRTimestamp *)localWriteTime; -@property(nonatomic, strong, readonly) FSTDocumentKey *key; +- (const firebase::firestore::model::DocumentKey &)key; /** The precondition for this mutation. */ @property(nonatomic, strong, readonly) FSTPrecondition *precondition; @@ -230,7 +230,7 @@ typedef NS_ENUM(NSUInteger, FSTPreconditionExists) { */ @interface FSTSetMutation : FSTMutation -- (instancetype)initWithKey:(FSTDocumentKey *)key +- (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key precondition:(FSTPrecondition *)precondition NS_UNAVAILABLE; /** @@ -241,7 +241,7 @@ typedef NS_ENUM(NSUInteger, FSTPreconditionExists) { * key. * @param precondition The precondition for this mutation. */ -- (instancetype)initWithKey:(FSTDocumentKey *)key +- (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key value:(FSTObjectValue *)value precondition:(FSTPrecondition *)precondition NS_DESIGNATED_INITIALIZER; @@ -263,7 +263,7 @@ typedef NS_ENUM(NSUInteger, FSTPreconditionExists) { @interface FSTPatchMutation : FSTMutation /** Returns the precondition for the given FSTPrecondition. */ -- (instancetype)initWithKey:(FSTDocumentKey *)key +- (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key precondition:(FSTPrecondition *)precondition NS_UNAVAILABLE; /** @@ -277,7 +277,7 @@ typedef NS_ENUM(NSUInteger, FSTPreconditionExists) { * to determine the locations at which it should be applied). * @param precondition The precondition for this mutation. */ -- (instancetype)initWithKey:(FSTDocumentKey *)key +- (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key fieldMask:(FSTFieldMask *)fieldMask value:(FSTObjectValue *)value precondition:(FSTPrecondition *)precondition NS_DESIGNATED_INITIALIZER; @@ -306,7 +306,7 @@ typedef NS_ENUM(NSUInteger, FSTPreconditionExists) { */ @interface FSTTransformMutation : FSTMutation -- (instancetype)initWithKey:(FSTDocumentKey *)key +- (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key precondition:(FSTPrecondition *)precondition NS_UNAVAILABLE; /** @@ -315,7 +315,7 @@ typedef NS_ENUM(NSUInteger, FSTPreconditionExists) { * @param key Identifies the location of the document to mutate. * @param fieldTransforms A list of FSTFieldTransform objects to perform to the document. */ -- (instancetype)initWithKey:(FSTDocumentKey *)key +- (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key fieldTransforms:(NSArray *)fieldTransforms NS_DESIGNATED_INITIALIZER; diff --git a/Firestore/Source/Model/FSTMutation.mm b/Firestore/Source/Model/FSTMutation.mm index a61ee84..253a853 100644 --- a/Firestore/Source/Model/FSTMutation.mm +++ b/Firestore/Source/Model/FSTMutation.mm @@ -16,17 +16,20 @@ #import "Firestore/Source/Model/FSTMutation.h" +#include + #import "FIRTimestamp.h" #import "Firestore/Source/Core/FSTSnapshotVersion.h" #import "Firestore/Source/Model/FSTDocument.h" -#import "Firestore/Source/Model/FSTDocumentKey.h" #import "Firestore/Source/Model/FSTFieldValue.h" #import "Firestore/Source/Util/FSTAssert.h" #import "Firestore/Source/Util/FSTClasses.h" +#include "Firestore/core/src/firebase/firestore/model/document_key.h" #include "Firestore/core/src/firebase/firestore/model/field_path.h" +using firebase::firestore::model::DocumentKey; using firebase::firestore::model::FieldPath; NS_ASSUME_NONNULL_BEGIN @@ -246,11 +249,13 @@ NS_ASSUME_NONNULL_BEGIN #pragma mark - FSTMutation -@implementation FSTMutation +@implementation FSTMutation { + DocumentKey _key; +} -- (instancetype)initWithKey:(FSTDocumentKey *)key precondition:(FSTPrecondition *)precondition { +- (instancetype)initWithKey:(DocumentKey)key precondition:(FSTPrecondition *)precondition { if (self = [super init]) { - _key = key; + _key = std::move(key); _precondition = precondition; } return self; @@ -270,24 +275,28 @@ NS_ASSUME_NONNULL_BEGIN [self applyTo:maybeDoc baseDocument:baseDoc localWriteTime:localWriteTime mutationResult:nil]; } +- (const DocumentKey &)key { + return _key; +} + @end #pragma mark - FSTSetMutation @implementation FSTSetMutation -- (instancetype)initWithKey:(FSTDocumentKey *)key +- (instancetype)initWithKey:(DocumentKey)key value:(FSTObjectValue *)value precondition:(FSTPrecondition *)precondition { - if (self = [super initWithKey:key precondition:precondition]) { + if (self = [super initWithKey:std::move(key) precondition:precondition]) { _value = value; } return self; } - (NSString *)description { - return [NSString stringWithFormat:@"", self.key, - self.value, self.precondition]; + return [NSString stringWithFormat:@"", + self.key.ToString().c_str(), self.value, self.precondition]; } - (BOOL)isEqual:(id)other { @@ -347,11 +356,11 @@ NS_ASSUME_NONNULL_BEGIN @implementation FSTPatchMutation -- (instancetype)initWithKey:(FSTDocumentKey *)key +- (instancetype)initWithKey:(DocumentKey)key fieldMask:(FSTFieldMask *)fieldMask value:(FSTObjectValue *)value precondition:(FSTPrecondition *)precondition { - self = [super initWithKey:key precondition:precondition]; + self = [super initWithKey:std::move(key) precondition:precondition]; if (self) { _fieldMask = fieldMask; _value = value; @@ -382,8 +391,9 @@ NS_ASSUME_NONNULL_BEGIN } - (NSString *)description { - return [NSString stringWithFormat:@"", - self.key, self.fieldMask, self.value, self.precondition]; + return [NSString stringWithFormat:@"", + self.key.ToString().c_str(), self.fieldMask, self.value, + self.precondition]; } - (nullable FSTMaybeDocument *)applyTo:(nullable FSTMaybeDocument *)maybeDoc @@ -401,7 +411,7 @@ NS_ASSUME_NONNULL_BEGIN BOOL hasLocalMutations = (mutationResult == nil); if (!maybeDoc || [maybeDoc isMemberOfClass:[FSTDeletedDocument class]]) { // Precondition applied, so create the document if necessary - FSTDocumentKey *key = maybeDoc ? maybeDoc.key : self.key; + const DocumentKey &key = maybeDoc ? maybeDoc.key : self.key; FSTSnapshotVersion *version = maybeDoc ? maybeDoc.version : [FSTSnapshotVersion noVersion]; maybeDoc = [FSTDocument documentWithData:[FSTObjectValue objectValue] key:key @@ -439,12 +449,13 @@ NS_ASSUME_NONNULL_BEGIN @implementation FSTTransformMutation -- (instancetype)initWithKey:(FSTDocumentKey *)key +- (instancetype)initWithKey:(DocumentKey)key fieldTransforms:(NSArray *)fieldTransforms { // NOTE: We set a precondition of exists: true as a safety-check, since we always combine // FSTTransformMutations with a FSTSetMutation or FSTPatchMutation which (if successful) should // end up with an existing document. - if (self = [super initWithKey:key precondition:[FSTPrecondition preconditionWithExists:YES]]) { + if (self = [super initWithKey:std::move(key) + precondition:[FSTPrecondition preconditionWithExists:YES]]) { _fieldTransforms = fieldTransforms; } return self; @@ -472,8 +483,9 @@ NS_ASSUME_NONNULL_BEGIN } - (NSString *)description { - return [NSString stringWithFormat:@"", - self.key, self.fieldTransforms, self.precondition]; + return [NSString stringWithFormat:@"", + self.key.ToString().c_str(), self.fieldTransforms, + self.precondition]; } - (nullable FSTMaybeDocument *)applyTo:(nullable FSTMaybeDocument *)maybeDoc @@ -584,8 +596,8 @@ NS_ASSUME_NONNULL_BEGIN } - (NSString *)description { - return [NSString - stringWithFormat:@"", self.key, self.precondition]; + return [NSString stringWithFormat:@"", + self.key.ToString().c_str(), self.precondition]; } - (nullable FSTMaybeDocument *)applyTo:(nullable FSTMaybeDocument *)maybeDoc diff --git a/Firestore/Source/Model/FSTMutationBatch.h b/Firestore/Source/Model/FSTMutationBatch.h index 1de79fe..3c82338 100644 --- a/Firestore/Source/Model/FSTMutationBatch.h +++ b/Firestore/Source/Model/FSTMutationBatch.h @@ -20,6 +20,8 @@ #import "Firestore/Source/Model/FSTDocumentKeySet.h" #import "Firestore/Source/Model/FSTDocumentVersionDictionary.h" +#include "Firestore/core/src/firebase/firestore/model/document_key.h" + @class FSTMutation; @class FIRTimestamp; @class FSTMutationResult; @@ -60,7 +62,7 @@ extern const FSTBatchID kFSTBatchIDUnknown; * their hasLocalMutations flag set. */ - (FSTMaybeDocument *_Nullable)applyTo:(FSTMaybeDocument *_Nullable)maybeDoc - documentKey:(FSTDocumentKey *)documentKey + documentKey:(const firebase::firestore::model::DocumentKey &)documentKey mutationBatchResult:(FSTMutationBatchResult *_Nullable)mutationBatchResult; /** @@ -68,7 +70,7 @@ extern const FSTBatchID kFSTBatchIDUnknown; * the backend). */ - (FSTMaybeDocument *_Nullable)applyTo:(FSTMaybeDocument *_Nullable)maybeDoc - documentKey:(FSTDocumentKey *)documentKey; + documentKey:(const firebase::firestore::model::DocumentKey &)documentKey; /** * Returns YES if this mutation batch has already been removed from the mutation queue. diff --git a/Firestore/Source/Model/FSTMutationBatch.mm b/Firestore/Source/Model/FSTMutationBatch.mm index 07aadbb..6598d27 100644 --- a/Firestore/Source/Model/FSTMutationBatch.mm +++ b/Firestore/Source/Model/FSTMutationBatch.mm @@ -24,6 +24,10 @@ #import "Firestore/Source/Model/FSTMutation.h" #import "Firestore/Source/Util/FSTAssert.h" +#include "Firestore/core/src/firebase/firestore/model/document_key.h" + +using firebase::firestore::model::DocumentKey; + NS_ASSUME_NONNULL_BEGIN const FSTBatchID kFSTBatchIDUnknown = -1; @@ -68,10 +72,11 @@ const FSTBatchID kFSTBatchIDUnknown = -1; } - (FSTMaybeDocument *_Nullable)applyTo:(FSTMaybeDocument *_Nullable)maybeDoc - documentKey:(FSTDocumentKey *)documentKey + documentKey:(const DocumentKey &)documentKey mutationBatchResult:(FSTMutationBatchResult *_Nullable)mutationBatchResult { FSTAssert(!maybeDoc || [maybeDoc.key isEqualToKey:documentKey], - @"applyTo: key %@ doesn't match maybeDoc key %@", documentKey, maybeDoc.key); + @"applyTo: key %s doesn't match maybeDoc key %s", documentKey.ToString().c_str(), + maybeDoc.key.ToString().c_str()); FSTMaybeDocument *baseDoc = maybeDoc; if (mutationBatchResult) { FSTAssert(mutationBatchResult.mutationResults.count == self.mutations.count, @@ -94,7 +99,7 @@ const FSTBatchID kFSTBatchIDUnknown = -1; } - (FSTMaybeDocument *_Nullable)applyTo:(FSTMaybeDocument *_Nullable)maybeDoc - documentKey:(FSTDocumentKey *)documentKey { + documentKey:(const DocumentKey &)documentKey { return [self applyTo:maybeDoc documentKey:documentKey mutationBatchResult:nil]; } -- cgit v1.2.3