Skip to content

Commit 34fcd5a

Browse files
authored
Migrate FSTTimestampValue to FSTDelegateValue (#3170)
1 parent 12b7397 commit 34fcd5a

File tree

8 files changed

+37
-80
lines changed

8 files changed

+37
-80
lines changed

Firestore/Example/Tests/API/FSTUserDataConverterTests.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ - (void)testConvertsDates {
109109
NSArray *values = @[ FSTTestDate(1900, 12, 1, 1, 20, 30), FSTTestDate(2017, 4, 24, 13, 20, 30) ];
110110
for (id value in values) {
111111
FSTFieldValue *wrapped = FSTTestFieldValue(value);
112-
XCTAssertEqualObjects([wrapped class], [FSTTimestampValue class]);
112+
XCTAssertEqualObjects([wrapped class], [FSTDelegateValue class]);
113113
XCTAssertEqualObjects([[wrapped value] class], [FIRTimestamp class]);
114114
XCTAssertEqualObjects([wrapped value], [FIRTimestamp timestampWithDate:value]);
115115
XCTAssertEqual(wrapped.type, FieldValue::Type::Timestamp);

Firestore/Example/Tests/Model/FSTFieldValueTests.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ - (void)testValueEquality {
307307
@[ FSTTestFieldValue(@"strin") ],
308308
@[ FSTTestFieldValue(@"e\u0301b") ], // latin small letter e + combining acute accent
309309
@[ FSTTestFieldValue(@"\u00e9a") ], // latin small letter e with acute accent
310-
@[ FSTTestFieldValue(date1), [FSTTimestampValue timestampValue:MakeTimestamp(date1)] ],
310+
@[ FSTTestFieldValue(date1), FieldValue::FromTimestamp(MakeTimestamp(date1)).Wrap() ],
311311
@[ FSTTestFieldValue(date2) ],
312312
@[
313313
// NOTE: ServerTimestampValues can't be parsed via FSTTestFieldValue().

Firestore/Example/Tests/Model/FSTMutationTests.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,9 @@ - (void)testAppliesServerAckedServerTimestampTransformToDocuments {
443443
FSTMutation *transform = FSTTestTransformMutation(
444444
@"collection/key", @{@"foo.bar" : [FIRFieldValue fieldValueForServerTimestamp]});
445445

446-
FSTMutationResult *mutationResult = [[FSTMutationResult alloc]
447-
initWithVersion:testutil::Version(1)
448-
transformResults:@[ [FSTTimestampValue timestampValue:_timestamp] ]];
446+
FSTMutationResult *mutationResult =
447+
[[FSTMutationResult alloc] initWithVersion:testutil::Version(1)
448+
transformResults:@[ FieldValue::FromTimestamp(_timestamp).Wrap() ]];
449449

450450
FIRTimestamp *publicTimestamp = api::MakeFIRTimestamp(_timestamp);
451451
FSTMaybeDocument *transformedDoc = [transform applyToRemoteDocument:baseDoc

Firestore/Source/API/FSTUserDataConverter.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,12 @@ - (nullable FSTFieldValue *)parseScalarValue:(nullable id)input context:(ParseCo
453453

454454
} else if ([input isKindOfClass:[NSDate class]]) {
455455
NSDate *inputDate = input;
456-
return [FSTTimestampValue timestampValue:api::MakeTimestamp(inputDate)];
456+
return FieldValue::FromTimestamp(api::MakeTimestamp(inputDate)).Wrap();
457457

458458
} else if ([input isKindOfClass:[FIRTimestamp class]]) {
459459
FIRTimestamp *inputTimestamp = input;
460460
Timestamp timestamp = TimestampInternal::Truncate(api::MakeTimestamp(inputTimestamp));
461-
return [FSTTimestampValue timestampValue:timestamp];
461+
return FieldValue::FromTimestamp(timestamp).Wrap();
462462

463463
} else if ([input isKindOfClass:[FIRGeoPoint class]]) {
464464
return FieldValue::FromGeoPoint(api::MakeGeoPoint(input)).Wrap();

Firestore/Source/Model/FSTFieldValue.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,6 @@ typedef NS_ENUM(NSInteger, FSTTypeOrder) {
103103

104104
@end
105105

106-
/**
107-
* A timestamp value stored in Firestore.
108-
*/
109-
@interface FSTTimestampValue : FSTFieldValue <FIRTimestamp *>
110-
+ (instancetype)timestampValue:(const firebase::Timestamp &)value;
111-
@end
112-
113106
/**
114107
* Represents a locally-applied Server Timestamp.
115108
*

Firestore/Source/Model/FSTFieldValue.mm

Lines changed: 29 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#import "FIRTimestamp.h"
2424

2525
#import "Firestore/Source/API/FIRGeoPoint+Internal.h"
26+
#import "Firestore/Source/API/FIRTimestamp+Internal.h"
2627
#import "Firestore/Source/API/converters.h"
2728
#import "Firestore/Source/Model/FSTDocumentKey.h"
2829
#import "Firestore/Source/Util/FSTClasses.h"
@@ -127,66 +128,6 @@ - (double)doubleValue {
127128

128129
@end
129130

130-
#pragma mark - FSTTimestampValue
131-
132-
@implementation FSTTimestampValue {
133-
Timestamp _internalValue;
134-
}
135-
136-
+ (instancetype)timestampValue:(const Timestamp &)value {
137-
return [[FSTTimestampValue alloc] initWithValue:value];
138-
}
139-
140-
- (id)initWithValue:(const Timestamp &)value {
141-
self = [super init];
142-
if (self) {
143-
_internalValue = value;
144-
}
145-
return self;
146-
}
147-
148-
- (FieldValue::Type)type {
149-
return FieldValue::Type::Timestamp;
150-
}
151-
152-
- (FSTTypeOrder)typeOrder {
153-
return FSTTypeOrderTimestamp;
154-
}
155-
156-
- (id)value {
157-
return MakeFIRTimestamp(_internalValue);
158-
}
159-
160-
- (id)valueWithOptions:(const FieldValueOptions &)options {
161-
if (options.timestamps_in_snapshots_enabled()) {
162-
return self.value;
163-
} else {
164-
return [self.value dateValue];
165-
}
166-
}
167-
168-
- (BOOL)isEqual:(id)other {
169-
return [other isKindOfClass:[FSTFieldValue class]] &&
170-
((FSTFieldValue *)other).type == FieldValue::Type::Timestamp &&
171-
_internalValue == ((FSTTimestampValue *)other)->_internalValue;
172-
}
173-
174-
- (NSUInteger)hash {
175-
return TimestampInternal::Hash(_internalValue);
176-
}
177-
178-
- (NSComparisonResult)compare:(FSTFieldValue *)other {
179-
if (other.type == FieldValue::Type::Timestamp) {
180-
return WrapCompare(_internalValue, ((FSTTimestampValue *)other)->_internalValue);
181-
} else if (other.type == FieldValue::Type::ServerTimestamp) {
182-
// Concrete timestamps come before server timestamps.
183-
return NSOrderedAscending;
184-
} else {
185-
return [self defaultCompare:other];
186-
}
187-
}
188-
189-
@end
190131
#pragma mark - FSTServerTimestampValue
191132

192133
@implementation FSTServerTimestampValue {
@@ -226,7 +167,7 @@ - (id)valueWithOptions:(const FieldValueOptions &)options {
226167
case ServerTimestampBehavior::kNone:
227168
return [NSNull null];
228169
case ServerTimestampBehavior::kEstimate:
229-
return [[FSTTimestampValue timestampValue:self.localWriteTime] valueWithOptions:options];
170+
return [FieldValue::FromTimestamp(self.localWriteTime).Wrap() valueWithOptions:options];
230171
case ServerTimestampBehavior::kPrevious:
231172
return self.previousValue ? [self.previousValue valueWithOptions:options] : [NSNull null];
232173
default:
@@ -683,7 +624,11 @@ - (id)value {
683624
return @(self.internalValue.integer_value());
684625
case FieldValue::Type::Double:
685626
return @(self.internalValue.double_value());
686-
case FieldValue::Type::Timestamp:
627+
case FieldValue::Type::Timestamp: {
628+
auto timestamp = self.internalValue.timestamp_value();
629+
return [[FIRTimestamp alloc] initWithSeconds:timestamp.seconds()
630+
nanoseconds:timestamp.nanoseconds()];
631+
}
687632
case FieldValue::Type::ServerTimestamp:
688633
HARD_FAIL("TODO(rsgowman): implement");
689634
case FieldValue::Type::String:
@@ -701,6 +646,20 @@ - (id)value {
701646
UNREACHABLE();
702647
}
703648

649+
- (id)valueWithOptions:(const model::FieldValueOptions &)options {
650+
switch (self.internalValue.type()) {
651+
case FieldValue::Type::Timestamp:
652+
if (options.timestamps_in_snapshots_enabled()) {
653+
return [self value];
654+
} else {
655+
return [[self value] dateValue];
656+
}
657+
658+
default:
659+
return [self value];
660+
}
661+
}
662+
704663
- (NSComparisonResult)compare:(FSTFieldValue *)other {
705664
// TODO(rsgowman): Port the other FST*Value's, and then remove this comment:
706665
//
@@ -710,8 +669,14 @@ - (NSComparisonResult)compare:(FSTFieldValue *)other {
710669
// FSTDelegateValue handles (eg) booleans to ensure this case never occurs.
711670

712671
if (FieldValue::Comparable(self.type, other.type)) {
713-
HARD_ASSERT([other isKindOfClass:[FSTDelegateValue class]]);
714-
return WrapCompare<FieldValue>(self.internalValue, ((FSTDelegateValue *)other).internalValue);
672+
if ([other isKindOfClass:[FSTServerTimestampValue class]]) {
673+
HARD_ASSERT(self.type == FieldValue::Type::Timestamp);
674+
// Server timestamps come after all concrete timestamps.
675+
return NSOrderedAscending;
676+
} else {
677+
HARD_ASSERT([other isKindOfClass:[FSTDelegateValue class]]);
678+
return WrapCompare<FieldValue>(self.internalValue, ((FSTDelegateValue *)other).internalValue);
679+
}
715680
} else {
716681
return [self defaultCompare:other];
717682
}

Firestore/Source/Remote/FSTSerializerBeta.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ - (FSTFieldValue *)decodedFieldValue:(GCFSValue *)valueProto {
264264

265265
case GCFSValue_ValueType_OneOfCase_TimestampValue: {
266266
Timestamp value = [self decodedTimestamp:valueProto.timestampValue];
267-
return [FSTTimestampValue timestampValue:value];
267+
return FieldValue::FromTimestamp(value).Wrap();
268268
}
269269

270270
case GCFSValue_ValueType_OneOfCase_GeoPointValue:

Firestore/core/test/firebase/firestore/testutil/xcgmock.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ OBJC_PRINT_TO(FSTSetMutation);
192192
OBJC_PRINT_TO(FSTSortOrder);
193193
OBJC_PRINT_TO(FSTStringValue);
194194
OBJC_PRINT_TO(FSTSyncEngine);
195-
OBJC_PRINT_TO(FSTTimestampValue);
196195
OBJC_PRINT_TO(FSTTransformMutation);
197196
OBJC_PRINT_TO(FSTUnknownDocument);
198197
OBJC_PRINT_TO(FSTUserDataConverter);

0 commit comments

Comments
 (0)