aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Source/API
diff options
context:
space:
mode:
authorGravatar zxu <zxu@google.com>2017-12-19 13:47:04 -0500
committerGravatar GitHub <noreply@github.com>2017-12-19 13:47:04 -0500
commit9ddf3637ac984abfe16d4ebef6ff3947b4e946c1 (patch)
tree008d6eea9adc10675c79c7152703f4fd05af4cf9 /Firestore/Source/API
parent52cdf0b25a12b5baed8546893634dab9c1a03e56 (diff)
Firestore `isEqual()` on Public Types (#569)
* add bone code for new isEqual and unit test for old isEqual * add bone code for `FIRCollectionReference`, `FIRDocumentSnapshot`, `FIRFieldValue`, `FIRQuerySnapshot`, `FIRSnapshotMetadata`; * change inconsistenciness of `FIRFieldPath.isEqual` implementation; * add unit test (and file) for `FIRDocumentReferenceTest.m`, `FIRFieldPathTest.m`, `FIRQueryTests.testEquals`; `FIRGeoPoint` already has test and Blob is internal type. * Implement isEqual for FIRCollectionReference adding the working code and unit test. * implement isEqual for FIRSnapshotMetadata * Implement isEqual for FIRDocumentSnapshot * Implement isEqual for FIRQuerySnapshot * (un)implement `isEqual` for `FIRFieldValue` Since `FIRFieldValue` both types are singleton, we do not need override `isEqual`. Add test to test the default `NSObject` `isEqual` works just fine. * fix style with `scripts/style.sh` * Implement hash for those with overridden isEqual without hash yet. * refactor to use test helper functions -- FSTTestFirestore, FSTTestPath, FSTTestDocKey * refactor using test helper `FSTTestDocSnapshot`, `FSTTestFieldPath`, `FSTTestQuery`, `FSTTestDoc` * refactoring to use test helper method `FSTTestQuerySnapshot`, * remove unneccessary nil-check, check isKindOfClass instead of isEqual * refactoring: adding `FSTAPIHelpers.{h,m}`, `FSTTest{Collection,Document}Ref`, better naming and style fix * a file forgot in last commit * mainly clean up import and some minor refactoring * fix style via style.sh * minor style fix * add pragma ignored -Wnonnull
Diffstat (limited to 'Firestore/Source/API')
-rw-r--r--Firestore/Source/API/FIRCollectionReference.mm24
-rw-r--r--Firestore/Source/API/FIRDocumentReference.m2
-rw-r--r--Firestore/Source/API/FIRDocumentSnapshot.m31
-rw-r--r--Firestore/Source/API/FIRFieldPath.m2
-rw-r--r--Firestore/Source/API/FIRQuery.m2
-rw-r--r--Firestore/Source/API/FIRQuerySnapshot.m30
-rw-r--r--Firestore/Source/API/FIRSnapshotMetadata.m22
7 files changed, 110 insertions, 3 deletions
diff --git a/Firestore/Source/API/FIRCollectionReference.mm b/Firestore/Source/API/FIRCollectionReference.mm
index 92cccc6..70a14c2 100644
--- a/Firestore/Source/API/FIRCollectionReference.mm
+++ b/Firestore/Source/API/FIRCollectionReference.mm
@@ -15,6 +15,7 @@
*/
#import "FIRCollectionReference.h"
+#import "FIRFirestore.h"
#include "Firestore/core/src/firebase/firestore/util/autoid.h"
@@ -65,6 +66,29 @@ NS_ASSUME_NONNULL_BEGIN
FSTFail(@"Use FIRCollectionReference initWithPath: initializer.");
}
+// NSObject Methods
+- (BOOL)isEqual:(nullable id)other {
+ if (other == self) return YES;
+ if (![[other class] isEqual:[self class]]) return NO;
+
+ return [self isEqualToReference:other];
+}
+
+- (BOOL)isEqualToReference:(nullable FIRCollectionReference *)reference {
+ if (self == reference) return YES;
+ if (reference == nil) return NO;
+ if (self.firestore != reference.firestore && ![self.firestore isEqual:reference.firestore])
+ return NO;
+ if (self.query != reference.query && ![self.query isEqual:reference.query]) return NO;
+ return YES;
+}
+
+- (NSUInteger)hash {
+ NSUInteger hash = [self.firestore hash];
+ hash = hash * 31u + [self.query hash];
+ return hash;
+}
+
- (NSString *)collectionID {
return [self.query.path lastSegment];
}
diff --git a/Firestore/Source/API/FIRDocumentReference.m b/Firestore/Source/API/FIRDocumentReference.m
index 6279b7c..87e6631 100644
--- a/Firestore/Source/API/FIRDocumentReference.m
+++ b/Firestore/Source/API/FIRDocumentReference.m
@@ -116,7 +116,7 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)isEqual:(nullable id)other {
if (other == self) return YES;
- if (!other || ![[other class] isEqual:[self class]]) return NO;
+ if (![[other class] isEqual:[self class]]) return NO;
return [self isEqualToReference:other];
}
diff --git a/Firestore/Source/API/FIRDocumentSnapshot.m b/Firestore/Source/API/FIRDocumentSnapshot.m
index 65c7b61..358ddac 100644
--- a/Firestore/Source/API/FIRDocumentSnapshot.m
+++ b/Firestore/Source/API/FIRDocumentSnapshot.m
@@ -78,6 +78,37 @@ NS_ASSUME_NONNULL_BEGIN
return self;
}
+// NSObject Methods
+- (BOOL)isEqual:(nullable id)other {
+ if (other == self) return YES;
+ // self class could be FIRDocumentSnapshot or subtype. So we compare with base type explicitly.
+ if (![other isKindOfClass:[FIRDocumentSnapshot class]]) return NO;
+
+ return [self isEqualToSnapshot:other];
+}
+
+- (BOOL)isEqualToSnapshot:(nullable FIRDocumentSnapshot *)snapshot {
+ if (self == snapshot) return YES;
+ if (snapshot == nil) return NO;
+ if (self.firestore != snapshot.firestore && ![self.firestore isEqual:snapshot.firestore])
+ return NO;
+ if (self.internalKey != snapshot.internalKey && ![self.internalKey isEqual:snapshot.internalKey])
+ return NO;
+ if (self.internalDocument != snapshot.internalDocument &&
+ ![self.internalDocument isEqual:snapshot.internalDocument])
+ return NO;
+ if (self.fromCache != snapshot.fromCache) return NO;
+ return YES;
+}
+
+- (NSUInteger)hash {
+ NSUInteger hash = [self.firestore hash];
+ hash = hash * 31u + [self.internalKey hash];
+ hash = hash * 31u + [self.internalDocument hash];
+ hash = hash * 31u + (self.fromCache ? 1 : 0);
+ return hash;
+}
+
@dynamic exists;
- (BOOL)exists {
diff --git a/Firestore/Source/API/FIRFieldPath.m b/Firestore/Source/API/FIRFieldPath.m
index d0a70c0..f4e532f 100644
--- a/Firestore/Source/API/FIRFieldPath.m
+++ b/Firestore/Source/API/FIRFieldPath.m
@@ -80,7 +80,7 @@ NS_ASSUME_NONNULL_BEGIN
return [[[self class] alloc] initPrivate:self.internalValue];
}
-- (BOOL)isEqual:(id)object {
+- (BOOL)isEqual:(nullable id)object {
if (self == object) {
return YES;
}
diff --git a/Firestore/Source/API/FIRQuery.m b/Firestore/Source/API/FIRQuery.m
index bb1599a..2feca39 100644
--- a/Firestore/Source/API/FIRQuery.m
+++ b/Firestore/Source/API/FIRQuery.m
@@ -107,7 +107,7 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)isEqual:(nullable id)other {
if (other == self) return YES;
- if (!other || ![[other class] isEqual:[self class]]) return NO;
+ if (![[other class] isEqual:[self class]]) return NO;
return [self isEqualToQuery:other];
}
diff --git a/Firestore/Source/API/FIRQuerySnapshot.m b/Firestore/Source/API/FIRQuerySnapshot.m
index 5e1af9a..177e461 100644
--- a/Firestore/Source/API/FIRQuerySnapshot.m
+++ b/Firestore/Source/API/FIRQuerySnapshot.m
@@ -16,6 +16,7 @@
#import "Firestore/Source/API/FIRQuerySnapshot+Internal.h"
+#import "FIRFirestore.h"
#import "FIRSnapshotMetadata.h"
#import "Firestore/Source/API/FIRDocumentChange+Internal.h"
#import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
@@ -76,6 +77,35 @@ NS_ASSUME_NONNULL_BEGIN
return self;
}
+// NSObject Methods
+- (BOOL)isEqual:(nullable id)other {
+ if (other == self) return YES;
+ if (![[other class] isEqual:[self class]]) return NO;
+
+ return [self isEqualToSnapshot:other];
+}
+
+- (BOOL)isEqualToSnapshot:(nullable FIRQuerySnapshot *)snapshot {
+ if (self == snapshot) return YES;
+ if (snapshot == nil) return NO;
+ if (self.firestore != snapshot.firestore && ![self.firestore isEqual:snapshot.firestore])
+ return NO;
+ if (self.originalQuery != snapshot.originalQuery &&
+ ![self.originalQuery isEqual:snapshot.originalQuery])
+ return NO;
+ if (self.snapshot != snapshot.snapshot && ![self.snapshot isEqual:snapshot.snapshot]) return NO;
+ if (self.metadata != snapshot.metadata && ![self.metadata isEqual:snapshot.metadata]) return NO;
+ return YES;
+}
+
+- (NSUInteger)hash {
+ NSUInteger hash = [self.firestore hash];
+ hash = hash * 31u + [self.originalQuery hash];
+ hash = hash * 31u + [self.snapshot hash];
+ hash = hash * 31u + [self.metadata hash];
+ return hash;
+}
+
@dynamic empty;
- (FIRQuery *)query {
diff --git a/Firestore/Source/API/FIRSnapshotMetadata.m b/Firestore/Source/API/FIRSnapshotMetadata.m
index 224015f..d957a8d 100644
--- a/Firestore/Source/API/FIRSnapshotMetadata.m
+++ b/Firestore/Source/API/FIRSnapshotMetadata.m
@@ -44,6 +44,28 @@ NS_ASSUME_NONNULL_BEGIN
return self;
}
+// NSObject Methods
+- (BOOL)isEqual:(nullable id)other {
+ if (other == self) return YES;
+ if (![[other class] isEqual:[self class]]) return NO;
+
+ return [self isEqualToMetadata:other];
+}
+
+- (BOOL)isEqualToMetadata:(nullable FIRSnapshotMetadata *)metadata {
+ if (self == metadata) return YES;
+ if (metadata == nil) return NO;
+ if (self.pendingWrites != metadata.pendingWrites) return NO;
+ if (self.fromCache != metadata.fromCache) return NO;
+ return YES;
+}
+
+- (NSUInteger)hash {
+ NSUInteger hash = self.pendingWrites ? 1 : 0;
+ hash = hash * 31u + (self.fromCache ? 1 : 0);
+ return hash;
+}
+
@end
NS_ASSUME_NONNULL_END