Skip to content

Commit 2fb7a14

Browse files
committed
add concat()
1 parent e42cce2 commit 2fb7a14

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

Firestore/Swift/Source/ExpressionImplementation.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,4 +894,9 @@ public extension Expression {
894894
func equivalent(_ other: Sendable) -> BooleanExpression {
895895
return BooleanExpression("equivalent", [self, Helper.sendableToExpr(other)])
896896
}
897+
898+
func concat(_ values: [Sendable]) -> FunctionExpression {
899+
let exprs = [self] + values.map { Helper.sendableToExpr($0) }
900+
return FunctionExpression("concat", exprs)
901+
}
897902
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,4 +1564,15 @@ public protocol Expression: Sendable {
15641564
/// - Parameter other: The value to compare against.
15651565
/// - Returns: A `BooleanExpr` that can be used in `where` clauses.
15661566
func equivalent(_ other: Sendable) -> BooleanExpression
1567+
1568+
/// Creates an expression that concatenates multiple sequenceable types together.
1569+
///
1570+
/// ```swift
1571+
/// // Concatenate the firstName and lastName with a space in between.
1572+
/// Field("firstName").concat([" ", Field("lastName")])
1573+
/// ```
1574+
///
1575+
/// - Parameter values: The values to concatenate.
1576+
/// - Returns: A new `FunctionExpression` representing the concatenated result.
1577+
func concat(_ values: [Sendable]) -> FunctionExpression
15671578
}

Firestore/Swift/Tests/Integration/PipelineTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,6 +1869,30 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
18691869
TestHelper.compare(snapshot: snapshot, expected: expectedResults, enforceOrder: true)
18701870
}
18711871

1872+
func testConcatWorks() async throws {
1873+
let collRef = collectionRef(withDocuments: [
1874+
"doc1": ["s": "a", "b": "b", "c": "c"],
1875+
"doc2": ["s": "x", "b": "y", "c": "z"],
1876+
])
1877+
let db = collRef.firestore
1878+
1879+
let pipeline = db.pipeline()
1880+
.collection(collRef.path)
1881+
.select([
1882+
Field("s").concat([Field("b"), Field("c"), " "]).as("concatenated"),
1883+
])
1884+
.sort([Field("concatenated").ascending()])
1885+
1886+
let snapshot = try await pipeline.execute()
1887+
1888+
let expectedResults: [[String: Sendable]] = [
1889+
["concatenated": "abc "],
1890+
["concatenated": "xyz "],
1891+
]
1892+
1893+
TestHelper.compare(snapshot: snapshot, expected: expectedResults, enforceOrder: true)
1894+
}
1895+
18721896
func testStartsWith() async throws {
18731897
let collRef = collectionRef(withDocuments: bookDocs)
18741898
let db = collRef.firestore

0 commit comments

Comments
 (0)