FSTFieldValue.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #import "Firestore/third_party/Immutable/FSTImmutableSortedDictionary.h"
  18. @class FSTDatabaseID;
  19. @class FSTDocumentKey;
  20. @class FSTFieldPath;
  21. @class FSTTimestamp;
  22. @class FIRGeoPoint;
  23. NS_ASSUME_NONNULL_BEGIN
  24. /** The order of types in Firestore; this order is defined by the backend. */
  25. typedef NS_ENUM(NSInteger, FSTTypeOrder) {
  26. FSTTypeOrderNull,
  27. FSTTypeOrderBoolean,
  28. FSTTypeOrderNumber,
  29. FSTTypeOrderTimestamp,
  30. FSTTypeOrderString,
  31. FSTTypeOrderBlob,
  32. FSTTypeOrderReference,
  33. FSTTypeOrderGeoPoint,
  34. FSTTypeOrderArray,
  35. FSTTypeOrderObject,
  36. };
  37. /**
  38. * Abstract base class representing an immutable data value as stored in Firestore. FSTFieldValue
  39. * represents all the different kinds of values that can be stored in fields in a document.
  40. *
  41. * Supported types are:
  42. * - Null
  43. * - Boolean
  44. * - Long
  45. * - Double
  46. * - Timestamp
  47. * - ServerTimestamp (a sentinel used in uncommitted writes)
  48. * - String
  49. * - Binary
  50. * - (Document) References
  51. * - GeoPoint
  52. * - Array
  53. * - Object
  54. */
  55. @interface FSTFieldValue : NSObject
  56. /** Returns the FSTTypeOrder for this value. */
  57. - (FSTTypeOrder)typeOrder;
  58. /**
  59. * Converts an FSTFieldValue into the value that users will see in document snapshots.
  60. *
  61. * TODO(mikelehen): This conversion should probably happen at the API level and right now `value` is
  62. * used inappropriately in the serializer implementation, etc. We need to do some reworking.
  63. */
  64. - (id)value;
  65. /** Compares against another FSTFieldValue. */
  66. - (NSComparisonResult)compare:(FSTFieldValue *)other;
  67. @end
  68. /**
  69. * A null value stored in Firestore. The |value| of a FSTNullValue is [NSNull null].
  70. */
  71. @interface FSTNullValue : FSTFieldValue
  72. + (instancetype)nullValue;
  73. - (id)value;
  74. @end
  75. /**
  76. * A boolean value stored in Firestore.
  77. */
  78. @interface FSTBooleanValue : FSTFieldValue
  79. + (instancetype)trueValue;
  80. + (instancetype)falseValue;
  81. + (instancetype)booleanValue:(BOOL)value;
  82. - (NSNumber *)value;
  83. @end
  84. /**
  85. * Base class inherited from by FSTIntegerValue and FSTDoubleValue. It implements proper number
  86. * comparisons between the two types.
  87. */
  88. @interface FSTNumberValue : FSTFieldValue
  89. @end
  90. /**
  91. * An integer value stored in Firestore.
  92. */
  93. @interface FSTIntegerValue : FSTNumberValue
  94. + (instancetype)integerValue:(int64_t)value;
  95. - (NSNumber *)value;
  96. - (int64_t)internalValue;
  97. @end
  98. /**
  99. * A double-precision floating point number stored in Firestore.
  100. */
  101. @interface FSTDoubleValue : FSTNumberValue
  102. + (instancetype)doubleValue:(double)value;
  103. + (instancetype)nanValue;
  104. - (NSNumber *)value;
  105. - (double)internalValue;
  106. @end
  107. /**
  108. * A string stored in Firestore.
  109. */
  110. @interface FSTStringValue : FSTFieldValue
  111. + (instancetype)stringValue:(NSString *)value;
  112. - (NSString *)value;
  113. @end
  114. /**
  115. * A timestamp value stored in Firestore.
  116. */
  117. @interface FSTTimestampValue : FSTFieldValue
  118. + (instancetype)timestampValue:(FSTTimestamp *)value;
  119. - (NSDate *)value;
  120. - (FSTTimestamp *)internalValue;
  121. @end
  122. /**
  123. * Represents a locally-applied Server Timestamp.
  124. *
  125. * Notes:
  126. * - FSTServerTimestampValue instances are created as the result of applying an FSTTransformMutation
  127. * (see [FSTTransformMutation applyTo]). They can only exist in the local view of a document.
  128. * Therefore they do not need to be parsed or serialized.
  129. * - When evaluated locally (e.g. via FSTDocumentSnapshot data), they evaluate to NSNull (at least
  130. * for now, see b/62064202).
  131. * - They sort after all FSTTimestampValues. With respect to other FSTServerTimestampValues, they
  132. * sort by their localWriteTime.
  133. */
  134. @interface FSTServerTimestampValue : FSTFieldValue
  135. + (instancetype)serverTimestampValueWithLocalWriteTime:(FSTTimestamp *)localWriteTime;
  136. - (NSNull *)value;
  137. @property(nonatomic, strong, readonly) FSTTimestamp *localWriteTime;
  138. @end
  139. /**
  140. * A geo point value stored in Firestore.
  141. */
  142. @interface FSTGeoPointValue : FSTFieldValue
  143. + (instancetype)geoPointValue:(FIRGeoPoint *)value;
  144. - (FIRGeoPoint *)value;
  145. @end
  146. /**
  147. * A blob value stored in Firestore.
  148. */
  149. @interface FSTBlobValue : FSTFieldValue
  150. + (instancetype)blobValue:(NSData *)value;
  151. - (NSData *)value;
  152. @end
  153. /**
  154. * A reference value stored in Firestore.
  155. */
  156. @interface FSTReferenceValue : FSTFieldValue
  157. + (instancetype)referenceValue:(FSTDocumentKey *)value databaseID:(FSTDatabaseID *)databaseID;
  158. - (FSTDocumentKey *)value;
  159. @property(nonatomic, strong, readonly) FSTDatabaseID *databaseID;
  160. @end
  161. /**
  162. * A structured object value stored in Firestore.
  163. */
  164. @interface FSTObjectValue : FSTFieldValue
  165. /** Returns an empty FSTObjectValue. */
  166. + (instancetype)objectValue;
  167. /**
  168. * Initializes this FSTObjectValue with the given dictionary.
  169. */
  170. - (instancetype)initWithDictionary:(NSDictionary<NSString *, FSTFieldValue *> *)value;
  171. /**
  172. * Initializes this FSTObjectValue with the given immutable dictionary.
  173. */
  174. - (instancetype)initWithImmutableDictionary:
  175. (FSTImmutableSortedDictionary<NSString *, FSTFieldValue *> *)value NS_DESIGNATED_INITIALIZER;
  176. - (instancetype)init NS_UNAVAILABLE;
  177. - (NSDictionary<NSString *, id> *)value;
  178. - (FSTImmutableSortedDictionary<NSString *, FSTFieldValue *> *)internalValue;
  179. /** Returns the value at the given path if it exists. Returns nil otherwise. */
  180. - (nullable FSTFieldValue *)valueForPath:(FSTFieldPath *)fieldPath;
  181. /**
  182. * Returns a new object where the field at the named path has its value set to the given value.
  183. * This object remains unmodified.
  184. */
  185. - (FSTObjectValue *)objectBySettingValue:(FSTFieldValue *)value forPath:(FSTFieldPath *)fieldPath;
  186. /**
  187. * Returns a new object where the field at the named path has been removed. If any segment of the
  188. * path does not exist within this object's structure, no change is performed.
  189. */
  190. - (FSTObjectValue *)objectByDeletingPath:(FSTFieldPath *)fieldPath;
  191. @end
  192. /**
  193. * An array value stored in Firestore.
  194. */
  195. @interface FSTArrayValue : FSTFieldValue
  196. /**
  197. * Initializes this instance with the given array of wrapped values.
  198. *
  199. * @param value An immutable array of FSTFieldValue objects. Caller is responsible for copying the
  200. * value or releasing all references.
  201. */
  202. - (instancetype)initWithValueNoCopy:(NSArray<FSTFieldValue *> *)value NS_DESIGNATED_INITIALIZER;
  203. - (instancetype)init NS_UNAVAILABLE;
  204. - (NSArray<id> *)value;
  205. - (NSArray<FSTFieldValue *> *)internalValue;
  206. @end
  207. NS_ASSUME_NONNULL_END