Skip to content

Commit fb482c7

Browse files
committed
add arrayReverse
1 parent caf8158 commit fb482c7

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

Firestore/Swift/Source/ExpressionImplementation.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public extension Expression {
7171

7272
// MARK: Array Operations
7373

74+
func arrayReverse() -> FunctionExpression {
75+
return FunctionExpression("array_reverse", [self])
76+
}
77+
7478
func arrayConcat(_ arrays: [Expression]) -> FunctionExpression {
7579
return FunctionExpression("array_concat", [self] + arrays)
7680
}

Firestore/Swift/Source/SwiftAPI/Pipeline/Expressions/Expression.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,16 @@ public protocol Expression: Sendable {
187187

188188
// --- Added Array Operations ---
189189

190+
/// Creates an expression that returns the `input` with elements in reverse order.
191+
///
192+
/// ```swift
193+
/// // Reverse the "tags" array.
194+
/// Field("tags").arrayReverse()
195+
/// ```
196+
///
197+
/// - Returns: A new `FunctionExpression` representing the reversed array.
198+
func arrayReverse() -> FunctionExpression
199+
190200
/// Creates an expression that concatenates an array expression (from `self`) with one or more
191201
/// other array expressions.
192202
/// Assumes `self` and all parameters evaluate to arrays.

Firestore/Swift/Source/SwiftAPI/Pipeline/Expressions/FunctionExpressions/BooleanExpression.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class BooleanExpression: FunctionExpression, @unchecked Sendable {
5252
/// ```
5353
///
5454
/// - Returns: An `AggregateFunction` that performs the conditional count.
55-
internal func countIf() -> AggregateFunction {
55+
func countIf() -> AggregateFunction {
5656
return AggregateFunction("count_if", [self])
5757
}
5858

Firestore/Swift/Tests/Integration/PipelineTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,6 +1761,32 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
17611761
XCTAssertEqual(snapshot.results.count, 10)
17621762
}
17631763

1764+
func testArrayReverseWorks() async throws {
1765+
let collRef = collectionRef(withDocuments: [
1766+
"doc1": ["tags": ["a", "b", "c"]],
1767+
"doc2": ["tags": [1, 2, 3]],
1768+
"doc3": ["tags": []],
1769+
])
1770+
let db = collRef.firestore
1771+
1772+
let pipeline = db.pipeline()
1773+
.collection(collRef.path)
1774+
.select([
1775+
Field("tags").arrayReverse().as("reversedTags"),
1776+
])
1777+
.sort([Field("reversedTags").ascending()])
1778+
1779+
let snapshot = try await pipeline.execute()
1780+
1781+
let expectedResults: [[String: Sendable]] = [
1782+
["reversedTags": []],
1783+
["reversedTags": [3, 2, 1]],
1784+
["reversedTags": ["c", "b", "a"]],
1785+
]
1786+
1787+
TestHelper.compare(pipelineSnapshot: snapshot, expected: expectedResults, enforceOrder: true)
1788+
}
1789+
17641790
func testStrConcat() async throws {
17651791
let collRef = collectionRef(withDocuments: bookDocs)
17661792
let db = collRef.firestore

0 commit comments

Comments
 (0)