FSTFieldValue.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. /** Accesses this FSTFieldValue as an integer. */
  84. - (int64_t)integerValue;
  85. - (bool)isNAN;
  86. /** Accesses this FSTFieldValue as an double. */
  87. - (double)doubleValue;
  88. @end
  89. /**
  90. * A timestamp value stored in Firestore.
  91. */
  92. @interface FSTTimestampValue : FSTFieldValue <FIRTimestamp *>
  93. + (instancetype)timestampValue:(FIRTimestamp *)value;
  94. @end
  95. /**
  96. * Represents a locally-applied Server Timestamp.
  97. *
  98. * Notes:
  99. * - FSTServerTimestampValue instances are created as the result of applying an FSTTransformMutation
  100. * (see [FSTTransformMutation applyTo]). They can only exist in the local view of a document.
  101. * Therefore they do not need to be parsed or serialized.
  102. * - When evaluated locally (e.g. via FSTDocumentSnapshot data), they by default evaluate to NSNull.
  103. * This behavior can be configured by passing custom FieldValueOptions to `valueWithOptions:`.
  104. * - They sort after all FSTTimestampValues. With respect to other FSTServerTimestampValues, they
  105. * sort by their localWriteTime.
  106. */
  107. @interface FSTServerTimestampValue : FSTFieldValue <id>
  108. + (instancetype)serverTimestampValueWithLocalWriteTime:(FIRTimestamp *)localWriteTime
  109. previousValue:(nullable FSTFieldValue *)previousValue;
  110. @property(nonatomic, strong, readonly) FIRTimestamp *localWriteTime;
  111. @property(nonatomic, strong, readonly, nullable) FSTFieldValue *previousValue;
  112. @end
  113. /**
  114. * A blob value stored in Firestore.
  115. */
  116. @interface FSTBlobValue : FSTFieldValue <NSData *>
  117. + (instancetype)blobValue:(NSData *)value;
  118. @end
  119. /**
  120. * A reference value stored in Firestore.
  121. */
  122. @interface FSTReferenceValue : FSTFieldValue <FSTDocumentKey *>
  123. + (instancetype)referenceValue:(FSTDocumentKey *)value databaseID:(model::DatabaseId)databaseID;
  124. @property(nonatomic, assign, readonly) const model::DatabaseId &databaseID;
  125. @end
  126. /**
  127. * A structured object value stored in Firestore.
  128. */
  129. // clang-format off
  130. @interface FSTObjectValue : FSTFieldValue < NSDictionary<NSString *, id> * >
  131. - (instancetype)init NS_UNAVAILABLE;
  132. // clang-format on
  133. /** Returns an empty FSTObjectValue. */
  134. + (instancetype)objectValue;
  135. /**
  136. * Initializes this FSTObjectValue with the given dictionary.
  137. */
  138. - (instancetype)initWithDictionary:(NSDictionary<NSString *, FSTFieldValue *> *)value;
  139. /**
  140. * Initializes this FSTObjectValue with the given immutable dictionary.
  141. */
  142. - (instancetype)initWithImmutableDictionary:
  143. (FSTImmutableSortedDictionary<NSString *, FSTFieldValue *> *)value NS_DESIGNATED_INITIALIZER;
  144. - (FSTImmutableSortedDictionary<NSString *, FSTFieldValue *> *)internalValue;
  145. /** Returns the value at the given path if it exists. Returns nil otherwise. */
  146. - (nullable FSTFieldValue *)valueForPath:(const model::FieldPath &)fieldPath;
  147. /**
  148. * Returns a new object where the field at the named path has its value set to the given value.
  149. * This object remains unmodified.
  150. */
  151. - (FSTObjectValue *)objectBySettingValue:(FSTFieldValue *)value
  152. forPath:(const model::FieldPath &)fieldPath;
  153. /**
  154. * Returns a new object where the field at the named path has been removed. If any segment of the
  155. * path does not exist within this object's structure, no change is performed.
  156. */
  157. - (FSTObjectValue *)objectByDeletingPath:(const model::FieldPath &)fieldPath;
  158. /**
  159. * Applies this field mask to the provided object value and returns an object that only contains
  160. * fields that are specified in both the input object and this field mask.
  161. */
  162. // TODO(mrschmidt): Once FieldValues are C++, move this to FieldMask to match other platforms.
  163. - (FSTObjectValue *)objectByApplyingFieldMask:(const model::FieldMask &)fieldMask;
  164. @end
  165. /**
  166. * An array value stored in Firestore.
  167. */
  168. // clang-format off
  169. @interface FSTArrayValue : FSTFieldValue < NSArray <id> * >
  170. - (instancetype)init NS_UNAVAILABLE;
  171. // clang-format on
  172. /**
  173. * Initializes this instance with the given array of wrapped values.
  174. *
  175. * @param value An immutable array of FSTFieldValue objects. Caller is responsible for copying the
  176. * value or releasing all references.
  177. */
  178. - (instancetype)initWithValueNoCopy:(NSArray<FSTFieldValue *> *)value NS_DESIGNATED_INITIALIZER;
  179. - (NSArray<FSTFieldValue *> *)internalValue;
  180. @end
  181. /**
  182. * A value that delegates to the c++ model::FieldValue.
  183. */
  184. @interface FSTDelegateValue : FSTFieldValue <id>
  185. + (instancetype)delegateWithValue:(model::FieldValue &&)value;
  186. - (const model::FieldValue &)internalValue;
  187. @end
  188. NS_ASSUME_NONNULL_END