FSTDocumentKey.mm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "Firestore/Source/Model/FSTDocumentKey.h"
  17. #include <utility>
  18. #import "Firestore/Source/Core/FSTFirestoreClient.h"
  19. #import "Firestore/Source/Util/FSTAssert.h"
  20. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  21. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  22. namespace util = firebase::firestore::util;
  23. using firebase::firestore::model::ResourcePath;
  24. NS_ASSUME_NONNULL_BEGIN
  25. @interface FSTDocumentKey () {
  26. /** The path to the document. */
  27. ResourcePath _path;
  28. }
  29. @end
  30. @implementation FSTDocumentKey
  31. + (instancetype)keyWithPath:(ResourcePath)path {
  32. return [[FSTDocumentKey alloc] initWithPath:std::move(path)];
  33. }
  34. + (instancetype)keyWithSegments:(std::initializer_list<std::string>)segments {
  35. return [FSTDocumentKey keyWithPath:ResourcePath(segments)];
  36. }
  37. + (instancetype)keyWithPathString:(NSString *)resourcePath {
  38. return [FSTDocumentKey keyWithPath:ResourcePath::FromString(util::MakeStringView(resourcePath))];
  39. }
  40. /** Designated initializer. */
  41. - (instancetype)initWithPath:(ResourcePath)path {
  42. FSTAssert([FSTDocumentKey isDocumentKey:path], @"invalid document key path: %s",
  43. path.CanonicalString().c_str());
  44. if (self = [super init]) {
  45. _path = path;
  46. }
  47. return self;
  48. }
  49. - (BOOL)isEqual:(id)object {
  50. if (self == object) {
  51. return YES;
  52. }
  53. if (![object isKindOfClass:[FSTDocumentKey class]]) {
  54. return NO;
  55. }
  56. return [self isEqualToKey:(FSTDocumentKey *)object];
  57. }
  58. - (NSUInteger)hash {
  59. return _path.Hash();
  60. }
  61. - (NSString *)description {
  62. return [NSString stringWithFormat:@"<FSTDocumentKey: %s>", _path.CanonicalString().c_str()];
  63. }
  64. /** Implements NSCopying without actually copying because FSTDocumentKeys are immutable. */
  65. - (id)copyWithZone:(NSZone *_Nullable)zone {
  66. return self;
  67. }
  68. - (BOOL)isEqualToKey:(FSTDocumentKey *)other {
  69. return FSTDocumentKeyComparator(self, other) == NSOrderedSame;
  70. }
  71. - (NSComparisonResult)compare:(FSTDocumentKey *)other {
  72. return FSTDocumentKeyComparator(self, other);
  73. }
  74. + (NSComparator)comparator {
  75. return ^NSComparisonResult(id obj1, id obj2) {
  76. return [obj1 compare:obj2];
  77. };
  78. }
  79. + (BOOL)isDocumentKey:(const ResourcePath &)path {
  80. return path.size() % 2 == 0;
  81. }
  82. - (const firebase::firestore::model::ResourcePath &)path {
  83. return _path;
  84. }
  85. @end
  86. const NSComparator FSTDocumentKeyComparator =
  87. ^NSComparisonResult(FSTDocumentKey *key1, FSTDocumentKey *key2) {
  88. if (key1.path < key2.path) {
  89. return NSOrderedAscending;
  90. } else if (key1.path > key2.path) {
  91. return NSOrderedDescending;
  92. } else {
  93. return NSOrderedSame;
  94. }
  95. };
  96. NSString *const kDocumentKeyPath = @"__name__";
  97. NS_ASSUME_NONNULL_END