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