FSTDocumentKey.mm 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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)keyWithDocumentKey:(const firebase::firestore::model::DocumentKey &)documentKey {
  36. return [[FSTDocumentKey alloc] initWithDocumentKey:documentKey];
  37. }
  38. /** Designated initializer. */
  39. - (instancetype)initWithDocumentKey:(const DocumentKey &)key {
  40. if (self = [super init]) {
  41. _delegate = key;
  42. }
  43. return self;
  44. }
  45. - (const DocumentKey &)key {
  46. return _delegate;
  47. }
  48. - (BOOL)isEqual:(id)object {
  49. if (self == object) {
  50. return YES;
  51. }
  52. if (![object isKindOfClass:[FSTDocumentKey class]]) {
  53. return NO;
  54. }
  55. return _delegate == static_cast<FSTDocumentKey *>(object)->_delegate;
  56. }
  57. - (NSUInteger)hash {
  58. return _delegate.Hash();
  59. }
  60. - (NSString *)description {
  61. return [NSString stringWithFormat:@"<FSTDocumentKey: %s>", _delegate.ToString().c_str()];
  62. }
  63. /** Implements NSCopying without actually copying because FSTDocumentKeys are immutable. */
  64. - (id)copyWithZone:(NSZone *_Nullable)zone {
  65. return self;
  66. }
  67. @end
  68. NSString *const kDocumentKeyPath = @"__name__";
  69. NS_ASSUME_NONNULL_END