FSTDocumentKey.mm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/document_key.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. using firebase::firestore::model::DocumentKey;
  27. NS_ASSUME_NONNULL_BEGIN
  28. @interface FSTDocumentKey () {
  29. // Forward most of the logic to the C++ implementation until FSTDocumentKey usages are completely
  30. // migrated.
  31. DocumentKey _delegate;
  32. }
  33. @end
  34. @implementation FSTDocumentKey
  35. + (instancetype)keyWithPath:(ResourcePath)path {
  36. return [[FSTDocumentKey alloc] initWithDocumentKey:DocumentKey{path}];
  37. }
  38. + (instancetype)keyWithDocumentKey:(const firebase::firestore::model::DocumentKey &)documentKey {
  39. return [[FSTDocumentKey alloc] initWithDocumentKey:documentKey];
  40. }
  41. + (instancetype)keyWithSegments:(std::initializer_list<std::string>)segments {
  42. return [FSTDocumentKey keyWithPath:ResourcePath(segments)];
  43. }
  44. + (instancetype)keyWithPathString:(NSString *)resourcePath {
  45. return [FSTDocumentKey keyWithPath:ResourcePath::FromString(util::MakeStringView(resourcePath))];
  46. }
  47. /** Designated initializer. */
  48. - (instancetype)initWithDocumentKey:(const DocumentKey &)key {
  49. if (self = [super init]) {
  50. _delegate = key;
  51. }
  52. return self;
  53. }
  54. - (BOOL)isEqual:(id)object {
  55. if (self == object) {
  56. return YES;
  57. }
  58. if (![object isKindOfClass:[FSTDocumentKey class]]) {
  59. return NO;
  60. }
  61. return [self isEqualToKey:(FSTDocumentKey *)object];
  62. }
  63. - (NSUInteger)hash {
  64. return _delegate.Hash();
  65. }
  66. - (NSString *)description {
  67. return [NSString stringWithFormat:@"<FSTDocumentKey: %s>", _delegate.ToString().c_str()];
  68. }
  69. /** Implements NSCopying without actually copying because FSTDocumentKeys are immutable. */
  70. - (id)copyWithZone:(NSZone *_Nullable)zone {
  71. return self;
  72. }
  73. - (BOOL)isEqualToKey:(FSTDocumentKey *)other {
  74. return FSTDocumentKeyComparator(self, other) == NSOrderedSame;
  75. }
  76. - (NSComparisonResult)compare:(FSTDocumentKey *)other {
  77. return FSTDocumentKeyComparator(self, other);
  78. }
  79. + (NSComparator)comparator {
  80. return ^NSComparisonResult(id obj1, id obj2) {
  81. return [obj1 compare:obj2];
  82. };
  83. }
  84. + (BOOL)isDocumentKey:(const ResourcePath &)path {
  85. return DocumentKey::IsDocumentKey(path);
  86. }
  87. - (const ResourcePath &)path {
  88. return _delegate.path();
  89. }
  90. @end
  91. const NSComparator FSTDocumentKeyComparator =
  92. ^NSComparisonResult(FSTDocumentKey *key1, FSTDocumentKey *key2) {
  93. if (key1.path < key2.path) {
  94. return NSOrderedAscending;
  95. } else if (key1.path > key2.path) {
  96. return NSOrderedDescending;
  97. } else {
  98. return NSOrderedSame;
  99. }
  100. };
  101. NSString *const kDocumentKeyPath = @"__name__";
  102. NS_ASSUME_NONNULL_END