aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Source/Model/FSTFieldValue.mm
diff options
context:
space:
mode:
Diffstat (limited to 'Firestore/Source/Model/FSTFieldValue.mm')
-rw-r--r--Firestore/Source/Model/FSTFieldValue.mm102
1 files changed, 67 insertions, 35 deletions
diff --git a/Firestore/Source/Model/FSTFieldValue.mm b/Firestore/Source/Model/FSTFieldValue.mm
index 2f013c3..80bd11f 100644
--- a/Firestore/Source/Model/FSTFieldValue.mm
+++ b/Firestore/Source/Model/FSTFieldValue.mm
@@ -48,28 +48,35 @@ NS_ASSUME_NONNULL_BEGIN
@implementation FSTFieldValueOptions
-+ (instancetype)optionsForSnapshotOptions:(FIRSnapshotOptions *)options {
- if (options.serverTimestampBehavior == FSTServerTimestampBehaviorNone) {
- static FSTFieldValueOptions *defaultInstance = nil;
- static dispatch_once_t onceToken;
-
- dispatch_once(&onceToken, ^{
- defaultInstance = [[FSTFieldValueOptions alloc]
- initWithServerTimestampBehavior:FSTServerTimestampBehaviorNone];
- });
- return defaultInstance;
- } else {
- return [[FSTFieldValueOptions alloc]
- initWithServerTimestampBehavior:options.serverTimestampBehavior];
++ (instancetype)optionsForSnapshotOptions:(FIRSnapshotOptions *)options
+ timestampsInSnapshotsEnabled:(BOOL)timestampsInSnapshotsEnabled {
+ FSTServerTimestampBehavior convertedServerTimestampBehavior = FSTServerTimestampBehaviorNone;
+ switch (options.serverTimestampBehavior) {
+ case FIRServerTimestampBehaviorNone:
+ convertedServerTimestampBehavior = FSTServerTimestampBehaviorNone;
+ break;
+ case FIRServerTimestampBehaviorEstimate:
+ convertedServerTimestampBehavior = FSTServerTimestampBehaviorEstimate;
+ break;
+ case FIRServerTimestampBehaviorPrevious:
+ convertedServerTimestampBehavior = FSTServerTimestampBehaviorPrevious;
+ break;
+ default:
+ FSTFail(@"Unexpected server timestamp option: %ld", (long)options.serverTimestampBehavior);
}
+
+ return
+ [[FSTFieldValueOptions alloc] initWithServerTimestampBehavior:convertedServerTimestampBehavior
+ timestampsInSnapshotsEnabled:timestampsInSnapshotsEnabled];
}
-- (instancetype)initWithServerTimestampBehavior:
- (FSTServerTimestampBehavior)serverTimestampBehavior {
+- (instancetype)initWithServerTimestampBehavior:(FSTServerTimestampBehavior)serverTimestampBehavior
+ timestampsInSnapshotsEnabled:(BOOL)timestampsInSnapshotsEnabled {
self = [super init];
if (self) {
_serverTimestampBehavior = serverTimestampBehavior;
+ _timestampsInSnapshotsEnabled = timestampsInSnapshotsEnabled;
}
return self;
}
@@ -89,12 +96,11 @@ NS_ASSUME_NONNULL_BEGIN
}
- (id)value {
- return [self valueWithOptions:[FSTFieldValueOptions
- optionsForSnapshotOptions:[FIRSnapshotOptions defaultOptions]]];
+ @throw FSTAbstractMethodException(); // NOLINT
}
- (id)valueWithOptions:(FSTFieldValueOptions *)options {
- @throw FSTAbstractMethodException(); // NOLINT
+ return [self value];
}
- (BOOL)isEqual:(id)other {
@@ -143,7 +149,7 @@ NS_ASSUME_NONNULL_BEGIN
return FSTTypeOrderNull;
}
-- (id)valueWithOptions:(FSTFieldValueOptions *)options {
+- (id)value {
return [NSNull null];
}
@@ -209,7 +215,7 @@ NS_ASSUME_NONNULL_BEGIN
return FSTTypeOrderBoolean;
}
-- (id)valueWithOptions:(FSTFieldValueOptions *)options {
+- (id)value {
return self.internalValue ? @YES : @NO;
}
@@ -290,7 +296,7 @@ NS_ASSUME_NONNULL_BEGIN
return self;
}
-- (id)valueWithOptions:(FSTFieldValueOptions *)options {
+- (id)value {
return @(self.internalValue);
}
@@ -342,7 +348,7 @@ NS_ASSUME_NONNULL_BEGIN
return self;
}
-- (id)valueWithOptions:(FSTFieldValueOptions *)options {
+- (id)value {
return @(self.internalValue);
}
@@ -400,7 +406,7 @@ struct Comparator<NSString *> {
return FSTTypeOrderString;
}
-- (id)valueWithOptions:(FSTFieldValueOptions *)options {
+- (id)value {
return self.internalValue;
}
@@ -447,9 +453,16 @@ struct Comparator<NSString *> {
return FSTTypeOrderTimestamp;
}
+- (id)value {
+ return self.internalValue;
+}
+
- (id)valueWithOptions:(FSTFieldValueOptions *)options {
- // For developers, we expose Timestamps as Dates.
- return self.internalValue.approximateDateValue;
+ if (options.timestampsInSnapshotsEnabled) {
+ return self.value;
+ } else {
+ return [self.value dateValue];
+ }
}
- (BOOL)isEqual:(id)other {
@@ -473,7 +486,6 @@ struct Comparator<NSString *> {
}
@end
-
#pragma mark - FSTServerTimestampValue
@implementation FSTServerTimestampValue
@@ -498,16 +510,20 @@ struct Comparator<NSString *> {
return FSTTypeOrderTimestamp;
}
+- (id)value {
+ return [NSNull null];
+}
+
- (id)valueWithOptions:(FSTFieldValueOptions *)options {
switch (options.serverTimestampBehavior) {
case FSTServerTimestampBehaviorNone:
return [NSNull null];
case FSTServerTimestampBehaviorEstimate:
- return [self.localWriteTime approximateDateValue];
+ return [[FSTTimestampValue timestampValue:self.localWriteTime] valueWithOptions:options];
case FSTServerTimestampBehaviorPrevious:
return self.previousValue ? [self.previousValue valueWithOptions:options] : [NSNull null];
default:
- FSTFail(@"Unexpected server timestamp option: %d", (int)options.serverTimestampBehavior);
+ FSTFail(@"Unexpected server timestamp option: %ld", (long)options.serverTimestampBehavior);
}
}
@@ -561,7 +577,7 @@ struct Comparator<NSString *> {
return FSTTypeOrderGeoPoint;
}
-- (id)valueWithOptions:(FSTFieldValueOptions *)options {
+- (id)value {
return self.internalValue;
}
@@ -583,7 +599,6 @@ struct Comparator<NSString *> {
}
@end
-
#pragma mark - FSTBlobValue
static NSComparisonResult CompareBytes(NSData *left, NSData *right) {
@@ -625,7 +640,7 @@ static NSComparisonResult CompareBytes(NSData *left, NSData *right) {
return FSTTypeOrderBlob;
}
-- (id)valueWithOptions:(FSTFieldValueOptions *)options {
+- (id)value {
return self.internalValue;
}
@@ -669,7 +684,7 @@ static NSComparisonResult CompareBytes(NSData *left, NSData *right) {
return self;
}
-- (id)valueWithOptions:(FSTFieldValueOptions *)options {
+- (id)value {
return self.key;
}
@@ -753,6 +768,15 @@ static const NSComparator StringComparator = ^NSComparisonResult(NSString *left,
return [self initWithImmutableDictionary:dictionary];
}
+- (id)value {
+ NSMutableDictionary *result = [NSMutableDictionary dictionary];
+ [self.internalValue
+ enumerateKeysAndObjectsUsingBlock:^(NSString *key, FSTFieldValue *obj, BOOL *stop) {
+ result[key] = [obj value];
+ }];
+ return result;
+}
+
- (id)valueWithOptions:(FSTFieldValueOptions *)options {
NSMutableDictionary *result = [NSMutableDictionary dictionary];
[self.internalValue
@@ -832,8 +856,8 @@ static const NSComparator StringComparator = ^NSComparisonResult(NSString *left,
// Recursive base case:
return [self objectBySettingValue:value forField:childName];
} else {
- // Nested path. Recursively generate a new sub-object and then wrap a new FSTObjectValue around
- // the result.
+ // Nested path. Recursively generate a new sub-object and then wrap a new FSTObjectValue
+ // around the result.
FSTFieldValue *child = [_internalValue objectForKey:childName];
FSTObjectValue *childObject;
if ([child isKindOfClass:[FSTObjectValue class]]) {
@@ -908,7 +932,7 @@ static const NSComparator StringComparator = ^NSComparisonResult(NSString *left,
return [self.internalValue hash];
}
-- (id)valueWithOptions:(FSTFieldValueOptions *)options {
+- (id)value {
NSMutableArray *result = [NSMutableArray arrayWithCapacity:_internalValue.count];
[self.internalValue enumerateObjectsUsingBlock:^(FSTFieldValue *obj, NSUInteger idx, BOOL *stop) {
[result addObject:[obj value]];
@@ -916,6 +940,14 @@ static const NSComparator StringComparator = ^NSComparisonResult(NSString *left,
return result;
}
+- (id)valueWithOptions:(FSTFieldValueOptions *)options {
+ NSMutableArray *result = [NSMutableArray arrayWithCapacity:_internalValue.count];
+ [self.internalValue enumerateObjectsUsingBlock:^(FSTFieldValue *obj, NSUInteger idx, BOOL *stop) {
+ [result addObject:[obj valueWithOptions:options]];
+ }];
+ return result;
+}
+
- (FSTTypeOrder)typeOrder {
return FSTTypeOrderArray;
}