From 0b09781f8fb9e8e1135f960fd97088ecf92044a2 Mon Sep 17 00:00:00 2001 From: Andrew Roan Date: Thu, 18 Jul 2024 15:03:01 -0500 Subject: [PATCH 1/2] Add a unified aggregate endpoint feature/make-internal-aggregate-endpoint-public --- .../CoreDataRepository+Aggregate.swift | 41 +++++++++++++++---- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/Sources/CoreDataRepository/CoreDataRepository+Aggregate.swift b/Sources/CoreDataRepository/CoreDataRepository+Aggregate.swift index c24b650..b4f6d19 100644 --- a/Sources/CoreDataRepository/CoreDataRepository+Aggregate.swift +++ b/Sources/CoreDataRepository/CoreDataRepository+Aggregate.swift @@ -12,6 +12,38 @@ import Foundation // swiftlint:disable file_length extension CoreDataRepository { + public enum AggregateFunction: String { + case count + case sum + case average + case min + case max + } + + @inlinable + public func aggregate( + function: AggregateFunction, + predicate: NSPredicate, + entityDesc: NSEntityDescription, + attributeDesc: NSAttributeDescription, + groupBy: NSAttributeDescription? = nil, + as valueType: Value.Type + ) async -> Result { + switch function { + case .count: + await count(predicate: predicate, entityDesc: entityDesc, as: valueType) + default: + await Self.send( + function: .sum, + context: context, + predicate: predicate, + entityDesc: entityDesc, + attributeDesc: attributeDesc, + groupBy: groupBy + ) + } + } + // MARK: Count /// Get the count or quantity of managed object instances that satisfy the predicate. @@ -375,15 +407,6 @@ extension CoreDataRepository { // MARK: Internals - @usableFromInline - enum AggregateFunction: String { - case count - case sum - case average - case min - case max - } - private static func aggregate( context: NSManagedObjectContext, request: NSFetchRequest From 06df31f6f0883f63ba3fdef090b258a11008562d Mon Sep 17 00:00:00 2001 From: Andrew Roan Date: Thu, 18 Jul 2024 15:10:54 -0500 Subject: [PATCH 2/2] WIP: add explicit return feature/make-internal-aggregate-endpoint-public --- Sources/CoreDataRepository/CoreDataRepository+Aggregate.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/CoreDataRepository/CoreDataRepository+Aggregate.swift b/Sources/CoreDataRepository/CoreDataRepository+Aggregate.swift index b4f6d19..9c2bb20 100644 --- a/Sources/CoreDataRepository/CoreDataRepository+Aggregate.swift +++ b/Sources/CoreDataRepository/CoreDataRepository+Aggregate.swift @@ -31,9 +31,9 @@ extension CoreDataRepository { ) async -> Result { switch function { case .count: - await count(predicate: predicate, entityDesc: entityDesc, as: valueType) + return await count(predicate: predicate, entityDesc: entityDesc, as: valueType) default: - await Self.send( + return await Self.send( function: .sum, context: context, predicate: predicate,