Skip to content

Commit 8e8557f

Browse files
committed
add length()
1 parent 54461f1 commit 8e8557f

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

Firestore/Swift/Source/ExpressionImplementation.swift

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

461461
// --- Added String Operations ---
462462

463+
func length() -> FunctionExpression {
464+
return FunctionExpression("length", [self])
465+
}
466+
463467
func charLength() -> FunctionExpression {
464468
return FunctionExpression("char_length", [self])
465469
}

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

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

651651
// MARK: String Operations
652652

653+
/// Creates an expression that returns the length of a string.
654+
///
655+
/// ```swift
656+
/// // Get the length of the "name" field.
657+
/// Field("name").length()
658+
/// ```
659+
///
660+
/// - Returns: A new `FunctionExpression` representing the length of the string.
661+
func length() -> FunctionExpression
662+
653663
/// Creates an expression that calculates the character length of a string in UTF-8.
654664
/// Assumes `self` evaluates to a string.
655665
///

Firestore/Swift/Tests/Integration/PipelineTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,32 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
19181918
TestHelper.compare(pipelineSnapshot: snapshot, expected: expectedResults, enforceOrder: true)
19191919
}
19201920

1921+
func testLength() async throws {
1922+
let collRef = collectionRef(withDocuments: [
1923+
"doc1": ["value": "abc"],
1924+
"doc2": ["value": ""],
1925+
"doc3": ["value": "a"],
1926+
])
1927+
let db = collRef.firestore
1928+
1929+
let pipeline = db.pipeline()
1930+
.collection(collRef.path)
1931+
.select([
1932+
Field("value").length().as("lengthValue"),
1933+
])
1934+
.sort([Field("lengthValue").ascending()])
1935+
1936+
let snapshot = try await pipeline.execute()
1937+
1938+
let expectedResults: [[String: Sendable]] = [
1939+
["lengthValue": 0],
1940+
["lengthValue": 1],
1941+
["lengthValue": 3],
1942+
]
1943+
1944+
TestHelper.compare(pipelineSnapshot: snapshot, expected: expectedResults, enforceOrder: true)
1945+
}
1946+
19211947
func testLike() async throws {
19221948
let collRef = collectionRef(withDocuments: bookDocs)
19231949
let db = collRef.firestore

0 commit comments

Comments
 (0)