FIRFieldPath.mm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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> converted;
  41. converted.reserve(fieldNames.count);
  42. for (NSString *fieldName in fieldNames) {
  43. converted.emplace_back(util::MakeString(fieldName));
  44. }
  45. return [self initPrivate:FieldPath::FromSegments(std::move(converted))];
  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. return
  58. [[FIRFieldPath alloc] initPrivate:FieldPath::FromDotSeparatedString(util::MakeString(path))];
  59. }
  60. /** Matches any characters in a field path string that are reserved. */
  61. + (NSRegularExpression *)reservedCharactersRegex {
  62. static NSRegularExpression *regex = nil;
  63. static dispatch_once_t onceToken;
  64. dispatch_once(&onceToken, ^{
  65. regex = [NSRegularExpression regularExpressionWithPattern:@"[~*/\\[\\]]" options:0 error:nil];
  66. });
  67. return regex;
  68. }
  69. - (id)copyWithZone:(NSZone *__nullable)zone {
  70. return [[[self class] alloc] initPrivate:_internalValue];
  71. }
  72. - (BOOL)isEqual:(nullable id)object {
  73. if (self == object) {
  74. return YES;
  75. }
  76. if (![object isKindOfClass:[FIRFieldPath class]]) {
  77. return NO;
  78. }
  79. return _internalValue == ((FIRFieldPath *)object)->_internalValue;
  80. }
  81. - (NSUInteger)hash {
  82. return util::Hash(_internalValue);
  83. }
  84. - (const firebase::firestore::model::FieldPath &)internalValue {
  85. return _internalValue;
  86. }
  87. @end
  88. NS_ASSUME_NONNULL_END