FIRFieldPath.mm 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 "FIRFieldPath.h"
  17. #include <functional>
  18. #include <string>
  19. #include <utility>
  20. #include <vector>
  21. #import "Firestore/Source/API/FIRFieldPath+Internal.h"
  22. #include "Firestore/core/src/model/field_path.h"
  23. #include "Firestore/core/src/util/exception.h"
  24. #include "Firestore/core/src/util/hashing.h"
  25. #include "Firestore/core/src/util/string_apple.h"
  26. using firebase::firestore::model::FieldPath;
  27. using firebase::firestore::util::Hash;
  28. using firebase::firestore::util::MakeString;
  29. using firebase::firestore::util::ThrowInvalidArgument;
  30. NS_ASSUME_NONNULL_BEGIN
  31. @interface FIRFieldPath () {
  32. /** Internal field path representation */
  33. firebase::firestore::model::FieldPath _internalValue;
  34. }
  35. @end
  36. @implementation FIRFieldPath
  37. - (instancetype)initWithFields:(NSArray<NSString *> *)fieldNames {
  38. if (fieldNames.count == 0) {
  39. ThrowInvalidArgument("Invalid field path. Provided names must not be empty.");
  40. }
  41. std::vector<std::string> converted;
  42. converted.reserve(fieldNames.count);
  43. for (NSString *fieldName in fieldNames) {
  44. converted.emplace_back(MakeString(fieldName));
  45. }
  46. return [self initPrivate:FieldPath::FromSegments(std::move(converted))];
  47. }
  48. + (instancetype)documentID {
  49. return [[FIRFieldPath alloc] initPrivate:FieldPath::KeyFieldPath()];
  50. }
  51. - (instancetype)initPrivate:(FieldPath)fieldPath {
  52. if (self = [super init]) {
  53. _internalValue = std::move(fieldPath);
  54. }
  55. return self;
  56. }
  57. + (instancetype)pathWithDotSeparatedString:(NSString *)path {
  58. return [[FIRFieldPath alloc] initPrivate:FieldPath::FromDotSeparatedString(MakeString(path))];
  59. }
  60. - (id)copyWithZone:(__unused NSZone *_Nullable)zone {
  61. return [[[self class] alloc] initPrivate:_internalValue];
  62. }
  63. - (BOOL)isEqual:(nullable id)object {
  64. if (self == object) {
  65. return YES;
  66. }
  67. if (![object isKindOfClass:[FIRFieldPath class]]) {
  68. return NO;
  69. }
  70. return _internalValue == ((FIRFieldPath *)object)->_internalValue;
  71. }
  72. - (NSUInteger)hash {
  73. return Hash(_internalValue);
  74. }
  75. - (const firebase::firestore::model::FieldPath &)internalValue {
  76. return _internalValue;
  77. }
  78. @end
  79. NS_ASSUME_NONNULL_END