Files
firebase-ios-sdk/Firestore/Source/API/FIRCollectionReference.mm
T
zxu 9ddf3637ac 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
2017-12-19 13:47:04 -05:00

141 lines
4.8 KiB
Plaintext

/*
* 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 "FIRCollectionReference.h"
#import "FIRFirestore.h"
#include "Firestore/core/src/firebase/firestore/util/autoid.h"
#import "Firestore/Source/API/FIRDocumentReference+Internal.h"
#import "Firestore/Source/API/FIRQuery+Internal.h"
#import "Firestore/Source/API/FIRQuery_Init.h"
#import "Firestore/Source/Core/FSTQuery.h"
#import "Firestore/Source/Model/FSTDocumentKey.h"
#import "Firestore/Source/Model/FSTPath.h"
#import "Firestore/Source/Util/FSTAssert.h"
#import "Firestore/Source/Util/FSTUsageValidation.h"
using firebase::firestore::util::CreateAutoId;
NS_ASSUME_NONNULL_BEGIN
@interface FIRCollectionReference ()
- (instancetype)initWithPath:(FSTResourcePath *)path
firestore:(FIRFirestore *)firestore NS_DESIGNATED_INITIALIZER;
// Mark the super class designated initializer unavailable.
- (instancetype)initWithQuery:(FSTQuery *)query
firestore:(FIRFirestore *)firestore
__attribute__((unavailable("Use the initWithPath constructor of FIRCollectionReference.")));
@end
@implementation FIRCollectionReference (Internal)
+ (instancetype)referenceWithPath:(FSTResourcePath *)path firestore:(FIRFirestore *)firestore {
return [[FIRCollectionReference alloc] initWithPath:path firestore:firestore];
}
@end
@implementation FIRCollectionReference
- (instancetype)initWithPath:(FSTResourcePath *)path firestore:(FIRFirestore *)firestore {
if (path.length % 2 != 1) {
FSTThrowInvalidArgument(
@"Invalid collection reference. Collection references must have an odd "
"number of segments, but %@ has %d",
path.canonicalString, path.length);
}
self = [super initWithQuery:[FSTQuery queryWithPath:path] firestore:firestore];
return self;
}
// Override the designated initializer from the super class.
- (instancetype)initWithQuery:(FSTQuery *)query firestore:(FIRFirestore *)firestore {
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];
}
- (FIRDocumentReference *_Nullable)parent {
FSTResourcePath *parentPath = [self.query.path pathByRemovingLastSegment];
if (parentPath.isEmpty) {
return nil;
} else {
FSTDocumentKey *key = [FSTDocumentKey keyWithPath:parentPath];
return [FIRDocumentReference referenceWithKey:key firestore:self.firestore];
}
}
- (NSString *)path {
return [self.query.path canonicalString];
}
- (FIRDocumentReference *)documentWithPath:(NSString *)documentPath {
if (!documentPath) {
FSTThrowInvalidArgument(@"Document path cannot be nil.");
}
FSTResourcePath *subPath = [FSTResourcePath pathWithString:documentPath];
FSTResourcePath *path = [self.query.path pathByAppendingPath:subPath];
return [FIRDocumentReference referenceWithPath:path firestore:self.firestore];
}
- (FIRDocumentReference *)addDocumentWithData:(NSDictionary<NSString *, id> *)data {
return [self addDocumentWithData:data completion:nil];
}
- (FIRDocumentReference *)addDocumentWithData:(NSDictionary<NSString *, id> *)data
completion:
(nullable void (^)(NSError *_Nullable error))completion {
FIRDocumentReference *docRef = [self documentWithAutoID];
[docRef setData:data completion:completion];
return docRef;
}
- (FIRDocumentReference *)documentWithAutoID {
NSString *autoID = [NSString stringWithUTF8String:CreateAutoId().c_str()];
FSTDocumentKey *key =
[FSTDocumentKey keyWithPath:[self.query.path pathByAppendingSegment:autoID]];
return [FIRDocumentReference referenceWithKey:key firestore:self.firestore];
}
@end
NS_ASSUME_NONNULL_END