FIRFieldPath.mm 3.5 KB

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