FSTFieldValue.h 8.1 KB

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