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