FIRAggregateQuerySnapshot.mm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2022 Google LLC
  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 "FIRAggregateQuerySnapshot+Internal.h"
  17. #import "FIRAggregateQuery.h"
  18. #import "FIRQuery.h"
  19. #import "Firestore/Source/API/FIRAggregateField+Internal.h"
  20. #import "Firestore/Source/API/FIRFieldPath+Internal.h"
  21. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  22. #import "Firestore/Source/API/FSTUserDataWriter.h"
  23. #include "absl/types/optional.h"
  24. #include "Firestore/Protos/nanopb/google/firestore/v1/document.nanopb.h"
  25. #include "Firestore/core/src/model/aggregate_alias.h"
  26. #include "Firestore/core/src/model/field_path.h"
  27. #include "Firestore/core/src/util/exception.h"
  28. using firebase::firestore::google_firestore_v1_Value;
  29. using firebase::firestore::model::AggregateAlias;
  30. using firebase::firestore::model::FieldPath;
  31. using firebase::firestore::model::ObjectValue;
  32. using firebase::firestore::util::ThrowInvalidArgument;
  33. NS_ASSUME_NONNULL_BEGIN
  34. @implementation FIRAggregateQuerySnapshot {
  35. ObjectValue _result;
  36. FIRAggregateQuery *_query;
  37. }
  38. - (instancetype)initWithObject:(ObjectValue)result query:(FIRAggregateQuery *)query {
  39. if (self = [super init]) {
  40. _result = std::move(result);
  41. _query = query;
  42. }
  43. return self;
  44. }
  45. #pragma mark - NSObject Methods
  46. - (BOOL)isEqual:(nullable id)other {
  47. if (other == self) return YES;
  48. if (![[other class] isEqual:[self class]]) return NO;
  49. auto otherSnap = static_cast<FIRAggregateQuerySnapshot *>(other);
  50. return _result == otherSnap->_result && [_query isEqual:otherSnap->_query];
  51. }
  52. - (NSUInteger)hash {
  53. NSUInteger result = [_query hash];
  54. result = 31 * result + _result.Hash();
  55. return result;
  56. }
  57. #pragma mark - Public Methods
  58. - (NSNumber *)count {
  59. return (NSNumber *)[self valueForAggregateField:[FIRAggregateField aggregateFieldForCount]];
  60. }
  61. - (FIRAggregateQuery *)query {
  62. return _query;
  63. }
  64. - (id)valueForAggregateField:(FIRAggregateField *)aggregateField {
  65. FIRServerTimestampBehavior serverTimestampBehavior = FIRServerTimestampBehaviorNone;
  66. AggregateAlias alias = [aggregateField createAlias];
  67. absl::optional<google_firestore_v1_Value> fieldValue = _result.Get(alias.StringValue());
  68. if (!fieldValue) {
  69. std::string path{""};
  70. if (aggregateField.fieldPath) {
  71. path = [aggregateField.fieldPath internalValue].CanonicalString();
  72. }
  73. ThrowInvalidArgument("'%s(%s)' was not requested in the aggregation query.",
  74. [aggregateField name], path);
  75. }
  76. FSTUserDataWriter *dataWriter =
  77. [[FSTUserDataWriter alloc] initWithFirestore:_query.query.firestore.wrapped
  78. serverTimestampBehavior:serverTimestampBehavior];
  79. return [dataWriter convertedValue:*fieldValue];
  80. }
  81. @end
  82. NS_ASSUME_NONNULL_END