FSTFieldValue.h 8.4 KB

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