aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Source/Model
diff options
context:
space:
mode:
authorGravatar zxu <zxu@google.com>2018-04-05 11:52:07 -0400
committerGravatar GitHub <noreply@github.com>2018-04-05 11:52:07 -0400
commitdb717bf6704b444b093d46f53935402c83441854 (patch)
tree11f69c3ba737c7509f620f3660d6c8d6057fc2d7 /Firestore/Source/Model
parent7257c0d22727a7e94867992aa2f1a3e283dc6bb7 (diff)
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
Diffstat (limited to 'Firestore/Source/Model')
-rw-r--r--Firestore/Source/Model/FSTMutation.h19
-rw-r--r--Firestore/Source/Model/FSTMutation.mm54
2 files changed, 25 insertions, 48 deletions
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 <Foundation/Foundation.h>
+#include <memory>
#include <vector>
#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 <NSObject>
-@end
-
-/** Transforms a value into a server-generated timestamp. */
-@interface FSTServerTimestampTransform : NSObject <FSTTransformOperation>
-+ (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<FSTTransformOperation>)transform NS_DESIGNATED_INITIALIZER;
+ transform:
+ (std::unique_ptr<firebase::firestore::model::TransformOperation>)transform
+ NS_DESIGNATED_INITIALIZER;
- (const firebase::firestore::model::FieldPath &)path;
-@property(nonatomic, strong, readonly) id<FSTTransformOperation> 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 <memory>
#include <utility>
#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<TransformOperation> _transform;
}
-- (instancetype)initWithPath:(FieldPath)path transform:(id<FSTTransformOperation>)transform {
+- (instancetype)initWithPath:(FieldPath)path
+ transform:(std::unique_ptr<TransformOperation>)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<FSTFieldValue *> *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<FSTTransformOperation> 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;