FSTFieldValue.h 8.6 KB

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