FIRAggregateField.mm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright 2023 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 "FIRAggregateField.h"
  17. #import "Firestore/Source/API/FIRAggregateField+Internal.h"
  18. #import "Firestore/Source/API/FIRFieldPath+Internal.h"
  19. #import "Firestore/core/src/model/aggregate_field.h"
  20. using firebase::firestore::model::AggregateField;
  21. NS_ASSUME_NONNULL_BEGIN
  22. #pragma mark - FIRAggregateField
  23. @interface FIRAggregateField ()
  24. @property(nonatomic, strong) FIRFieldPath *_fieldPath;
  25. @property(nonatomic, readwrite) model::AggregateField::OpKind _op;
  26. - (instancetype)initWithFieldPathAndKind:(nullable FIRFieldPath *)fieldPath
  27. opKind:(model::AggregateField::OpKind)op;
  28. @end
  29. @implementation FIRAggregateField
  30. - (instancetype)initWithFieldPathAndKind:(nullable FIRFieldPath *)fieldPath
  31. opKind:(model::AggregateField::OpKind)op {
  32. if (self = [super init]) {
  33. self._fieldPath = fieldPath;
  34. self._op = op;
  35. }
  36. return self;
  37. }
  38. - (FIRFieldPath *)fieldPath {
  39. return [self _fieldPath];
  40. }
  41. - (const std::string)name {
  42. switch ([self _op]) {
  43. case AggregateField::OpKind::Sum:
  44. return std::string("sum");
  45. case AggregateField::OpKind::Avg:
  46. return std::string("avg");
  47. case AggregateField::OpKind::Count:
  48. return std::string("count");
  49. }
  50. UNREACHABLE();
  51. }
  52. - (model::AggregateField)createInternalValue {
  53. if (self.fieldPath != Nil) {
  54. return model::AggregateField([self _op], [self createAlias], self.fieldPath.internalValue);
  55. } else {
  56. return model::AggregateField([self _op], [self createAlias]);
  57. }
  58. }
  59. - (model::AggregateAlias)createAlias {
  60. if (self.fieldPath != Nil) {
  61. return model::AggregateAlias([self name] + std::string{"_"} +
  62. self.fieldPath.internalValue.CanonicalString());
  63. } else {
  64. return model::AggregateAlias([self name]);
  65. }
  66. }
  67. + (instancetype)aggregateFieldForCount {
  68. return [[FSTCountAggregateField alloc] initPrivate];
  69. }
  70. + (instancetype)aggregateFieldForSumOfField:(NSString *)field {
  71. return [self aggregateFieldForSumOfFieldPath:[FIRFieldPath pathWithDotSeparatedString:field]];
  72. }
  73. + (instancetype)aggregateFieldForSumOfFieldPath:(FIRFieldPath *)fieldPath {
  74. return [[FSTSumAggregateField alloc] initWithFieldPath:fieldPath];
  75. }
  76. + (instancetype)aggregateFieldForAverageOfField:(NSString *)field {
  77. return [self aggregateFieldForAverageOfFieldPath:[FIRFieldPath pathWithDotSeparatedString:field]];
  78. }
  79. + (instancetype)aggregateFieldForAverageOfFieldPath:(FIRFieldPath *)fieldPath {
  80. return [[FSTAverageAggregateField alloc] initWithFieldPath:fieldPath];
  81. }
  82. @end
  83. #pragma mark - FSTSumAggregateField
  84. @implementation FSTSumAggregateField
  85. - (instancetype)initWithFieldPath:(FIRFieldPath *)fieldPath {
  86. self = [super initWithFieldPathAndKind:fieldPath opKind:model::AggregateField::OpKind::Sum];
  87. return self;
  88. }
  89. @end
  90. #pragma mark - FSTAverageAggregateField
  91. @implementation FSTAverageAggregateField
  92. - (instancetype)initWithFieldPath:(FIRFieldPath *)fieldPath {
  93. self = [super initWithFieldPathAndKind:fieldPath opKind:model::AggregateField::OpKind::Avg];
  94. return self;
  95. }
  96. @end
  97. #pragma mark - FSTCountAggregateField
  98. @implementation FSTCountAggregateField
  99. - (instancetype)initPrivate {
  100. self = [super initWithFieldPathAndKind:Nil opKind:model::AggregateField::OpKind::Count];
  101. return self;
  102. }
  103. @end
  104. NS_ASSUME_NONNULL_END