FSTFieldValue.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. namespace model = firebase::firestore::model;
  26. NS_ASSUME_NONNULL_BEGIN
  27. /** The order of types in Firestore; this order is defined by the backend. */
  28. typedef NS_ENUM(NSInteger, FSTTypeOrder) {
  29. FSTTypeOrderNull,
  30. FSTTypeOrderBoolean,
  31. FSTTypeOrderNumber,
  32. FSTTypeOrderTimestamp,
  33. FSTTypeOrderString,
  34. FSTTypeOrderBlob,
  35. FSTTypeOrderReference,
  36. FSTTypeOrderGeoPoint,
  37. FSTTypeOrderArray,
  38. FSTTypeOrderObject,
  39. };
  40. /**
  41. * Abstract base class representing an immutable data value as stored in Firestore. FSTFieldValue
  42. * represents all the different kinds of values that can be stored in fields in a document.
  43. *
  44. * Supported types are:
  45. * - Null
  46. * - Boolean
  47. * - Long
  48. * - Double
  49. * - Timestamp
  50. * - ServerTimestamp (a sentinel used in uncommitted writes)
  51. * - String
  52. * - Binary
  53. * - (Document) References
  54. * - GeoPoint
  55. * - Array
  56. * - Object
  57. */
  58. @interface FSTFieldValue<__covariant T> : NSObject
  59. /**
  60. * Returns the 'type' of this FSTFieldValue. Used for RTTI (rather than isKindOfClass)
  61. * to ease migration to C++.
  62. */
  63. @property(nonatomic, assign, readonly) model::FieldValue::Type type;
  64. /** Returns the FSTTypeOrder for this value. */
  65. @property(nonatomic, assign, readonly) FSTTypeOrder typeOrder;
  66. /**
  67. * Converts an FSTFieldValue into the value that users will see in document snapshots.
  68. *
  69. * TODO(mikelehen): This conversion should probably happen at the API level and right now `value` is
  70. * used inappropriately in the serializer implementation, etc. We need to do some reworking.
  71. */
  72. - (T)value;
  73. /**
  74. * Converts an FSTFieldValue into the value that users will see in document snapshots.
  75. *
  76. * Options can be provided to configure the deserialization of some field values (such as server
  77. * timestamps).
  78. */
  79. - (T)valueWithOptions:(const model::FieldValueOptions &)options;
  80. /** Compares against another FSTFieldValue. */
  81. - (NSComparisonResult)compare:(FSTFieldValue *)other;
  82. /** Accesses this FSTFieldValue as an integer. */
  83. - (int64_t)integerValue;
  84. - (bool)isNAN;
  85. /** Accesses this FSTFieldValue as an double. */
  86. - (double)doubleValue;
  87. @end
  88. /**
  89. * Represents a locally-applied Server Timestamp.
  90. *
  91. * Notes:
  92. * - FSTServerTimestampValue instances are created as the result of applying an FSTTransformMutation
  93. * (see [FSTTransformMutation applyTo]). They can only exist in the local view of a document.
  94. * Therefore they do not need to be parsed or serialized.
  95. * - When evaluated locally (e.g. via FSTDocumentSnapshot data), they by default evaluate to NSNull.
  96. * This behavior can be configured by passing custom FieldValueOptions to `valueWithOptions:`.
  97. * - They sort after all Timestamp values. With respect to other FSTServerTimestampValues, they
  98. * sort by their localWriteTime.
  99. */
  100. @interface FSTServerTimestampValue : FSTFieldValue <id>
  101. + (instancetype)serverTimestampValueWithLocalWriteTime:(const firebase::Timestamp &)localWriteTime
  102. previousValue:(nullable FSTFieldValue *)previousValue;
  103. @property(nonatomic, assign, readonly) const firebase::Timestamp &localWriteTime;
  104. @property(nonatomic, strong, readonly, nullable) FSTFieldValue *previousValue;
  105. @end
  106. /**
  107. * A reference value stored in Firestore.
  108. */
  109. @interface FSTReferenceValue : FSTFieldValue <FSTDocumentKey *>
  110. + (instancetype)referenceValue:(FSTDocumentKey *)value databaseID:(model::DatabaseId)databaseID;
  111. @property(nonatomic, assign, readonly) const model::DatabaseId &databaseID;
  112. @end
  113. /**
  114. * A structured object value stored in Firestore.
  115. */
  116. // clang-format off
  117. @interface FSTObjectValue : FSTFieldValue < NSDictionary<NSString *, id> * >
  118. - (instancetype)init NS_UNAVAILABLE;
  119. // clang-format on
  120. /** Returns an empty FSTObjectValue. */
  121. + (instancetype)objectValue;
  122. /**
  123. * Initializes this FSTObjectValue with the given dictionary.
  124. */
  125. - (instancetype)initWithDictionary:(NSDictionary<NSString *, FSTFieldValue *> *)value;
  126. /**
  127. * Initializes this FSTObjectValue with the given immutable dictionary.
  128. */
  129. - (instancetype)initWithImmutableDictionary:
  130. (FSTImmutableSortedDictionary<NSString *, FSTFieldValue *> *)value NS_DESIGNATED_INITIALIZER;
  131. - (FSTImmutableSortedDictionary<NSString *, FSTFieldValue *> *)internalValue;
  132. /** Returns the value at the given path if it exists. Returns nil otherwise. */
  133. - (nullable FSTFieldValue *)valueForPath:(const model::FieldPath &)fieldPath;
  134. /**
  135. * Returns a new object where the field at the named path has its value set to the given value.
  136. * This object remains unmodified.
  137. */
  138. - (FSTObjectValue *)objectBySettingValue:(FSTFieldValue *)value
  139. forPath:(const model::FieldPath &)fieldPath;
  140. /**
  141. * Returns a new object where the field at the named path has been removed. If any segment of the
  142. * path does not exist within this object's structure, no change is performed.
  143. */
  144. - (FSTObjectValue *)objectByDeletingPath:(const model::FieldPath &)fieldPath;
  145. /**
  146. * Applies this field mask to the provided object value and returns an object that only contains
  147. * fields that are specified in both the input object and this field mask.
  148. */
  149. // TODO(mrschmidt): Once FieldValues are C++, move this to FieldMask to match other platforms.
  150. - (FSTObjectValue *)objectByApplyingFieldMask:(const model::FieldMask &)fieldMask;
  151. @end
  152. /**
  153. * An array value stored in Firestore.
  154. */
  155. // clang-format off
  156. @interface FSTArrayValue : FSTFieldValue < NSArray <id> * >
  157. - (instancetype)init NS_UNAVAILABLE;
  158. // clang-format on
  159. /**
  160. * Initializes this instance with the given array of wrapped values.
  161. *
  162. * @param value An immutable array of FSTFieldValue objects. Caller is responsible for copying the
  163. * value or releasing all references.
  164. */
  165. - (instancetype)initWithValueNoCopy:(NSArray<FSTFieldValue *> *)value NS_DESIGNATED_INITIALIZER;
  166. - (NSArray<FSTFieldValue *> *)internalValue;
  167. @end
  168. /**
  169. * A value that delegates to the c++ model::FieldValue.
  170. */
  171. @interface FSTDelegateValue : FSTFieldValue <id>
  172. + (instancetype)delegateWithValue:(model::FieldValue &&)value;
  173. - (const model::FieldValue &)internalValue;
  174. @end
  175. NS_ASSUME_NONNULL_END