FIRFieldPath.mm 3.7 KB

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