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