diff options
author | zxu <zxu@google.com> | 2018-03-06 14:28:04 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-06 14:28:04 -0500 |
commit | 8311c6432ecff78bedd13e27f64d241659324786 (patch) | |
tree | 982484ba69cf17172ee4e1308254a0b76894b7d5 /Firestore/Source/Model | |
parent | 34ebf10b0acc65f1924d723e82085d4104bc281d (diff) |
port paths to FSTDocumentKey (#877)
* replace path with C++ implementation in FSTDocumentKey.{h,mm} only
* address changes
* address changes
Diffstat (limited to 'Firestore/Source/Model')
-rw-r--r-- | Firestore/Source/Model/FSTDocument.mm | 4 | ||||
-rw-r--r-- | Firestore/Source/Model/FSTDocumentKey.h | 15 | ||||
-rw-r--r-- | Firestore/Source/Model/FSTDocumentKey.mm | 53 |
3 files changed, 45 insertions, 27 deletions
diff --git a/Firestore/Source/Model/FSTDocument.mm b/Firestore/Source/Model/FSTDocument.mm index bf416e7..58d3629 100644 --- a/Firestore/Source/Model/FSTDocument.mm +++ b/Firestore/Source/Model/FSTDocument.mm @@ -94,8 +94,8 @@ NS_ASSUME_NONNULL_BEGIN } - (NSString *)description { - return [NSString stringWithFormat:@"<FSTDocument: key:%@ version:%@ localMutations:%@ data:%@>", - self.key.path, self.version, + return [NSString stringWithFormat:@"<FSTDocument: key:%s version:%@ localMutations:%@ data:%@>", + self.key.path.CanonicalString().c_str(), self.version, self.localMutations ? @"YES" : @"NO", self.data]; } diff --git a/Firestore/Source/Model/FSTDocumentKey.h b/Firestore/Source/Model/FSTDocumentKey.h index 2af1c9a..dbcff2c 100644 --- a/Firestore/Source/Model/FSTDocumentKey.h +++ b/Firestore/Source/Model/FSTDocumentKey.h @@ -16,7 +16,9 @@ #import <Foundation/Foundation.h> -@class FSTResourcePath; +#include <initializer_list> + +#include "Firestore/core/src/firebase/firestore/model/resource_path.h" NS_ASSUME_NONNULL_BEGIN @@ -29,16 +31,14 @@ NS_ASSUME_NONNULL_BEGIN * @param path The path to the document. * @return A new instance of FSTDocumentKey. */ -+ (instancetype)keyWithPath:(FSTResourcePath *)path; - ++ (instancetype)keyWithPath:(firebase::firestore::model::ResourcePath)path; /** * Creates and returns a new document key with a path with the given segments. * * @param segments The segments of the path to the document. * @return A new instance of FSTDocumentKey. */ -+ (instancetype)keyWithSegments:(NSArray<NSString *> *)segments; - ++ (instancetype)keyWithSegments:(std::initializer_list<std::string>)segments; /** * Creates and returns a new document key from the given resource path string. * @@ -48,13 +48,12 @@ NS_ASSUME_NONNULL_BEGIN + (instancetype)keyWithPathString:(NSString *)resourcePath; /** Returns true iff the given path is a path to a document. */ -+ (BOOL)isDocumentKey:(FSTResourcePath *)path; - ++ (BOOL)isDocumentKey:(const firebase::firestore::model::ResourcePath &)path; - (BOOL)isEqualToKey:(FSTDocumentKey *)other; - (NSComparisonResult)compare:(FSTDocumentKey *)other; /** The path to the document. */ -@property(strong, nonatomic, readonly) FSTResourcePath *path; +- (const firebase::firestore::model::ResourcePath &)path; @end diff --git a/Firestore/Source/Model/FSTDocumentKey.mm b/Firestore/Source/Model/FSTDocumentKey.mm index a382a55..269748f 100644 --- a/Firestore/Source/Model/FSTDocumentKey.mm +++ b/Firestore/Source/Model/FSTDocumentKey.mm @@ -16,35 +16,43 @@ #import "Firestore/Source/Model/FSTDocumentKey.h" +#include <utility> + #import "Firestore/Source/Core/FSTFirestoreClient.h" -#import "Firestore/Source/Model/FSTPath.h" #import "Firestore/Source/Util/FSTAssert.h" +#include "Firestore/core/src/firebase/firestore/model/resource_path.h" +#include "Firestore/core/src/firebase/firestore/util/string_apple.h" + +namespace util = firebase::firestore::util; +using firebase::firestore::model::ResourcePath; + NS_ASSUME_NONNULL_BEGIN -@interface FSTDocumentKey () -/** The path to the document. */ -@property(strong, nonatomic, readwrite) FSTResourcePath *path; +@interface FSTDocumentKey () { + /** The path to the document. */ + ResourcePath _path; +} @end @implementation FSTDocumentKey -+ (instancetype)keyWithPath:(FSTResourcePath *)path { - return [[FSTDocumentKey alloc] initWithPath:path]; ++ (instancetype)keyWithPath:(ResourcePath)path { + return [[FSTDocumentKey alloc] initWithPath:std::move(path)]; } -+ (instancetype)keyWithSegments:(NSArray<NSString *> *)segments { - return [FSTDocumentKey keyWithPath:[FSTResourcePath pathWithSegments:segments]]; ++ (instancetype)keyWithSegments:(std::initializer_list<std::string>)segments { + return [FSTDocumentKey keyWithPath:ResourcePath(segments)]; } + (instancetype)keyWithPathString:(NSString *)resourcePath { - NSArray<NSString *> *segments = [resourcePath componentsSeparatedByString:@"/"]; - return [FSTDocumentKey keyWithSegments:segments]; + return [FSTDocumentKey keyWithPath:ResourcePath::FromString(util::MakeStringView(resourcePath))]; } /** Designated initializer. */ -- (instancetype)initWithPath:(FSTResourcePath *)path { - FSTAssert([FSTDocumentKey isDocumentKey:path], @"invalid document key path: %@", path); +- (instancetype)initWithPath:(ResourcePath)path { + FSTAssert([FSTDocumentKey isDocumentKey:path], @"invalid document key path: %s", + path.CanonicalString().c_str()); if (self = [super init]) { _path = path; @@ -63,11 +71,11 @@ NS_ASSUME_NONNULL_BEGIN } - (NSUInteger)hash { - return self.path.hash; + return _path.Hash(); } - (NSString *)description { - return [NSString stringWithFormat:@"<FSTDocumentKey: %@>", self.path]; + return [NSString stringWithFormat:@"<FSTDocumentKey: %s>", _path.CanonicalString().c_str()]; } /** Implements NSCopying without actually copying because FSTDocumentKeys are immutable. */ @@ -89,15 +97,26 @@ NS_ASSUME_NONNULL_BEGIN }; } -+ (BOOL)isDocumentKey:(FSTResourcePath *)path { - return path.length % 2 == 0; ++ (BOOL)isDocumentKey:(const ResourcePath &)path { + return path.size() % 2 == 0; +} + +- (const firebase::firestore::model::ResourcePath &)path { + return _path; } @end const NSComparator FSTDocumentKeyComparator = ^NSComparisonResult(FSTDocumentKey *key1, FSTDocumentKey *key2) { - return [key1.path compare:key2.path]; + if (key1.path < key2.path) { + return NSOrderedAscending; + } else if (key1.path > key2.path) { + return NSOrderedDescending; + } else { + return NSOrderedSame; + } + }; NSString *const kDocumentKeyPath = @"__name__"; |