FIRFieldPath.mm 3.6 KB

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