Skip to content

Commit 7ae2af4

Browse files
authored
Port FSTFieldValueOptions to C++ (#2989)
1 parent 03c2e72 commit 7ae2af4

File tree

6 files changed

+88
-68
lines changed

6 files changed

+88
-68
lines changed

Firestore/Source/API/FIRDocumentSnapshot.mm

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
3535
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
3636
#include "Firestore/core/src/firebase/firestore/model/field_value.h"
37+
#include "Firestore/core/src/firebase/firestore/model/field_value_options.h"
3738
#include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
3839
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"
3940

@@ -45,6 +46,8 @@
4546
using firebase::firestore::model::DatabaseId;
4647
using firebase::firestore::model::DocumentKey;
4748
using firebase::firestore::model::FieldValue;
49+
using firebase::firestore::model::FieldValueOptions;
50+
using firebase::firestore::model::ServerTimestampBehavior;
4851
using firebase::firestore::util::WrapNSString;
4952

5053
NS_ASSUME_NONNULL_BEGIN
@@ -57,11 +60,11 @@
5760
ServerTimestampBehavior InternalServerTimestampBehavior(FIRServerTimestampBehavior behavior) {
5861
switch (behavior) {
5962
case FIRServerTimestampBehaviorNone:
60-
return ServerTimestampBehavior::None;
63+
return ServerTimestampBehavior::kNone;
6164
case FIRServerTimestampBehaviorEstimate:
62-
return ServerTimestampBehavior::Estimate;
65+
return ServerTimestampBehavior::kEstimate;
6366
case FIRServerTimestampBehaviorPrevious:
64-
return ServerTimestampBehavior::Previous;
67+
return ServerTimestampBehavior::kPrevious;
6568
default:
6669
HARD_FAIL("Unexpected server timestamp option: %s", behavior);
6770
}
@@ -147,7 +150,7 @@ - (FIRSnapshotMetadata *)metadata {
147150

148151
- (nullable NSDictionary<NSString *, id> *)dataWithServerTimestampBehavior:
149152
(FIRServerTimestampBehavior)serverTimestampBehavior {
150-
FSTFieldValueOptions *options = [self optionsForServerTimestampBehavior:serverTimestampBehavior];
153+
FieldValueOptions options = [self optionsForServerTimestampBehavior:serverTimestampBehavior];
151154
FSTObjectValue *data = _snapshot.GetData();
152155
return data == nil ? nil : [self convertedObject:data options:options];
153156
}
@@ -168,26 +171,23 @@ - (nullable id)valueForField:(id)field
168171
}
169172

170173
FSTFieldValue *fieldValue = _snapshot.GetValue(fieldPath.internalValue);
171-
FSTFieldValueOptions *options = [self optionsForServerTimestampBehavior:serverTimestampBehavior];
174+
FieldValueOptions options = [self optionsForServerTimestampBehavior:serverTimestampBehavior];
172175
return fieldValue == nil ? nil : [self convertedValue:fieldValue options:options];
173176
}
174177

175178
- (nullable id)objectForKeyedSubscript:(id)key {
176179
return [self valueForField:key];
177180
}
178181

179-
- (FSTFieldValueOptions *)optionsForServerTimestampBehavior:
182+
- (FieldValueOptions)optionsForServerTimestampBehavior:
180183
(FIRServerTimestampBehavior)serverTimestampBehavior {
181184
SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN()
182-
return [[FSTFieldValueOptions alloc]
183-
initWithServerTimestampBehavior:InternalServerTimestampBehavior(serverTimestampBehavior)
184-
timestampsInSnapshotsEnabled:_snapshot.firestore()
185-
->settings()
186-
.timestamps_in_snapshots_enabled()];
185+
return FieldValueOptions(InternalServerTimestampBehavior(serverTimestampBehavior),
186+
_snapshot.firestore()->settings().timestamps_in_snapshots_enabled());
187187
SUPPRESS_END()
188188
}
189189

190-
- (id)convertedValue:(FSTFieldValue *)value options:(FSTFieldValueOptions *)options {
190+
- (id)convertedValue:(FSTFieldValue *)value options:(const FieldValueOptions &)options {
191191
if (value.type == FieldValue::Type::Object) {
192192
return [self convertedObject:(FSTObjectValue *)value options:options];
193193
} else if (value.type == FieldValue::Type::Array) {
@@ -213,7 +213,7 @@ - (id)convertedValue:(FSTFieldValue *)value options:(FSTFieldValueOptions *)opti
213213
}
214214

215215
- (NSDictionary<NSString *, id> *)convertedObject:(FSTObjectValue *)objectValue
216-
options:(FSTFieldValueOptions *)options {
216+
options:(const FieldValueOptions &)options {
217217
NSMutableDictionary *result = [NSMutableDictionary dictionary];
218218
[objectValue.internalValue
219219
enumerateKeysAndObjectsUsingBlock:^(NSString *key, FSTFieldValue *value, BOOL *stop) {
@@ -223,7 +223,7 @@ - (id)convertedValue:(FSTFieldValue *)value options:(FSTFieldValueOptions *)opti
223223
}
224224

225225
- (NSArray<id> *)convertedArray:(FSTArrayValue *)arrayValue
226-
options:(FSTFieldValueOptions *)options {
226+
options:(const FieldValueOptions &)options {
227227
NSArray<FSTFieldValue *> *internalValue = arrayValue.internalValue;
228228
NSMutableArray *result = [NSMutableArray arrayWithCapacity:internalValue.count];
229229
[internalValue enumerateObjectsUsingBlock:^(id value, NSUInteger idx, BOOL *stop) {

Firestore/Source/Model/FSTFieldValue.h

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
#include "Firestore/core/src/firebase/firestore/model/field_mask.h"
2424
#include "Firestore/core/src/firebase/firestore/model/field_path.h"
2525
#include "Firestore/core/src/firebase/firestore/model/field_value.h"
26+
#include "Firestore/core/src/firebase/firestore/model/field_value_options.h"
2627

2728
@class FIRTimestamp;
28-
@class FSTFieldValueOptions;
2929
@class FIRGeoPoint;
3030

3131
namespace model = firebase::firestore::model;
@@ -46,28 +46,6 @@ typedef NS_ENUM(NSInteger, FSTTypeOrder) {
4646
FSTTypeOrderObject,
4747
};
4848

49-
/** Defines the return value for pending server timestamps. */
50-
enum class ServerTimestampBehavior { None, Estimate, Previous };
51-
52-
/** Holds properties that define field value deserialization options. */
53-
@interface FSTFieldValueOptions : NSObject
54-
55-
@property(nonatomic, readonly, assign) ServerTimestampBehavior serverTimestampBehavior;
56-
57-
@property(nonatomic) BOOL timestampsInSnapshotsEnabled;
58-
59-
- (instancetype)init NS_UNAVAILABLE;
60-
61-
/**
62-
* Creates an FSTFieldValueOptions instance that specifies deserialization behavior for pending
63-
* server timestamps.
64-
*/
65-
- (instancetype)initWithServerTimestampBehavior:(ServerTimestampBehavior)serverTimestampBehavior
66-
timestampsInSnapshotsEnabled:(BOOL)timestampsInSnapshotsEnabled
67-
NS_DESIGNATED_INITIALIZER;
68-
69-
@end
70-
7149
/**
7250
* Abstract base class representing an immutable data value as stored in Firestore. FSTFieldValue
7351
* represents all the different kinds of values that can be stored in fields in a document.
@@ -111,7 +89,7 @@ enum class ServerTimestampBehavior { None, Estimate, Previous };
11189
* Options can be provided to configure the deserialization of some field values (such as server
11290
* timestamps).
11391
*/
114-
- (T)valueWithOptions:(FSTFieldValueOptions *)options;
92+
- (T)valueWithOptions:(const model::FieldValueOptions &)options;
11593

11694
/** Compares against another FSTFieldValue. */
11795
- (NSComparisonResult)compare:(FSTFieldValue *)other;
@@ -164,7 +142,7 @@ enum class ServerTimestampBehavior { None, Estimate, Previous };
164142
* (see [FSTTransformMutation applyTo]). They can only exist in the local view of a document.
165143
* Therefore they do not need to be parsed or serialized.
166144
* - When evaluated locally (e.g. via FSTDocumentSnapshot data), they by default evaluate to NSNull.
167-
* This behavior can be configured by passing custom FSTFieldValueOptions to `valueWithOptions:`.
145+
* This behavior can be configured by passing custom FieldValueOptions to `valueWithOptions:`.
168146
* - They sort after all FSTTimestampValues. With respect to other FSTServerTimestampValues, they
169147
* sort by their localWriteTime.
170148
*/

Firestore/Source/Model/FSTFieldValue.mm

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
using firebase::firestore::model::FieldMask;
3939
using firebase::firestore::model::FieldPath;
4040
using firebase::firestore::model::FieldValue;
41+
using firebase::firestore::model::FieldValueOptions;
42+
using firebase::firestore::model::ServerTimestampBehavior;
4143
using firebase::firestore::util::Comparator;
4244
using firebase::firestore::util::CompareMixedNumber;
4345
using firebase::firestore::util::DoubleBitwiseEquals;
@@ -48,23 +50,6 @@
4850

4951
NS_ASSUME_NONNULL_BEGIN
5052

51-
#pragma mark - FSTFieldValueOptions
52-
53-
@implementation FSTFieldValueOptions
54-
55-
- (instancetype)initWithServerTimestampBehavior:(ServerTimestampBehavior)serverTimestampBehavior
56-
timestampsInSnapshotsEnabled:(BOOL)timestampsInSnapshotsEnabled {
57-
self = [super init];
58-
59-
if (self) {
60-
_serverTimestampBehavior = serverTimestampBehavior;
61-
_timestampsInSnapshotsEnabled = timestampsInSnapshotsEnabled;
62-
}
63-
return self;
64-
}
65-
66-
@end
67-
6853
#pragma mark - FSTFieldValue
6954

7055
@interface FSTFieldValue ()
@@ -88,7 +73,7 @@ - (id)value {
8873
@throw FSTAbstractMethodException(); // NOLINT
8974
}
9075

91-
- (id)valueWithOptions:(FSTFieldValueOptions *)options {
76+
- (id)valueWithOptions:(const FieldValueOptions &)options {
9277
return [self value];
9378
}
9479

@@ -337,8 +322,8 @@ - (id)value {
337322
return self.internalValue;
338323
}
339324

340-
- (id)valueWithOptions:(FSTFieldValueOptions *)options {
341-
if (options.timestampsInSnapshotsEnabled) {
325+
- (id)valueWithOptions:(const FieldValueOptions &)options {
326+
if (options.timestamps_in_snapshots_enabled()) {
342327
return self.value;
343328
} else {
344329
return [self.value dateValue];
@@ -399,16 +384,16 @@ - (id)value {
399384
return [NSNull null];
400385
}
401386

402-
- (id)valueWithOptions:(FSTFieldValueOptions *)options {
403-
switch (options.serverTimestampBehavior) {
404-
case ServerTimestampBehavior::None:
387+
- (id)valueWithOptions:(const FieldValueOptions &)options {
388+
switch (options.server_timestamp_behavior()) {
389+
case ServerTimestampBehavior::kNone:
405390
return [NSNull null];
406-
case ServerTimestampBehavior::Estimate:
391+
case ServerTimestampBehavior::kEstimate:
407392
return [[FSTTimestampValue timestampValue:self.localWriteTime] valueWithOptions:options];
408-
case ServerTimestampBehavior::Previous:
393+
case ServerTimestampBehavior::kPrevious:
409394
return self.previousValue ? [self.previousValue valueWithOptions:options] : [NSNull null];
410395
default:
411-
HARD_FAIL("Unexpected server timestamp option: %s", options.serverTimestampBehavior);
396+
HARD_FAIL("Unexpected server timestamp option: %s", options.server_timestamp_behavior());
412397
}
413398
}
414399

@@ -680,7 +665,7 @@ - (id)value {
680665
return result;
681666
}
682667

683-
- (id)valueWithOptions:(FSTFieldValueOptions *)options {
668+
- (id)valueWithOptions:(const FieldValueOptions &)options {
684669
NSMutableDictionary *result = [NSMutableDictionary dictionary];
685670
[self.internalValue
686671
enumerateKeysAndObjectsUsingBlock:^(NSString *key, FSTFieldValue *obj, BOOL *stop) {
@@ -863,7 +848,7 @@ - (id)value {
863848
return result;
864849
}
865850

866-
- (id)valueWithOptions:(FSTFieldValueOptions *)options {
851+
- (id)valueWithOptions:(const FieldValueOptions &)options {
867852
NSMutableArray *result = [NSMutableArray arrayWithCapacity:_internalValue.count];
868853
[self.internalValue enumerateObjectsUsingBlock:^(FSTFieldValue *obj, NSUInteger idx, BOOL *stop) {
869854
[result addObject:[obj valueWithOptions:options]];

Firestore/core/src/firebase/firestore/model/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ cc_library(
2929
field_transform.h
3030
field_value.cc
3131
field_value.h
32+
field_value_options.h
3233
maybe_document.cc
3334
maybe_document.h
3435
mutation.cc
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2019 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_FIELD_VALUE_OPTIONS_H_
18+
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_FIELD_VALUE_OPTIONS_H_
19+
20+
namespace firebase {
21+
namespace firestore {
22+
namespace model {
23+
24+
/** Defines the return value for pending server timestamps. */
25+
enum class ServerTimestampBehavior { kNone, kEstimate, kPrevious };
26+
27+
/** Holds properties that define field value deserialization options. */
28+
class FieldValueOptions {
29+
public:
30+
/**
31+
* Creates an FieldValueOptions instance that specifies deserialization
32+
* behavior for pending server timestamps.
33+
*/
34+
FieldValueOptions(ServerTimestampBehavior server_timestamp_behavior,
35+
bool timestamps_in_snapshots_enabled)
36+
: server_timestamp_behavior_(server_timestamp_behavior),
37+
timestamps_in_snapshots_enabled_(timestamps_in_snapshots_enabled) {
38+
}
39+
40+
ServerTimestampBehavior server_timestamp_behavior() const {
41+
return server_timestamp_behavior_;
42+
}
43+
44+
bool timestamps_in_snapshots_enabled() const {
45+
return timestamps_in_snapshots_enabled_;
46+
}
47+
48+
private:
49+
ServerTimestampBehavior server_timestamp_behavior_;
50+
bool timestamps_in_snapshots_enabled_;
51+
};
52+
53+
} // namespace model
54+
} // namespace firestore
55+
} // namespace firebase
56+
57+
#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_FIELD_VALUE_OPTIONS_H_

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ OBJC_PRINT_TO(FSTDocumentSet);
157157
OBJC_PRINT_TO(FSTDoubleValue);
158158
OBJC_PRINT_TO(FSTEventManager);
159159
OBJC_PRINT_TO(FSTFieldValue);
160-
OBJC_PRINT_TO(FSTFieldValueOptions);
161160
OBJC_PRINT_TO(FSTFilter);
162161
OBJC_PRINT_TO(FSTFirestoreClient);
163162
OBJC_PRINT_TO(FSTFirestoreComponent);

0 commit comments

Comments
 (0)