FSTFieldValue.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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 "Firestore/Source/Model/FSTFieldValue.h"
  17. #import "Firestore/Source/API/FIRGeoPoint+Internal.h"
  18. #import "Firestore/Source/Core/FSTTimestamp.h"
  19. #import "Firestore/Source/Model/FSTDatabaseID.h"
  20. #import "Firestore/Source/Model/FSTDocumentKey.h"
  21. #import "Firestore/Source/Model/FSTPath.h"
  22. #import "Firestore/Source/Util/FSTAssert.h"
  23. #import "Firestore/Source/Util/FSTClasses.h"
  24. #import "Firestore/Source/Util/FSTComparison.h"
  25. NS_ASSUME_NONNULL_BEGIN
  26. #pragma mark - FSTFieldValue
  27. @interface FSTFieldValue ()
  28. - (NSComparisonResult)defaultCompare:(FSTFieldValue *)other;
  29. @end
  30. @implementation FSTFieldValue
  31. - (FSTTypeOrder)typeOrder {
  32. @throw FSTAbstractMethodException(); // NOLINT
  33. }
  34. - (id)value {
  35. @throw FSTAbstractMethodException(); // NOLINT
  36. }
  37. - (BOOL)isEqual:(id)other {
  38. @throw FSTAbstractMethodException(); // NOLINT
  39. }
  40. - (NSUInteger)hash {
  41. @throw FSTAbstractMethodException(); // NOLINT
  42. }
  43. - (NSComparisonResult)compare:(FSTFieldValue *)other {
  44. @throw FSTAbstractMethodException(); // NOLINT
  45. }
  46. - (NSString *)description {
  47. return [[self value] description];
  48. }
  49. - (NSComparisonResult)defaultCompare:(FSTFieldValue *)other {
  50. if (self.typeOrder > other.typeOrder) {
  51. return NSOrderedDescending;
  52. } else {
  53. FSTAssert(self.typeOrder < other.typeOrder,
  54. @"defaultCompare should not be used for values of same type.");
  55. return NSOrderedAscending;
  56. }
  57. }
  58. @end
  59. #pragma mark - FSTNullValue
  60. @implementation FSTNullValue
  61. + (instancetype)nullValue {
  62. static FSTNullValue *sharedInstance = nil;
  63. static dispatch_once_t onceToken;
  64. dispatch_once(&onceToken, ^{
  65. sharedInstance = [[FSTNullValue alloc] init];
  66. });
  67. return sharedInstance;
  68. }
  69. - (FSTTypeOrder)typeOrder {
  70. return FSTTypeOrderNull;
  71. }
  72. - (id)value {
  73. return [NSNull null];
  74. }
  75. - (BOOL)isEqual:(id)other {
  76. return [other isKindOfClass:[self class]];
  77. }
  78. - (NSUInteger)hash {
  79. return 47;
  80. }
  81. - (NSComparisonResult)compare:(FSTFieldValue *)other {
  82. if ([other isKindOfClass:[self class]]) {
  83. return NSOrderedSame;
  84. } else {
  85. return [self defaultCompare:other];
  86. }
  87. }
  88. @end
  89. #pragma mark - FSTBooleanValue
  90. @interface FSTBooleanValue ()
  91. @property(nonatomic, assign, readonly) BOOL internalValue;
  92. @end
  93. @implementation FSTBooleanValue
  94. + (instancetype)trueValue {
  95. static FSTBooleanValue *sharedInstance = nil;
  96. static dispatch_once_t onceToken;
  97. dispatch_once(&onceToken, ^{
  98. sharedInstance = [[FSTBooleanValue alloc] initWithValue:YES];
  99. });
  100. return sharedInstance;
  101. }
  102. + (instancetype)falseValue {
  103. static FSTBooleanValue *sharedInstance = nil;
  104. static dispatch_once_t onceToken;
  105. dispatch_once(&onceToken, ^{
  106. sharedInstance = [[FSTBooleanValue alloc] initWithValue:NO];
  107. });
  108. return sharedInstance;
  109. }
  110. + (instancetype)booleanValue:(BOOL)value {
  111. return value ? [FSTBooleanValue trueValue] : [FSTBooleanValue falseValue];
  112. }
  113. - (id)initWithValue:(BOOL)value {
  114. self = [super init];
  115. if (self) {
  116. _internalValue = value;
  117. }
  118. return self;
  119. }
  120. - (FSTTypeOrder)typeOrder {
  121. return FSTTypeOrderBoolean;
  122. }
  123. - (id)value {
  124. return self.internalValue ? @YES : @NO;
  125. }
  126. - (BOOL)isEqual:(id)other {
  127. // Since we create shared instances for true / false, we can use reference equality.
  128. return self == other;
  129. }
  130. - (NSUInteger)hash {
  131. return self.internalValue ? 1231 : 1237;
  132. }
  133. - (NSComparisonResult)compare:(FSTFieldValue *)other {
  134. if ([other isKindOfClass:[FSTBooleanValue class]]) {
  135. return FSTCompareBools(self.internalValue, ((FSTBooleanValue *)other).internalValue);
  136. } else {
  137. return [self defaultCompare:other];
  138. }
  139. }
  140. @end
  141. #pragma mark - FSTNumberValue
  142. @implementation FSTNumberValue
  143. - (FSTTypeOrder)typeOrder {
  144. return FSTTypeOrderNumber;
  145. }
  146. - (NSComparisonResult)compare:(FSTFieldValue *)other {
  147. if (![other isKindOfClass:[FSTNumberValue class]]) {
  148. return [self defaultCompare:other];
  149. } else {
  150. if ([self isKindOfClass:[FSTDoubleValue class]]) {
  151. double thisDouble = ((FSTDoubleValue *)self).internalValue;
  152. if ([other isKindOfClass:[FSTDoubleValue class]]) {
  153. return FSTCompareDoubles(thisDouble, ((FSTDoubleValue *)other).internalValue);
  154. } else {
  155. FSTAssert([other isKindOfClass:[FSTIntegerValue class]], @"Unknown number value: %@",
  156. other);
  157. return FSTCompareMixed(thisDouble, ((FSTIntegerValue *)other).internalValue);
  158. }
  159. } else {
  160. int64_t thisInt = ((FSTIntegerValue *)self).internalValue;
  161. if ([other isKindOfClass:[FSTIntegerValue class]]) {
  162. return FSTCompareInt64s(thisInt, ((FSTIntegerValue *)other).internalValue);
  163. } else {
  164. FSTAssert([other isKindOfClass:[FSTDoubleValue class]], @"Unknown number value: %@", other);
  165. return -1 * FSTCompareMixed(((FSTDoubleValue *)other).internalValue, thisInt);
  166. }
  167. }
  168. }
  169. }
  170. @end
  171. #pragma mark - FSTIntegerValue
  172. @interface FSTIntegerValue ()
  173. @property(nonatomic, assign, readonly) int64_t internalValue;
  174. @end
  175. @implementation FSTIntegerValue
  176. + (instancetype)integerValue:(int64_t)value {
  177. return [[FSTIntegerValue alloc] initWithValue:value];
  178. }
  179. - (id)initWithValue:(int64_t)value {
  180. self = [super init];
  181. if (self) {
  182. _internalValue = value;
  183. }
  184. return self;
  185. }
  186. - (id)value {
  187. return @(self.internalValue);
  188. }
  189. - (BOOL)isEqual:(id)other {
  190. // NOTE: DoubleValue and LongValue instances may compare: the same, but that doesn't make them
  191. // equal via isEqual:
  192. return [other isKindOfClass:[FSTIntegerValue class]] &&
  193. self.internalValue == ((FSTIntegerValue *)other).internalValue;
  194. }
  195. - (NSUInteger)hash {
  196. return (((NSUInteger)self.internalValue) ^ (NSUInteger)(self.internalValue >> 32));
  197. }
  198. // NOTE: compare: is implemented in NumberValue.
  199. @end
  200. #pragma mark - FSTDoubleValue
  201. @interface FSTDoubleValue ()
  202. @property(nonatomic, assign, readonly) double internalValue;
  203. @end
  204. @implementation FSTDoubleValue
  205. + (instancetype)doubleValue:(double)value {
  206. // Normalize NaNs to match the behavior on the backend (which uses Double.doubletoLongBits()).
  207. if (isnan(value)) {
  208. return [FSTDoubleValue nanValue];
  209. }
  210. return [[FSTDoubleValue alloc] initWithValue:value];
  211. }
  212. + (instancetype)nanValue {
  213. static FSTDoubleValue *sharedInstance = nil;
  214. static dispatch_once_t onceToken;
  215. dispatch_once(&onceToken, ^{
  216. sharedInstance = [[FSTDoubleValue alloc] initWithValue:NAN];
  217. });
  218. return sharedInstance;
  219. }
  220. - (id)initWithValue:(double)value {
  221. self = [super init];
  222. if (self) {
  223. _internalValue = value;
  224. }
  225. return self;
  226. }
  227. - (id)value {
  228. return @(self.internalValue);
  229. }
  230. - (BOOL)isEqual:(id)other {
  231. // NOTE: DoubleValue and LongValue instances may compare: the same, but that doesn't make them
  232. // equal via isEqual:
  233. // NOTE: isEqual: should compare NaN equal to itself and -0.0 not equal to 0.0.
  234. return [other isKindOfClass:[FSTDoubleValue class]] &&
  235. FSTDoubleBitwiseEquals(self.internalValue, ((FSTDoubleValue *)other).internalValue);
  236. }
  237. - (NSUInteger)hash {
  238. return FSTDoubleBitwiseHash(self.internalValue);
  239. }
  240. // NOTE: compare: is implemented in NumberValue.
  241. @end
  242. #pragma mark - FSTStringValue
  243. @interface FSTStringValue ()
  244. @property(nonatomic, copy, readonly) NSString *internalValue;
  245. @end
  246. // TODO(b/37267885): Add truncation support
  247. @implementation FSTStringValue
  248. + (instancetype)stringValue:(NSString *)value {
  249. return [[FSTStringValue alloc] initWithValue:value];
  250. }
  251. - (id)initWithValue:(NSString *)value {
  252. self = [super init];
  253. if (self) {
  254. _internalValue = [value copy];
  255. }
  256. return self;
  257. }
  258. - (FSTTypeOrder)typeOrder {
  259. return FSTTypeOrderString;
  260. }
  261. - (id)value {
  262. return self.internalValue;
  263. }
  264. - (BOOL)isEqual:(id)other {
  265. return [other isKindOfClass:[FSTStringValue class]] &&
  266. [self.internalValue isEqualToString:((FSTStringValue *)other).internalValue];
  267. }
  268. - (NSUInteger)hash {
  269. return self.internalValue ? 1 : 0;
  270. }
  271. - (NSComparisonResult)compare:(FSTFieldValue *)other {
  272. if ([other isKindOfClass:[FSTStringValue class]]) {
  273. return FSTCompareStrings(self.internalValue, ((FSTStringValue *)other).internalValue);
  274. } else {
  275. return [self defaultCompare:other];
  276. }
  277. }
  278. @end
  279. #pragma mark - FSTTimestampValue
  280. @interface FSTTimestampValue ()
  281. @property(nonatomic, strong, readonly) FSTTimestamp *internalValue;
  282. @end
  283. @implementation FSTTimestampValue
  284. + (instancetype)timestampValue:(FSTTimestamp *)value {
  285. return [[FSTTimestampValue alloc] initWithValue:value];
  286. }
  287. - (id)initWithValue:(FSTTimestamp *)value {
  288. self = [super init];
  289. if (self) {
  290. _internalValue = value; // FSTTimestamp is immutable.
  291. }
  292. return self;
  293. }
  294. - (FSTTypeOrder)typeOrder {
  295. return FSTTypeOrderTimestamp;
  296. }
  297. - (id)value {
  298. // For developers, we expose Timestamps as Dates.
  299. return self.internalValue.approximateDateValue;
  300. }
  301. - (BOOL)isEqual:(id)other {
  302. return [other isKindOfClass:[FSTTimestampValue class]] &&
  303. [self.internalValue isEqual:((FSTTimestampValue *)other).internalValue];
  304. }
  305. - (NSUInteger)hash {
  306. return [self.internalValue hash];
  307. }
  308. - (NSComparisonResult)compare:(FSTFieldValue *)other {
  309. if ([other isKindOfClass:[FSTTimestampValue class]]) {
  310. return [self.internalValue compare:((FSTTimestampValue *)other).internalValue];
  311. } else if ([other isKindOfClass:[FSTServerTimestampValue class]]) {
  312. // Concrete timestamps come before server timestamps.
  313. return NSOrderedAscending;
  314. } else {
  315. return [self defaultCompare:other];
  316. }
  317. }
  318. @end
  319. #pragma mark - FSTServerTimestampValue
  320. @implementation FSTServerTimestampValue
  321. + (instancetype)serverTimestampValueWithLocalWriteTime:(FSTTimestamp *)localWriteTime {
  322. return [[FSTServerTimestampValue alloc] initWithLocalWriteTime:localWriteTime];
  323. }
  324. - (id)initWithLocalWriteTime:(FSTTimestamp *)localWriteTime {
  325. self = [super init];
  326. if (self) {
  327. _localWriteTime = localWriteTime;
  328. }
  329. return self;
  330. }
  331. - (FSTTypeOrder)typeOrder {
  332. return FSTTypeOrderTimestamp;
  333. }
  334. - (NSNull *)value {
  335. // For developers, server timestamps always evaluate to NSNull (for now, at least; b/62064202).
  336. return [NSNull null];
  337. }
  338. - (BOOL)isEqual:(id)other {
  339. return [other isKindOfClass:[FSTServerTimestampValue class]] &&
  340. [self.localWriteTime isEqual:((FSTServerTimestampValue *)other).localWriteTime];
  341. }
  342. - (NSUInteger)hash {
  343. return [self.localWriteTime hash];
  344. }
  345. - (NSString *)description {
  346. return [NSString stringWithFormat:@"<ServerTimestamp localTime=%@>", self.localWriteTime];
  347. }
  348. - (NSComparisonResult)compare:(FSTFieldValue *)other {
  349. if ([other isKindOfClass:[FSTServerTimestampValue class]]) {
  350. return [self.localWriteTime compare:((FSTServerTimestampValue *)other).localWriteTime];
  351. } else if ([other isKindOfClass:[FSTTimestampValue class]]) {
  352. // Server timestamps come after all concrete timestamps.
  353. return NSOrderedDescending;
  354. } else {
  355. return [self defaultCompare:other];
  356. }
  357. }
  358. @end
  359. #pragma mark - FSTGeoPointValue
  360. @interface FSTGeoPointValue ()
  361. @property(nonatomic, strong, readonly) FIRGeoPoint *internalValue;
  362. @end
  363. @implementation FSTGeoPointValue
  364. + (instancetype)geoPointValue:(FIRGeoPoint *)value {
  365. return [[FSTGeoPointValue alloc] initWithValue:value];
  366. }
  367. - (id)initWithValue:(FIRGeoPoint *)value {
  368. self = [super init];
  369. if (self) {
  370. _internalValue = value; // FIRGeoPoint is immutable.
  371. }
  372. return self;
  373. }
  374. - (FSTTypeOrder)typeOrder {
  375. return FSTTypeOrderGeoPoint;
  376. }
  377. - (id)value {
  378. return self.internalValue;
  379. }
  380. - (BOOL)isEqual:(id)other {
  381. return [other isKindOfClass:[FSTGeoPointValue class]] &&
  382. [self.internalValue isEqual:((FSTGeoPointValue *)other).internalValue];
  383. }
  384. - (NSUInteger)hash {
  385. return [self.internalValue hash];
  386. }
  387. - (NSComparisonResult)compare:(FSTFieldValue *)other {
  388. if ([other isKindOfClass:[FSTGeoPointValue class]]) {
  389. return [self.internalValue compare:((FSTGeoPointValue *)other).internalValue];
  390. } else {
  391. return [self defaultCompare:other];
  392. }
  393. }
  394. @end
  395. #pragma mark - FSTBlobValue
  396. @interface FSTBlobValue ()
  397. @property(nonatomic, copy, readonly) NSData *internalValue;
  398. @end
  399. // TODO(b/37267885): Add truncation support
  400. @implementation FSTBlobValue
  401. + (instancetype)blobValue:(NSData *)value {
  402. return [[FSTBlobValue alloc] initWithValue:value];
  403. }
  404. - (id)initWithValue:(NSData *)value {
  405. self = [super init];
  406. if (self) {
  407. _internalValue = [value copy];
  408. }
  409. return self;
  410. }
  411. - (FSTTypeOrder)typeOrder {
  412. return FSTTypeOrderBlob;
  413. }
  414. - (id)value {
  415. return self.internalValue;
  416. }
  417. - (BOOL)isEqual:(id)other {
  418. return [other isKindOfClass:[FSTBlobValue class]] &&
  419. [self.internalValue isEqual:((FSTBlobValue *)other).internalValue];
  420. }
  421. - (NSUInteger)hash {
  422. return [self.internalValue hash];
  423. }
  424. - (NSComparisonResult)compare:(FSTFieldValue *)other {
  425. if ([other isKindOfClass:[FSTBlobValue class]]) {
  426. return FSTCompareBytes(self.internalValue, ((FSTBlobValue *)other).internalValue);
  427. } else {
  428. return [self defaultCompare:other];
  429. }
  430. }
  431. @end
  432. #pragma mark - FSTReferenceValue
  433. @interface FSTReferenceValue ()
  434. @property(nonatomic, strong, readonly) FSTDocumentKey *key;
  435. @end
  436. @implementation FSTReferenceValue
  437. + (instancetype)referenceValue:(FSTDocumentKey *)value databaseID:(FSTDatabaseID *)databaseID {
  438. return [[FSTReferenceValue alloc] initWithValue:value databaseID:databaseID];
  439. }
  440. - (id)initWithValue:(FSTDocumentKey *)value databaseID:(FSTDatabaseID *)databaseID {
  441. self = [super init];
  442. if (self) {
  443. _key = value;
  444. _databaseID = databaseID;
  445. }
  446. return self;
  447. }
  448. - (id)value {
  449. return self.key;
  450. }
  451. - (FSTTypeOrder)typeOrder {
  452. return FSTTypeOrderReference;
  453. }
  454. - (BOOL)isEqual:(id)other {
  455. if (other == self) {
  456. return YES;
  457. }
  458. if (![other isKindOfClass:[FSTReferenceValue class]]) {
  459. return NO;
  460. }
  461. FSTReferenceValue *otherRef = (FSTReferenceValue *)other;
  462. return [self.key isEqualToKey:otherRef.key] &&
  463. [self.databaseID isEqualToDatabaseId:otherRef.databaseID];
  464. }
  465. - (NSUInteger)hash {
  466. NSUInteger result = [self.databaseID hash];
  467. result = 31 * result + [self.key hash];
  468. return result;
  469. }
  470. - (NSComparisonResult)compare:(FSTFieldValue *)other {
  471. if ([other isKindOfClass:[FSTReferenceValue class]]) {
  472. FSTReferenceValue *ref = (FSTReferenceValue *)other;
  473. NSComparisonResult cmp = [self.databaseID compare:ref.databaseID];
  474. return cmp != NSOrderedSame ? cmp : [self.key compare:ref.key];
  475. } else {
  476. return [self defaultCompare:other];
  477. }
  478. }
  479. @end
  480. #pragma mark - FSTObjectValue
  481. @interface FSTObjectValue ()
  482. @property(nonatomic, strong, readonly)
  483. FSTImmutableSortedDictionary<NSString *, FSTFieldValue *> *internalValue;
  484. @end
  485. @implementation FSTObjectValue
  486. + (instancetype)objectValue {
  487. static FSTObjectValue *sharedEmptyInstance = nil;
  488. static dispatch_once_t onceToken;
  489. dispatch_once(&onceToken, ^{
  490. FSTImmutableSortedDictionary<NSString *, FSTFieldValue *> *empty =
  491. [FSTImmutableSortedDictionary dictionaryWithComparator:FSTStringComparator];
  492. sharedEmptyInstance = [[FSTObjectValue alloc] initWithImmutableDictionary:empty];
  493. });
  494. return sharedEmptyInstance;
  495. }
  496. - (instancetype)initWithImmutableDictionary:
  497. (FSTImmutableSortedDictionary<NSString *, FSTFieldValue *> *)value {
  498. self = [super init];
  499. if (self) {
  500. _internalValue = value; // FSTImmutableSortedDictionary is immutable.
  501. }
  502. return self;
  503. }
  504. - (id)initWithDictionary:(NSDictionary<NSString *, FSTFieldValue *> *)value {
  505. FSTImmutableSortedDictionary<NSString *, FSTFieldValue *> *dictionary =
  506. [FSTImmutableSortedDictionary dictionaryWithDictionary:value comparator:FSTStringComparator];
  507. return [self initWithImmutableDictionary:dictionary];
  508. }
  509. - (id)value {
  510. NSMutableDictionary *result = [NSMutableDictionary dictionary];
  511. [self.internalValue
  512. enumerateKeysAndObjectsUsingBlock:^(NSString *key, FSTFieldValue *obj, BOOL *stop) {
  513. result[key] = [obj value];
  514. }];
  515. return result;
  516. }
  517. - (FSTTypeOrder)typeOrder {
  518. return FSTTypeOrderObject;
  519. }
  520. - (BOOL)isEqual:(id)other {
  521. if (other == self) {
  522. return YES;
  523. }
  524. if (![other isKindOfClass:[FSTObjectValue class]]) {
  525. return NO;
  526. }
  527. FSTObjectValue *otherObj = other;
  528. return [self.internalValue isEqual:otherObj.internalValue];
  529. }
  530. - (NSUInteger)hash {
  531. return [self.internalValue hash];
  532. }
  533. - (NSComparisonResult)compare:(FSTFieldValue *)other {
  534. if ([other isKindOfClass:[FSTObjectValue class]]) {
  535. FSTImmutableSortedDictionary *selfDict = self.internalValue;
  536. FSTImmutableSortedDictionary *otherDict = ((FSTObjectValue *)other).internalValue;
  537. NSEnumerator *enumerator1 = [selfDict keyEnumerator];
  538. NSEnumerator *enumerator2 = [otherDict keyEnumerator];
  539. NSString *key1 = [enumerator1 nextObject];
  540. NSString *key2 = [enumerator2 nextObject];
  541. while (key1 && key2) {
  542. NSComparisonResult keyCompare = [key1 compare:key2];
  543. if (keyCompare != NSOrderedSame) {
  544. return keyCompare;
  545. }
  546. NSComparisonResult valueCompare = [selfDict[key1] compare:otherDict[key2]];
  547. if (valueCompare != NSOrderedSame) {
  548. return valueCompare;
  549. }
  550. key1 = [enumerator1 nextObject];
  551. key2 = [enumerator2 nextObject];
  552. }
  553. // Only equal if both enumerators are exhausted.
  554. return FSTCompareBools(key1 != nil, key2 != nil);
  555. } else {
  556. return [self defaultCompare:other];
  557. }
  558. }
  559. - (nullable FSTFieldValue *)valueForPath:(FSTFieldPath *)fieldPath {
  560. FSTFieldValue *value = self;
  561. for (int i = 0, max = fieldPath.length; value && i < max; i++) {
  562. if (![value isMemberOfClass:[FSTObjectValue class]]) {
  563. return nil;
  564. }
  565. NSString *fieldName = fieldPath[i];
  566. value = ((FSTObjectValue *)value).internalValue[fieldName];
  567. }
  568. return value;
  569. }
  570. - (FSTObjectValue *)objectBySettingValue:(FSTFieldValue *)value forPath:(FSTFieldPath *)fieldPath {
  571. FSTAssert([fieldPath length] > 0, @"Cannot set value with an empty path");
  572. NSString *childName = [fieldPath firstSegment];
  573. if ([fieldPath length] == 1) {
  574. // Recursive base case:
  575. return [self objectBySettingValue:value forField:childName];
  576. } else {
  577. // Nested path. Recursively generate a new sub-object and then wrap a new FSTObjectValue around
  578. // the result.
  579. FSTFieldValue *child = [_internalValue objectForKey:childName];
  580. FSTObjectValue *childObject;
  581. if ([child isKindOfClass:[FSTObjectValue class]]) {
  582. childObject = (FSTObjectValue *)child;
  583. } else {
  584. // If the child is not found or is a primitive type, pretend as if an empty object lived
  585. // there.
  586. childObject = [FSTObjectValue objectValue];
  587. }
  588. FSTFieldValue *newChild =
  589. [childObject objectBySettingValue:value forPath:[fieldPath pathByRemovingFirstSegment]];
  590. return [self objectBySettingValue:newChild forField:childName];
  591. }
  592. }
  593. - (FSTObjectValue *)objectByDeletingPath:(FSTFieldPath *)fieldPath {
  594. FSTAssert([fieldPath length] > 0, @"Cannot delete an empty path");
  595. NSString *childName = [fieldPath firstSegment];
  596. if ([fieldPath length] == 1) {
  597. return [[FSTObjectValue alloc]
  598. initWithImmutableDictionary:[_internalValue dictionaryByRemovingObjectForKey:childName]];
  599. } else {
  600. FSTFieldValue *child = _internalValue[childName];
  601. if ([child isKindOfClass:[FSTObjectValue class]]) {
  602. FSTObjectValue *newChild =
  603. [((FSTObjectValue *)child) objectByDeletingPath:[fieldPath pathByRemovingFirstSegment]];
  604. return [self objectBySettingValue:newChild forField:childName];
  605. } else {
  606. // If the child is not found or is a primitive type, make no modifications
  607. return self;
  608. }
  609. }
  610. }
  611. - (FSTObjectValue *)objectBySettingValue:(FSTFieldValue *)value forField:(NSString *)field {
  612. return [[FSTObjectValue alloc]
  613. initWithImmutableDictionary:[_internalValue dictionaryBySettingObject:value forKey:field]];
  614. }
  615. @end
  616. @interface FSTArrayValue ()
  617. @property(nonatomic, strong, readonly) NSArray<FSTFieldValue *> *internalValue;
  618. @end
  619. #pragma mark - FSTArrayValue
  620. @implementation FSTArrayValue
  621. - (id)initWithValueNoCopy:(NSArray<FSTFieldValue *> *)value {
  622. self = [super init];
  623. if (self) {
  624. // Does not copy, assumes the caller has already copied.
  625. _internalValue = value;
  626. }
  627. return self;
  628. }
  629. - (BOOL)isEqual:(id)other {
  630. if (other == self) {
  631. return YES;
  632. }
  633. if (![other isKindOfClass:[self class]]) {
  634. return NO;
  635. }
  636. // NSArray's isEqual does the right thing for our purposes.
  637. FSTArrayValue *otherArray = other;
  638. return [self.internalValue isEqual:otherArray.internalValue];
  639. }
  640. - (NSUInteger)hash {
  641. return [self.internalValue hash];
  642. }
  643. - (id)value {
  644. NSMutableArray *result = [NSMutableArray arrayWithCapacity:_internalValue.count];
  645. [self.internalValue enumerateObjectsUsingBlock:^(FSTFieldValue *obj, NSUInteger idx, BOOL *stop) {
  646. [result addObject:[obj value]];
  647. }];
  648. return result;
  649. }
  650. - (FSTTypeOrder)typeOrder {
  651. return FSTTypeOrderArray;
  652. }
  653. - (NSComparisonResult)compare:(FSTFieldValue *)other {
  654. if ([other isKindOfClass:[FSTArrayValue class]]) {
  655. NSArray<FSTFieldValue *> *selfArray = self.internalValue;
  656. NSArray<FSTFieldValue *> *otherArray = ((FSTArrayValue *)other).internalValue;
  657. NSUInteger minLength = MIN(selfArray.count, otherArray.count);
  658. for (NSUInteger i = 0; i < minLength; i++) {
  659. NSComparisonResult cmp = [selfArray[i] compare:otherArray[i]];
  660. if (cmp != NSOrderedSame) {
  661. return cmp;
  662. }
  663. }
  664. return FSTCompareUIntegers(selfArray.count, otherArray.count);
  665. } else {
  666. return [self defaultCompare:other];
  667. }
  668. }
  669. @end
  670. NS_ASSUME_NONNULL_END