From a25d05487435d397f3b8cd399ee8355eae497f0d Mon Sep 17 00:00:00 2001 From: Gil Date: Mon, 16 Apr 2018 13:50:11 -0700 Subject: Replace the `SnapshotOptions` object with the behavior enum. (#1109) Instead of calling `get(field, SnapshotOptions.serverTimestampBehavior(.estimate))` call `get(field, serverTimestampBehavior: .estimate)` --- Firestore/Source/API/FIRDocumentSnapshot.mm | 57 +++++++++++------ Firestore/Source/API/FIRSnapshotOptions+Internal.h | 38 ------------ Firestore/Source/API/FIRSnapshotOptions.mm | 72 ---------------------- Firestore/Source/Model/FSTFieldValue.h | 5 -- Firestore/Source/Model/FSTFieldValue.mm | 24 +------- Firestore/Source/Public/FIRDocumentSnapshot.h | 49 +++++---------- 6 files changed, 53 insertions(+), 192 deletions(-) delete mode 100644 Firestore/Source/API/FIRSnapshotOptions+Internal.h delete mode 100644 Firestore/Source/API/FIRSnapshotOptions.mm (limited to 'Firestore/Source') diff --git a/Firestore/Source/API/FIRDocumentSnapshot.mm b/Firestore/Source/API/FIRDocumentSnapshot.mm index 0fd59f4..614982b 100644 --- a/Firestore/Source/API/FIRDocumentSnapshot.mm +++ b/Firestore/Source/API/FIRDocumentSnapshot.mm @@ -24,7 +24,6 @@ #import "Firestore/Source/API/FIRFieldPath+Internal.h" #import "Firestore/Source/API/FIRFirestore+Internal.h" #import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h" -#import "Firestore/Source/API/FIRSnapshotOptions+Internal.h" #import "Firestore/Source/Model/FSTDocument.h" #import "Firestore/Source/Model/FSTFieldValue.h" #import "Firestore/Source/Util/FSTAssert.h" @@ -40,6 +39,21 @@ using firebase::firestore::model::DocumentKey; NS_ASSUME_NONNULL_BEGIN +/** Converts a public FIRServerTimestampBehavior into its internal equivalent. */ +static FSTServerTimestampBehavior InternalServerTimestampBehavor( + FIRServerTimestampBehavior behavior) { + switch (behavior) { + case FIRServerTimestampBehaviorNone: + return FSTServerTimestampBehaviorNone; + case FIRServerTimestampBehaviorEstimate: + return FSTServerTimestampBehaviorEstimate; + case FIRServerTimestampBehaviorPrevious: + return FSTServerTimestampBehaviorPrevious; + default: + FIREBASE_ASSERT_MESSAGE(false, "Unexpected server timestamp option: %ld", (long)behavior); + } +} + @interface FIRDocumentSnapshot () - (instancetype)initWithFirestore:(FIRFirestore *)firestore @@ -144,24 +158,23 @@ NS_ASSUME_NONNULL_BEGIN } - (nullable NSDictionary *)data { - return [self dataWithOptions:[FIRSnapshotOptions defaultOptions]]; + return [self dataWithServerTimestampBehavior:FIRServerTimestampBehaviorNone]; } -- (nullable NSDictionary *)dataWithOptions:(FIRSnapshotOptions *)options { +- (nullable NSDictionary *)dataWithServerTimestampBehavior: + (FIRServerTimestampBehavior)serverTimestampBehavior { + FSTFieldValueOptions *options = [self optionsForServerTimestampBehavior:serverTimestampBehavior]; return self.internalDocument == nil ? nil - : [self convertedObject:[self.internalDocument data] - options:[FSTFieldValueOptions - optionsForSnapshotOptions:options - timestampsInSnapshotsEnabled: - self.firestore.settings.timestampsInSnapshotsEnabled]]; + : [self convertedObject:[self.internalDocument data] options:options]; } - (nullable id)valueForField:(id)field { - return [self valueForField:field options:[FIRSnapshotOptions defaultOptions]]; + return [self valueForField:field serverTimestampBehavior:FIRServerTimestampBehaviorNone]; } -- (nullable id)valueForField:(id)field options:(FIRSnapshotOptions *)options { +- (nullable id)valueForField:(id)field + serverTimestampBehavior:(FIRServerTimestampBehavior)serverTimestampBehavior { FIRFieldPath *fieldPath; if ([field isKindOfClass:[NSString class]]) { @@ -173,13 +186,17 @@ NS_ASSUME_NONNULL_BEGIN } FSTFieldValue *fieldValue = [[self.internalDocument data] valueForPath:fieldPath.internalValue]; - return fieldValue == nil - ? nil - : [self convertedValue:fieldValue - options:[FSTFieldValueOptions - optionsForSnapshotOptions:options - timestampsInSnapshotsEnabled: - self.firestore.settings.timestampsInSnapshotsEnabled]]; + FSTFieldValueOptions *options = [self optionsForServerTimestampBehavior:serverTimestampBehavior]; + return fieldValue == nil ? nil : [self convertedValue:fieldValue options:options]; +} + +- (FSTFieldValueOptions *)optionsForServerTimestampBehavior: + (FIRServerTimestampBehavior)serverTimestampBehavior { + FSTServerTimestampBehavior internalBehavior = + InternalServerTimestampBehavor(serverTimestampBehavior); + return [[FSTFieldValueOptions alloc] + initWithServerTimestampBehavior:internalBehavior + timestampsInSnapshotsEnabled:self.firestore.settings.timestampsInSnapshotsEnabled]; } - (nullable id)objectForKeyedSubscript:(id)key { @@ -262,8 +279,10 @@ NS_ASSUME_NONNULL_BEGIN return data; } -- (NSDictionary *)dataWithOptions:(FIRSnapshotOptions *)options { - NSDictionary *data = [super dataWithOptions:options]; +- (NSDictionary *)dataWithServerTimestampBehavior: + (FIRServerTimestampBehavior)serverTimestampBehavior { + NSDictionary *data = + [super dataWithServerTimestampBehavior:serverTimestampBehavior]; FSTAssert(data, @"Document in a QueryDocumentSnapshot should exist"); return data; } diff --git a/Firestore/Source/API/FIRSnapshotOptions+Internal.h b/Firestore/Source/API/FIRSnapshotOptions+Internal.h deleted file mode 100644 index 64e7dbc..0000000 --- a/Firestore/Source/API/FIRSnapshotOptions+Internal.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2017 Google - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#import "FIRDocumentSnapshot.h" - -#import - -#import "Firestore/Source/Model/FSTFieldValue.h" - -NS_ASSUME_NONNULL_BEGIN - -@interface FIRSnapshotOptions (Internal) - -/** Returns a default instance of FIRSnapshotOptions that specifies no options. */ -+ (instancetype)defaultOptions; - -/* Initializes a new instance with the specified server timestamp behavior. */ -- (instancetype)initWithServerTimestampBehavior:(FSTServerTimestampBehavior)serverTimestampBehavior; - -/* Returns the server timestamp behavior. Returns -1 if no behavior is specified. */ -- (FSTServerTimestampBehavior)serverTimestampBehavior; - -@end - -NS_ASSUME_NONNULL_END diff --git a/Firestore/Source/API/FIRSnapshotOptions.mm b/Firestore/Source/API/FIRSnapshotOptions.mm deleted file mode 100644 index 72ea3cc..0000000 --- a/Firestore/Source/API/FIRSnapshotOptions.mm +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2017 Google - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#import "FIRDocumentSnapshot.h" - -#import "Firestore/Source/API/FIRSnapshotOptions+Internal.h" -#import "Firestore/Source/Util/FSTAssert.h" - -NS_ASSUME_NONNULL_BEGIN - -@interface FIRSnapshotOptions () - -@property(nonatomic) FSTServerTimestampBehavior serverTimestampBehavior; - -@end - -@implementation FIRSnapshotOptions - -- (instancetype)initWithServerTimestampBehavior: - (FSTServerTimestampBehavior)serverTimestampBehavior { - self = [super init]; - - if (self) { - _serverTimestampBehavior = serverTimestampBehavior; - } - - return self; -} - -+ (instancetype)defaultOptions { - static FIRSnapshotOptions *sharedInstance = nil; - static dispatch_once_t onceToken; - - dispatch_once(&onceToken, ^{ - sharedInstance = - [[FIRSnapshotOptions alloc] initWithServerTimestampBehavior:FSTServerTimestampBehaviorNone]; - }); - - return sharedInstance; -} - -+ (instancetype)serverTimestampBehavior:(FIRServerTimestampBehavior)serverTimestampBehavior { - switch (serverTimestampBehavior) { - case FIRServerTimestampBehaviorEstimate: - return [[FIRSnapshotOptions alloc] - initWithServerTimestampBehavior:FSTServerTimestampBehaviorEstimate]; - case FIRServerTimestampBehaviorPrevious: - return [[FIRSnapshotOptions alloc] - initWithServerTimestampBehavior:FSTServerTimestampBehaviorPrevious]; - case FIRServerTimestampBehaviorNone: - return [FIRSnapshotOptions defaultOptions]; - default: - FSTFail(@"Encountered unknown server timestamp behavior: %d", (int)serverTimestampBehavior); - } -} - -@end - -NS_ASSUME_NONNULL_END \ No newline at end of file diff --git a/Firestore/Source/Model/FSTFieldValue.h b/Firestore/Source/Model/FSTFieldValue.h index 6914f4d..6f9798a 100644 --- a/Firestore/Source/Model/FSTFieldValue.h +++ b/Firestore/Source/Model/FSTFieldValue.h @@ -25,7 +25,6 @@ @class FIRTimestamp; @class FSTFieldValueOptions; @class FIRGeoPoint; -@class FIRSnapshotOptions; NS_ASSUME_NONNULL_BEGIN @@ -67,10 +66,6 @@ typedef NS_ENUM(NSInteger, FSTServerTimestampBehavior) { timestampsInSnapshotsEnabled:(BOOL)timestampsInSnapshotsEnabled NS_DESIGNATED_INITIALIZER; -/** Creates an FSTFieldValueOptions instance from FIRSnapshotOptions. */ -+ (instancetype)optionsForSnapshotOptions:(FIRSnapshotOptions *)value - timestampsInSnapshotsEnabled:(BOOL)timestampsInSnapshotsEnabled; - @end /** diff --git a/Firestore/Source/Model/FSTFieldValue.mm b/Firestore/Source/Model/FSTFieldValue.mm index 0d7c649..9e77d39 100644 --- a/Firestore/Source/Model/FSTFieldValue.mm +++ b/Firestore/Source/Model/FSTFieldValue.mm @@ -16,10 +16,10 @@ #import "Firestore/Source/Model/FSTFieldValue.h" +#import "FIRDocumentSnapshot.h" #import "FIRTimestamp.h" #import "Firestore/Source/API/FIRGeoPoint+Internal.h" -#import "Firestore/Source/API/FIRSnapshotOptions+Internal.h" #import "Firestore/Source/Model/FSTDocumentKey.h" #import "Firestore/Source/Util/FSTAssert.h" #import "Firestore/Source/Util/FSTClasses.h" @@ -46,28 +46,6 @@ NS_ASSUME_NONNULL_BEGIN @implementation FSTFieldValueOptions -+ (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 timestampsInSnapshotsEnabled:(BOOL)timestampsInSnapshotsEnabled { self = [super init]; diff --git a/Firestore/Source/Public/FIRDocumentSnapshot.h b/Firestore/Source/Public/FIRDocumentSnapshot.h index 6e79a7f..669fe07 100644 --- a/Firestore/Source/Public/FIRDocumentSnapshot.h +++ b/Firestore/Source/Public/FIRDocumentSnapshot.h @@ -47,29 +47,6 @@ typedef NS_ENUM(NSInteger, FIRServerTimestampBehavior) { FIRServerTimestampBehaviorPrevious } NS_SWIFT_NAME(ServerTimestampBehavior); -/** - * Options that configure how data is retrieved from a `DocumentSnapshot` - * (e.g. the desired behavior for server timestamps that have not yet been set - * to their final value). - */ -NS_SWIFT_NAME(SnapshotOptions) -@interface FIRSnapshotOptions : NSObject - -/** */ -- (instancetype)init __attribute__((unavailable("FIRSnapshotOptions cannot be created directly."))); - -/** - * If set, controls the return value for `FieldValue.serverTimestamp()` - * fields that have not yet been set to their final value. - * - * If omitted, `NSNull` will be returned by default. - * - * @return The created `FIRSnapshotOptions` object. - */ -+ (instancetype)serverTimestampBehavior:(FIRServerTimestampBehavior)serverTimestampBehavior; - -@end - /** * A `FIRDocumentSnapshot` contains data read from a document in your Firestore database. The data * can be extracted with the `data` property or by using subscript syntax to access a specific @@ -105,7 +82,7 @@ NS_SWIFT_NAME(DocumentSnapshot) * `NSNull`. You can use `dataWithOptions()` to configure this behavior. * * @return An `NSDictionary` containing all fields in the document or `nil` if the document doesn't - * exist. + * exist. */ - (nullable NSDictionary *)data; @@ -113,12 +90,13 @@ NS_SWIFT_NAME(DocumentSnapshot) * Retrieves all fields in the document as a `Dictionary`. Returns `nil` if the document doesn't * exist. * - * @param options `SnapshotOptions` to configure how data is returned from the snapshot (e.g. the - * desired behavior for server timestamps that have not yet been set to their final value). + * @param serverTimestampBehavior Configures how server timestamps that have not yet been set to + * their final value are returned from the snapshot. * @return A `Dictionary` containing all fields in the document or `nil` if the document doesn't - * exist. + * exist. */ -- (nullable NSDictionary *)dataWithOptions:(FIRSnapshotOptions *)options; +- (nullable NSDictionary *)dataWithServerTimestampBehavior: + (FIRServerTimestampBehavior)serverTimestampBehavior; /** * Retrieves a specific field from the document. Returns `nil` if the document or the field doesn't @@ -140,14 +118,14 @@ NS_SWIFT_NAME(DocumentSnapshot) * can use `get(_:options:)` to configure this behavior. * * @param field The field to retrieve. - * @param options `SnapshotOptions` to configure how data is returned from the snapshot (e.g. the - * desired behavior for server timestamps that have not yet been set to their final value). + * @param serverTimestampBehavior Configures how server timestamps that have not yet been set to + * their final value are returned from the snapshot. * @return The value contained in the field or `nil` if the document or field doesn't exist. */ // clang-format off - (nullable id)valueForField:(id)field - options:(FIRSnapshotOptions *)options - NS_SWIFT_NAME(get(_:options:)); + serverTimestampBehavior:(FIRServerTimestampBehavior)serverTimestampBehavior + NS_SWIFT_NAME(get(_:serverTimestampBehavior:)); // clang-format on /** @@ -190,11 +168,12 @@ NS_SWIFT_NAME(QueryDocumentSnapshot) /** * Retrieves all fields in the document as a `Dictionary`. * - * @param options `SnapshotOptions` to configure how data is returned from the snapshot (e.g. the - * desired behavior for server timestamps that have not yet been set to their final value). + * @param serverTimestampBehavior Configures how server timestamps that have not yet been set to + * their final value are returned from the snapshot. * @return A `Dictionary` containing all fields in the document. */ -- (NSDictionary *)dataWithOptions:(FIRSnapshotOptions *)options; +- (NSDictionary *)dataWithServerTimestampBehavior: + (FIRServerTimestampBehavior)serverTimestampBehavior; @end -- cgit v1.2.3