From db717bf6704b444b093d46f53935402c83441854 Mon Sep 17 00:00:00 2001 From: zxu Date: Thu, 5 Apr 2018 11:52:07 -0400 Subject: Port transform operations to C++ (#1020) * port FieldMask to C++ * address changes * address changes * fix test * address change * Port transform operations (FSTTransformOperation, FSTServerTimestampTransform) to C++ * address changes * address changes * address changes * address change --- Firestore/Source/Model/FSTMutation.h | 19 +++++------- Firestore/Source/Model/FSTMutation.mm | 54 ++++++++++++----------------------- 2 files changed, 25 insertions(+), 48 deletions(-) (limited to 'Firestore/Source/Model') diff --git a/Firestore/Source/Model/FSTMutation.h b/Firestore/Source/Model/FSTMutation.h index 2b81af6..2fa8806 100644 --- a/Firestore/Source/Model/FSTMutation.h +++ b/Firestore/Source/Model/FSTMutation.h @@ -16,11 +16,13 @@ #import +#include #include #include "Firestore/core/src/firebase/firestore/model/document_key.h" #include "Firestore/core/src/firebase/firestore/model/field_mask.h" #include "Firestore/core/src/firebase/firestore/model/field_path.h" +#include "Firestore/core/src/firebase/firestore/model/transform_operations.h" @class FSTDocument; @class FSTFieldValue; @@ -33,22 +35,15 @@ NS_ASSUME_NONNULL_BEGIN #pragma mark - FSTFieldTransform -/** Represents a transform within a TransformMutation. */ -@protocol FSTTransformOperation -@end - -/** Transforms a value into a server-generated timestamp. */ -@interface FSTServerTimestampTransform : NSObject -+ (instancetype)serverTimestampTransform; -@end - -/** A field path and the FSTTransformOperation to perform upon it. */ +/** A field path and the TransformOperation to perform upon it. */ @interface FSTFieldTransform : NSObject - (instancetype)init NS_UNAVAILABLE; - (instancetype)initWithPath:(firebase::firestore::model::FieldPath)path - transform:(id)transform NS_DESIGNATED_INITIALIZER; + transform: + (std::unique_ptr)transform + NS_DESIGNATED_INITIALIZER; - (const firebase::firestore::model::FieldPath &)path; -@property(nonatomic, strong, readonly) id transform; +- (const firebase::firestore::model::TransformOperation *)transform; @end #pragma mark - FSTPrecondition diff --git a/Firestore/Source/Model/FSTMutation.mm b/Firestore/Source/Model/FSTMutation.mm index df95155..a18053c 100644 --- a/Firestore/Source/Model/FSTMutation.mm +++ b/Firestore/Source/Model/FSTMutation.mm @@ -16,6 +16,7 @@ #import "Firestore/Source/Model/FSTMutation.h" +#include #include #import "FIRTimestamp.h" @@ -29,51 +30,29 @@ #include "Firestore/core/src/firebase/firestore/model/document_key.h" #include "Firestore/core/src/firebase/firestore/model/field_mask.h" #include "Firestore/core/src/firebase/firestore/model/field_path.h" +#include "Firestore/core/src/firebase/firestore/model/transform_operations.h" using firebase::firestore::model::DocumentKey; using firebase::firestore::model::FieldMask; using firebase::firestore::model::FieldPath; +using firebase::firestore::model::ServerTimestampTransform; +using firebase::firestore::model::TransformOperation; NS_ASSUME_NONNULL_BEGIN -#pragma mark - FSTServerTimestampTransform - -@implementation FSTServerTimestampTransform - -+ (instancetype)serverTimestampTransform { - static FSTServerTimestampTransform *sharedInstance = nil; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - sharedInstance = [[FSTServerTimestampTransform alloc] init]; - }); - return sharedInstance; -} - -- (BOOL)isEqual:(id)other { - if (other == self) { - return YES; - } - return [other isKindOfClass:[FSTServerTimestampTransform class]]; -} - -- (NSUInteger)hash { - // arbitrary number since all instances are equal. - return 37; -} - -@end - #pragma mark - FSTFieldTransform @implementation FSTFieldTransform { FieldPath _path; + std::unique_ptr _transform; } -- (instancetype)initWithPath:(FieldPath)path transform:(id)transform { +- (instancetype)initWithPath:(FieldPath)path + transform:(std::unique_ptr)transform { self = [super init]; if (self) { _path = std::move(path); - _transform = transform; + _transform = std::move(transform); } return self; } @@ -82,13 +61,12 @@ NS_ASSUME_NONNULL_BEGIN if (other == self) return YES; if (![[other class] isEqual:[self class]]) return NO; FSTFieldTransform *otherFieldTransform = other; - return self.path == otherFieldTransform.path && - [self.transform isEqual:otherFieldTransform.transform]; + return self.path == otherFieldTransform.path && *self.transform == *otherFieldTransform.transform; } - (NSUInteger)hash { NSUInteger hash = self.path.Hash(); - hash = hash * 31 + [self.transform hash]; + hash = hash * 31 + self.transform->Hash(); return hash; } @@ -96,6 +74,10 @@ NS_ASSUME_NONNULL_BEGIN return _path; } +- (const firebase::firestore::model::TransformOperation *)transform { + return _transform.get(); +} + @end #pragma mark - FSTPrecondition @@ -505,7 +487,7 @@ NS_ASSUME_NONNULL_BEGIN writeTime:(FIRTimestamp *)localWriteTime { NSMutableArray *transformResults = [NSMutableArray array]; for (FSTFieldTransform *fieldTransform in self.fieldTransforms) { - if ([fieldTransform.transform isKindOfClass:[FSTServerTimestampTransform class]]) { + if (fieldTransform.transform->type() == TransformOperation::Type::ServerTimestamp) { FSTFieldValue *previousValue = nil; if ([baseDocument isMemberOfClass:[FSTDocument class]]) { @@ -529,12 +511,12 @@ NS_ASSUME_NONNULL_BEGIN for (NSUInteger i = 0; i < self.fieldTransforms.count; i++) { FSTFieldTransform *fieldTransform = self.fieldTransforms[i]; - id transform = fieldTransform.transform; + const TransformOperation *transform = fieldTransform.transform; FieldPath fieldPath = fieldTransform.path; - if ([transform isKindOfClass:[FSTServerTimestampTransform class]]) { + if (transform->type() == TransformOperation::Type::ServerTimestamp) { objectValue = [objectValue objectBySettingValue:transformResults[i] forPath:fieldPath]; } else { - FSTFail(@"Encountered unknown transform: %@", transform); + FSTFail(@"Encountered unknown transform: %d type", transform->type()); } } return objectValue; -- cgit v1.2.3