FIRFieldPath.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #import "Firestore/Source/Model/FSTPath.h"
  18. #import "Firestore/Source/Util/FSTUsageValidation.h"
  19. NS_ASSUME_NONNULL_BEGIN
  20. @implementation FIRFieldPath
  21. - (instancetype)initWithFields:(NSArray<NSString *> *)fieldNames {
  22. if (fieldNames.count == 0) {
  23. FSTThrowInvalidArgument(@"Invalid field path. Provided names must not be empty.");
  24. }
  25. for (int i = 0; i < fieldNames.count; ++i) {
  26. if (fieldNames[i].length == 0) {
  27. FSTThrowInvalidArgument(@"Invalid field name at index %d. Field names must not be empty.", i);
  28. }
  29. }
  30. return [self initPrivate:[FSTFieldPath pathWithSegments:fieldNames]];
  31. }
  32. + (instancetype)documentID {
  33. return [[FIRFieldPath alloc] initPrivate:FSTFieldPath.keyFieldPath];
  34. }
  35. - (instancetype)initPrivate:(FSTFieldPath *)fieldPath {
  36. if (self = [super init]) {
  37. _internalValue = fieldPath;
  38. }
  39. return self;
  40. }
  41. + (instancetype)pathWithDotSeparatedString:(NSString *)path {
  42. if ([[FIRFieldPath reservedCharactersRegex]
  43. numberOfMatchesInString:path
  44. options:0
  45. range:NSMakeRange(0, path.length)] > 0) {
  46. FSTThrowInvalidArgument(
  47. @"Invalid field path (%@). Paths must not contain '~', '*', '/', '[', or ']'", path);
  48. }
  49. @try {
  50. return [[FIRFieldPath alloc] initWithFields:[path componentsSeparatedByString:@"."]];
  51. } @catch (NSException *exception) {
  52. FSTThrowInvalidArgument(
  53. @"Invalid field path (%@). Paths must not be empty, begin with '.', end with '.', or "
  54. @"contain '..'",
  55. path);
  56. }
  57. }
  58. /** Matches any characters in a field path string that are reserved. */
  59. + (NSRegularExpression *)reservedCharactersRegex {
  60. static NSRegularExpression *regex = nil;
  61. static dispatch_once_t onceToken;
  62. dispatch_once(&onceToken, ^{
  63. regex = [NSRegularExpression regularExpressionWithPattern:@"[~*/\\[\\]]" options:0 error:nil];
  64. });
  65. return regex;
  66. }
  67. - (id)copyWithZone:(NSZone *__nullable)zone {
  68. return [[[self class] alloc] initPrivate:self.internalValue];
  69. }
  70. - (BOOL)isEqual:(id)object {
  71. if (self == object) {
  72. return YES;
  73. }
  74. if (![object isKindOfClass:[FIRFieldPath class]]) {
  75. return NO;
  76. }
  77. return [self.internalValue isEqual:((FIRFieldPath *)object).internalValue];
  78. }
  79. - (NSUInteger)hash {
  80. return [self.internalValue hash];
  81. }
  82. @end
  83. NS_ASSUME_NONNULL_END