Skip to content

Commit 62434c1

Browse files
committed
add ceil()
1 parent fb482c7 commit 62434c1

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
@@ -29,6 +29,10 @@ public extension Expression {
2929
return FunctionExpression("abs", [self])
3030
}
3131

32+
func ceil() -> FunctionExpression {
33+
return FunctionExpression("ceil", [self])
34+
}
35+
3236
func add(_ value: Expression) -> FunctionExpression {
3337
return FunctionExpression("add", [self, value])
3438
}

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

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

3737
// --- Added Mathematical Operations ---
3838

39+
/// Creates an expression that returns the smallest numeric value that isn't less than the number.
40+
///
41+
/// ```swift
42+
/// // Get the ceiling of the "amount" field.
43+
/// Field("amount").ceil()
44+
/// ```
45+
///
46+
/// - Returns: A new `FunctionExpression` representing the ceiling of the number.
47+
func ceil() -> FunctionExpression
48+
3949
/// Creates an expression that returns the absolute value of the number.
4050
///
4151
/// ```swift

Firestore/Swift/Tests/Integration/PipelineTests.swift

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

2004+
func testCeilWorks() async throws {
2005+
let collRef = collectionRef(withDocuments: [
2006+
"doc1": ["value": -10.8],
2007+
"doc2": ["value": 5.3],
2008+
"doc3": ["value": 0],
2009+
])
2010+
let db = collRef.firestore
2011+
2012+
let pipeline = db.pipeline()
2013+
.collection(collRef.path)
2014+
.select([
2015+
Field("value").ceil().as("ceilValue"),
2016+
])
2017+
.sort([Field("ceilValue").ascending()])
2018+
2019+
let snapshot = try await pipeline.execute()
2020+
2021+
let expectedResults: [[String: Sendable]] = [
2022+
["ceilValue": -10],
2023+
["ceilValue": 0],
2024+
["ceilValue": 6],
2025+
]
2026+
2027+
TestHelper.compare(pipelineSnapshot: snapshot, expected: expectedResults, enforceOrder: true)
2028+
}
2029+
20042030
func testComparisonOperators() async throws {
20052031
let collRef = collectionRef(withDocuments: bookDocs)
20062032
let db = collRef.firestore

0 commit comments

Comments
 (0)