FSTPath.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <Foundation/Foundation.h>
  17. NS_ASSUME_NONNULL_BEGIN
  18. /**
  19. * FSTPath represents a path sequence in the Firestore database. It is composed of an ordered
  20. * sequence of string segments.
  21. *
  22. * ## Subclassing Notes
  23. *
  24. * FSTPath itself is an abstract class that must be specialized by subclasses. Subclasses should
  25. * implement constructors for common string-based representations of the path and also override
  26. * -canonicalString which converts back to the canonical string-based representation of the path.
  27. */
  28. @interface FSTPath <SelfType> : NSObject
  29. /** Returns the path segment of the given index. */
  30. - (NSString *)segmentAtIndex:(int)index;
  31. - (id)objectAtIndexedSubscript:(int)index;
  32. - (BOOL)isEqual:(id)path;
  33. - (NSComparisonResult)compare:(SelfType)other;
  34. /**
  35. * Returns a new path whose segments are the current path's plus one more.
  36. *
  37. * @param segment The new segment to concatenate to the path.
  38. * @return A new path with this path's segment plus the new one.
  39. */
  40. - (instancetype)pathByAppendingSegment:(NSString *)segment;
  41. /**
  42. * Returns a new path whose segments are the current path's plus another's.
  43. *
  44. * @param path The new path whose segments should be concatenated to the path.
  45. * @return A new path with this path's segment plus the new ones.
  46. */
  47. - (instancetype)pathByAppendingPath:(SelfType)path;
  48. /** Returns a new path whose segments are the same as this one's minus the first one. */
  49. - (instancetype)pathByRemovingFirstSegment;
  50. /** Returns a new path whose segments are the same as this one's minus the first `count`. */
  51. - (instancetype)pathByRemovingFirstSegments:(int)count;
  52. /** Returns a new path whose segments are the same as this one's minus the last one. */
  53. - (instancetype)pathByRemovingLastSegment;
  54. /** Convenience method for getting the first segment of this path. */
  55. - (NSString *)firstSegment;
  56. /** Convenience method for getting the last segment of this path. */
  57. - (NSString *)lastSegment;
  58. /** Returns true if this path is a prefix of the given path. */
  59. - (BOOL)isPrefixOfPath:(SelfType)other;
  60. /** Returns a standardized string representation of this path. */
  61. - (NSString *)canonicalString;
  62. /** The number of segments in the path. */
  63. @property(nonatomic, readonly) int length;
  64. /** True if the path is empty. */
  65. @property(nonatomic, readonly, getter=isEmpty) BOOL empty;
  66. @end
  67. /** A dot-separated path for navigating sub-objects within a document. */
  68. @class FSTFieldPath;
  69. @interface FSTFieldPath : FSTPath <FSTFieldPath *>
  70. /**
  71. * Creates and returns a new path with the given segments. The array of segments is not copied, so
  72. * one should not mutate the array once it is passed in here.
  73. *
  74. * @param segments The underlying array of segments for the path.
  75. * @return A new instance of FSTPath.
  76. */
  77. + (instancetype)pathWithSegments:(NSArray<NSString *> *)segments;
  78. /**
  79. * Creates and returns a new path from the server formatted field-path string, where path segments
  80. * are separated by a dot "." and optionally encoded using backticks.
  81. *
  82. * @param fieldPath A dot-separated string representing the path.
  83. */
  84. + (instancetype)pathWithServerFormat:(NSString *)fieldPath;
  85. /** Returns a field path that represents a document key. */
  86. + (instancetype)keyFieldPath;
  87. /** Returns a field path that represents an empty path. */
  88. + (instancetype)emptyPath;
  89. /** Returns YES if this is the `FSTFieldPath.keyFieldPath` field path. */
  90. - (BOOL)isKeyFieldPath;
  91. @end
  92. /** A slash-separated path for navigating resources (documents and collections) within Firestore. */
  93. @class FSTResourcePath;
  94. @interface FSTResourcePath : FSTPath <FSTResourcePath *>
  95. /**
  96. * Creates and returns a new path with the given segments. The array of segments is not copied, so
  97. * one should not mutate the array once it is passed in here.
  98. *
  99. * @param segments The underlying array of segments for the path.
  100. * @return A new instance of FSTPath.
  101. */
  102. + (instancetype)pathWithSegments:(NSArray<NSString *> *)segments;
  103. /**
  104. * Creates and returns a new path from the given resource-path string, where the path segments are
  105. * separated by a slash "/".
  106. *
  107. * @param resourcePath A slash-separated string representing the path.
  108. */
  109. + (instancetype)pathWithString:(NSString *)resourcePath;
  110. @end
  111. NS_ASSUME_NONNULL_END