FSTFieldValue.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. #include "Firestore/core/src/firebase/firestore/model/field_value.h"
  22. using firebase::firestore::model::FieldValue;
  23. @class FSTDocumentKey;
  24. @class FIRTimestamp;
  25. @class FSTFieldValueOptions;
  26. @class FIRGeoPoint;
  27. NS_ASSUME_NONNULL_BEGIN
  28. /** The order of types in Firestore; this order is defined by the backend. */
  29. typedef NS_ENUM(NSInteger, FSTTypeOrder) {
  30. FSTTypeOrderNull,
  31. FSTTypeOrderBoolean,
  32. FSTTypeOrderNumber,
  33. FSTTypeOrderTimestamp,
  34. FSTTypeOrderString,
  35. FSTTypeOrderBlob,
  36. FSTTypeOrderReference,
  37. FSTTypeOrderGeoPoint,
  38. FSTTypeOrderArray,
  39. FSTTypeOrderObject,
  40. };
  41. /** Defines the return value for pending server timestamps. */
  42. enum class ServerTimestampBehavior { None, Estimate, Previous };
  43. /** Holds properties that define field value deserialization options. */
  44. @interface FSTFieldValueOptions : NSObject
  45. @property(nonatomic, readonly, assign) ServerTimestampBehavior serverTimestampBehavior;
  46. @property(nonatomic) BOOL timestampsInSnapshotsEnabled;
  47. - (instancetype)init NS_UNAVAILABLE;
  48. /**
  49. * Creates an FSTFieldValueOptions instance that specifies deserialization behavior for pending
  50. * server timestamps.
  51. */
  52. - (instancetype)initWithServerTimestampBehavior:(ServerTimestampBehavior)serverTimestampBehavior
  53. timestampsInSnapshotsEnabled:(BOOL)timestampsInSnapshotsEnabled
  54. NS_DESIGNATED_INITIALIZER;
  55. @end
  56. /**
  57. * Abstract base class representing an immutable data value as stored in Firestore. FSTFieldValue
  58. * represents all the different kinds of values that can be stored in fields in a document.
  59. *
  60. * Supported types are:
  61. * - Null
  62. * - Boolean
  63. * - Long
  64. * - Double
  65. * - Timestamp
  66. * - ServerTimestamp (a sentinel used in uncommitted writes)
  67. * - String
  68. * - Binary
  69. * - (Document) References
  70. * - GeoPoint
  71. * - Array
  72. * - Object
  73. */
  74. @interface FSTFieldValue<__covariant T> : NSObject
  75. /**
  76. * Returns the 'type' of this FSTFieldValue. Used for RTTI (rather than isKindOfClass)
  77. * to ease migration to C++.
  78. */
  79. @property(nonatomic, assign, readonly) FieldValue::Type type;
  80. /** Returns the FSTTypeOrder for this value. */
  81. @property(nonatomic, assign, readonly) 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. * Base class inherited from by FSTIntegerValue and FSTDoubleValue. It implements proper number
  107. * comparisons between the two types.
  108. */
  109. @interface FSTNumberValue : FSTFieldValue <NSNumber *>
  110. @end
  111. /**
  112. * An integer value stored in Firestore.
  113. */
  114. @interface FSTIntegerValue : FSTNumberValue
  115. + (instancetype)integerValue:(int64_t)value;
  116. - (int64_t)internalValue;
  117. @end
  118. /**
  119. * A double-precision floating point number stored in Firestore.
  120. */
  121. @interface FSTDoubleValue : FSTNumberValue
  122. + (instancetype)doubleValue:(double)value;
  123. + (instancetype)nanValue;
  124. - (double)internalValue;
  125. @end
  126. /**
  127. * A timestamp value stored in Firestore.
  128. */
  129. @interface FSTTimestampValue : FSTFieldValue <FIRTimestamp *>
  130. + (instancetype)timestampValue:(FIRTimestamp *)value;
  131. @end
  132. /**
  133. * Represents a locally-applied Server Timestamp.
  134. *
  135. * Notes:
  136. * - FSTServerTimestampValue instances are created as the result of applying an FSTTransformMutation
  137. * (see [FSTTransformMutation applyTo]). They can only exist in the local view of a document.
  138. * Therefore they do not need to be parsed or serialized.
  139. * - When evaluated locally (e.g. via FSTDocumentSnapshot data), they by default evaluate to NSNull.
  140. * This behavior can be configured by passing custom FSTFieldValueOptions to `valueWithOptions:`.
  141. * - They sort after all FSTTimestampValues. With respect to other FSTServerTimestampValues, they
  142. * sort by their localWriteTime.
  143. */
  144. @interface FSTServerTimestampValue : FSTFieldValue <id>
  145. + (instancetype)serverTimestampValueWithLocalWriteTime:(FIRTimestamp *)localWriteTime
  146. previousValue:(nullable FSTFieldValue *)previousValue;
  147. @property(nonatomic, strong, readonly) FIRTimestamp *localWriteTime;
  148. @property(nonatomic, strong, readonly, nullable) FSTFieldValue *previousValue;
  149. @end
  150. /**
  151. * A geo point value stored in Firestore.
  152. */
  153. @interface FSTGeoPointValue : FSTFieldValue <FIRGeoPoint *>
  154. + (instancetype)geoPointValue:(FIRGeoPoint *)value;
  155. @end
  156. /**
  157. * A blob value stored in Firestore.
  158. */
  159. @interface FSTBlobValue : FSTFieldValue <NSData *>
  160. + (instancetype)blobValue:(NSData *)value;
  161. @end
  162. /**
  163. * A reference value stored in Firestore.
  164. */
  165. @interface FSTReferenceValue : FSTFieldValue <FSTDocumentKey *>
  166. + (instancetype)referenceValue:(FSTDocumentKey *)value
  167. databaseID:(const firebase::firestore::model::DatabaseId *)databaseID;
  168. // Does not own this DatabaseId.
  169. @property(nonatomic, assign, readonly) const firebase::firestore::model::DatabaseId *databaseID;
  170. @end
  171. /**
  172. * A structured object value stored in Firestore.
  173. */
  174. // clang-format off
  175. @interface FSTObjectValue : FSTFieldValue < NSDictionary<NSString *, id> * >
  176. - (instancetype)init NS_UNAVAILABLE;
  177. // clang-format on
  178. /** Returns an empty FSTObjectValue. */
  179. + (instancetype)objectValue;
  180. /**
  181. * Initializes this FSTObjectValue with the given dictionary.
  182. */
  183. - (instancetype)initWithDictionary:(NSDictionary<NSString *, FSTFieldValue *> *)value;
  184. /**
  185. * Initializes this FSTObjectValue with the given immutable dictionary.
  186. */
  187. - (instancetype)initWithImmutableDictionary:
  188. (FSTImmutableSortedDictionary<NSString *, FSTFieldValue *> *)value NS_DESIGNATED_INITIALIZER;
  189. - (FSTImmutableSortedDictionary<NSString *, FSTFieldValue *> *)internalValue;
  190. /** Returns the value at the given path if it exists. Returns nil otherwise. */
  191. - (nullable FSTFieldValue *)valueForPath:(const firebase::firestore::model::FieldPath &)fieldPath;
  192. /**
  193. * Returns a new object where the field at the named path has its value set to the given value.
  194. * This object remains unmodified.
  195. */
  196. - (FSTObjectValue *)objectBySettingValue:(FSTFieldValue *)value
  197. forPath:(const firebase::firestore::model::FieldPath &)fieldPath;
  198. /**
  199. * Returns a new object where the field at the named path has been removed. If any segment of the
  200. * path does not exist within this object's structure, no change is performed.
  201. */
  202. - (FSTObjectValue *)objectByDeletingPath:(const firebase::firestore::model::FieldPath &)fieldPath;
  203. /**
  204. * Applies this field mask to the provided object value and returns an object that only contains
  205. * fields that are specified in both the input object and this field mask.
  206. */
  207. // TODO(mrschmidt): Once FieldValues are C++, move this to FieldMask to match other platforms.
  208. - (FSTObjectValue *)objectByApplyingFieldMask:
  209. (const firebase::firestore::model::FieldMask &)fieldMask;
  210. @end
  211. /**
  212. * An array value stored in Firestore.
  213. */
  214. // clang-format off
  215. @interface FSTArrayValue : FSTFieldValue < NSArray <id> * >
  216. - (instancetype)init NS_UNAVAILABLE;
  217. // clang-format on
  218. /**
  219. * Initializes this instance with the given array of wrapped values.
  220. *
  221. * @param value An immutable array of FSTFieldValue objects. Caller is responsible for copying the
  222. * value or releasing all references.
  223. */
  224. - (instancetype)initWithValueNoCopy:(NSArray<FSTFieldValue *> *)value NS_DESIGNATED_INITIALIZER;
  225. - (NSArray<FSTFieldValue *> *)internalValue;
  226. @end
  227. /**
  228. * A value that delegates to the c++ model::FieldValue.
  229. */
  230. @interface FSTDelegateValue : FSTFieldValue <id>
  231. + (instancetype)delegateWithValue:(FieldValue &&)value;
  232. - (const FieldValue &)internalValue;
  233. @end
  234. NS_ASSUME_NONNULL_END