FIRFieldPath.mm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #import "Firestore/Source/Util/FSTUsageValidation.h"
  23. #include "Firestore/core/src/firebase/firestore/model/field_path.h"
  24. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  25. namespace util = firebase::firestore::util;
  26. using firebase::firestore::model::FieldPath;
  27. NS_ASSUME_NONNULL_BEGIN
  28. @interface FIRFieldPath () {
  29. /** Internal field path representation */
  30. firebase::firestore::model::FieldPath _internalValue;
  31. }
  32. @end
  33. @implementation FIRFieldPath
  34. - (instancetype)initWithFields:(NSArray<NSString *> *)fieldNames {
  35. if (fieldNames.count == 0) {
  36. FSTThrowInvalidArgument(@"Invalid field path. Provided names must not be empty.");
  37. }
  38. std::vector<std::string> field_names{};
  39. field_names.reserve(fieldNames.count);
  40. for (int i = 0; i < fieldNames.count; ++i) {
  41. if (fieldNames[i].length == 0) {
  42. FSTThrowInvalidArgument(@"Invalid field name at index %d. Field names must not be empty.", i);
  43. }
  44. field_names.emplace_back(util::MakeString(fieldNames[i]));
  45. }
  46. return [self initPrivate:FieldPath(std::move(field_names))];
  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. if ([[FIRFieldPath reservedCharactersRegex]
  59. numberOfMatchesInString:path
  60. options:0
  61. range:NSMakeRange(0, path.length)] > 0) {
  62. FSTThrowInvalidArgument(
  63. @"Invalid field path (%@). Paths must not contain '~', '*', '/', '[', or ']'", path);
  64. }
  65. @try {
  66. return [[FIRFieldPath alloc] initWithFields:[path componentsSeparatedByString:@"."]];
  67. } @catch (NSException *exception) {
  68. FSTThrowInvalidArgument(
  69. @"Invalid field path (%@). Paths must not be empty, begin with '.', end with '.', or "
  70. @"contain '..'",
  71. path);
  72. }
  73. }
  74. /** Matches any characters in a field path string that are reserved. */
  75. + (NSRegularExpression *)reservedCharactersRegex {
  76. static NSRegularExpression *regex = nil;
  77. static dispatch_once_t onceToken;
  78. dispatch_once(&onceToken, ^{
  79. regex = [NSRegularExpression regularExpressionWithPattern:@"[~*/\\[\\]]" options:0 error:nil];
  80. });
  81. return regex;
  82. }
  83. - (id)copyWithZone:(NSZone *__nullable)zone {
  84. return [[[self class] alloc] initPrivate:_internalValue];
  85. }
  86. - (BOOL)isEqual:(nullable id)object {
  87. if (self == object) {
  88. return YES;
  89. }
  90. if (![object isKindOfClass:[FIRFieldPath class]]) {
  91. return NO;
  92. }
  93. return _internalValue == ((FIRFieldPath *)object)->_internalValue;
  94. }
  95. - (NSUInteger)hash {
  96. return _internalValue.Hash();
  97. }
  98. - (const firebase::firestore::model::FieldPath &)internalValue {
  99. return _internalValue;
  100. }
  101. @end
  102. NS_ASSUME_NONNULL_END