aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Source/API
diff options
context:
space:
mode:
authorGravatar Gil <mcg@google.com>2018-04-16 13:50:11 -0700
committerGravatar GitHub <noreply@github.com>2018-04-16 13:50:11 -0700
commita25d05487435d397f3b8cd399ee8355eae497f0d (patch)
tree323b23f9ad4849093f2dbb53c8ebece59f0bd870 /Firestore/Source/API
parent0ac71f294ffedc8c6a3fb93e18253ee25624ec00 (diff)
Replace the `SnapshotOptions` object with the behavior enum. (#1109)
Instead of calling `get(field, SnapshotOptions.serverTimestampBehavior(.estimate))` call `get(field, serverTimestampBehavior: .estimate)`
Diffstat (limited to 'Firestore/Source/API')
-rw-r--r--Firestore/Source/API/FIRDocumentSnapshot.mm57
-rw-r--r--Firestore/Source/API/FIRSnapshotOptions+Internal.h38
-rw-r--r--Firestore/Source/API/FIRSnapshotOptions.mm72
3 files changed, 38 insertions, 129 deletions
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<NSString *, id> *)data {
- return [self dataWithOptions:[FIRSnapshotOptions defaultOptions]];
+ return [self dataWithServerTimestampBehavior:FIRServerTimestampBehaviorNone];
}
-- (nullable NSDictionary<NSString *, id> *)dataWithOptions:(FIRSnapshotOptions *)options {
+- (nullable NSDictionary<NSString *, id> *)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<NSString *, id> *)dataWithOptions:(FIRSnapshotOptions *)options {
- NSDictionary<NSString *, id> *data = [super dataWithOptions:options];
+- (NSDictionary<NSString *, id> *)dataWithServerTimestampBehavior:
+ (FIRServerTimestampBehavior)serverTimestampBehavior {
+ NSDictionary<NSString *, id> *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 <Foundation/Foundation.h>
-
-#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