FSTDocumentKey.mm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <string>
  18. #include <utility>
  19. #import "Firestore/Source/Core/FSTFirestoreClient.h"
  20. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  21. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  22. #include "Firestore/core/src/firebase/firestore/util/hashing.h"
  23. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  24. namespace util = firebase::firestore::util;
  25. using firebase::firestore::model::ResourcePath;
  26. NS_ASSUME_NONNULL_BEGIN
  27. @interface FSTDocumentKey () {
  28. /** The path to the document. */
  29. ResourcePath _path;
  30. }
  31. @end
  32. @implementation FSTDocumentKey
  33. + (instancetype)keyWithPath:(ResourcePath)path {
  34. return [[FSTDocumentKey alloc] initWithPath:std::move(path)];
  35. }
  36. + (instancetype)keyWithSegments:(std::initializer_list<std::string>)segments {
  37. return [FSTDocumentKey keyWithPath:ResourcePath(segments)];
  38. }
  39. + (instancetype)keyWithPathString:(NSString *)resourcePath {
  40. return [FSTDocumentKey keyWithPath:ResourcePath::FromString(util::MakeStringView(resourcePath))];
  41. }
  42. /** Designated initializer. */
  43. - (instancetype)initWithPath:(ResourcePath)path {
  44. HARD_ASSERT([FSTDocumentKey isDocumentKey:path], "invalid document key path: %s",
  45. path.CanonicalString());
  46. if (self = [super init]) {
  47. _path = path;
  48. }
  49. return self;
  50. }
  51. - (BOOL)isEqual:(id)object {
  52. if (self == object) {
  53. return YES;
  54. }
  55. if (![object isKindOfClass:[FSTDocumentKey class]]) {
  56. return NO;
  57. }
  58. return [self isEqualToKey:(FSTDocumentKey *)object];
  59. }
  60. - (NSUInteger)hash {
  61. return util::Hash(_path);
  62. }
  63. - (NSString *)description {
  64. return [NSString stringWithFormat:@"<FSTDocumentKey: %s>", _path.CanonicalString().c_str()];
  65. }
  66. /** Implements NSCopying without actually copying because FSTDocumentKeys are immutable. */
  67. - (id)copyWithZone:(NSZone *_Nullable)zone {
  68. return self;
  69. }
  70. - (BOOL)isEqualToKey:(FSTDocumentKey *)other {
  71. return FSTDocumentKeyComparator(self, other) == NSOrderedSame;
  72. }
  73. - (NSComparisonResult)compare:(FSTDocumentKey *)other {
  74. return FSTDocumentKeyComparator(self, other);
  75. }
  76. + (NSComparator)comparator {
  77. return ^NSComparisonResult(id obj1, id obj2) {
  78. return [obj1 compare:obj2];
  79. };
  80. }
  81. + (BOOL)isDocumentKey:(const ResourcePath &)path {
  82. return path.size() % 2 == 0;
  83. }
  84. - (const ResourcePath &)path {
  85. return _path;
  86. }
  87. @end
  88. const NSComparator FSTDocumentKeyComparator =
  89. ^NSComparisonResult(FSTDocumentKey *key1, FSTDocumentKey *key2) {
  90. if (key1.path < key2.path) {
  91. return NSOrderedAscending;
  92. } else if (key1.path > key2.path) {
  93. return NSOrderedDescending;
  94. } else {
  95. return NSOrderedSame;
  96. }
  97. };
  98. NSString *const kDocumentKeyPath = @"__name__";
  99. NS_ASSUME_NONNULL_END