Skip to content

Commit a367798

Browse files
committed
fix add/multiply
1 parent 09a0466 commit a367798

File tree

4 files changed

+22
-101
lines changed

4 files changed

+22
-101
lines changed

Firestore/Swift/Source/ExprImpl.swift

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@ public extension Expr {
3333
return FunctionExpr("add", [self, Helper.sendableToExpr(value)])
3434
}
3535

36-
func add(_ values: [Expr]) -> FunctionExpr {
37-
return FunctionExpr("add", [self, Helper.array(values)])
38-
}
39-
40-
func add(_ values: [Sendable]) -> FunctionExpr {
41-
return FunctionExpr("add", [self, Helper.array(values)])
42-
}
43-
4436
func subtract(_ other: Expr) -> FunctionExpr {
4537
return FunctionExpr("subtract", [self, other])
4638
}
@@ -57,14 +49,6 @@ public extension Expr {
5749
return FunctionExpr("multiply", [self, Helper.sendableToExpr(value)])
5850
}
5951

60-
func multiply(_ values: [Expr]) -> FunctionExpr {
61-
return FunctionExpr("multiply", [self, Helper.array(values)])
62-
}
63-
64-
func multiply(_ values: [Sendable]) -> FunctionExpr {
65-
return FunctionExpr("multiply", [self, Helper.array(values)])
66-
}
67-
6852
func divide(_ other: Expr) -> FunctionExpr {
6953
return FunctionExpr("divide", [self, other])
7054
}

Firestore/Swift/Source/Helper/PipelineHelper.swift

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,24 @@ enum Helper {
1818
return Constant.nil
1919
}
2020

21-
if value is Expr {
22-
return value as! Expr
23-
} else if value is [String: Sendable?] {
24-
return map(value as! [String: Sendable?])
25-
} else if value is [Sendable?] {
26-
return array(value as! [Sendable?])
27-
} else if value is TimeUnit {
28-
return Constant((value as! TimeUnit).rawValue)
21+
if let exprValue = value as? Expr {
22+
return exprValue
23+
} else if let dictionaryValue = value as? [String: Sendable?] {
24+
return map(dictionaryValue)
25+
} else if let arrayValue = value as? [Sendable?] {
26+
return array(arrayValue)
27+
} else if let timeUnitValue = value as? TimeUnit {
28+
return Constant(timeUnitValue.rawValue)
2929
} else {
3030
return Constant(value)
3131
}
3232
}
3333

3434
static func selectablesToMap(selectables: [Selectable]) -> [String: Expr] {
3535
let exprMap = selectables.reduce(into: [String: Expr]()) { result, selectable in
36-
let value = selectable as! SelectableWrapper
36+
guard let value = selectable as? SelectableWrapper else {
37+
fatalError("Selectable class must conform to SelectableWrapper.")
38+
}
3739
result[value.alias] = value.expr
3840
}
3941
return exprMap
@@ -57,22 +59,18 @@ enum Helper {
5759

5860
// This function is used to convert Swift type into Objective-C type.
5961
static func sendableToAnyObjectForRawStage(_ value: Sendable?) -> AnyObject {
60-
guard let value = value else {
61-
return Constant.nil.bridge
62-
}
63-
64-
guard !(value is NSNull) else {
62+
guard let value = value, !(value is NSNull) else {
6563
return Constant.nil.bridge
6664
}
6765

68-
if value is Expr {
69-
return (value as! Expr).toBridge()
70-
} else if value is AggregateFunction {
71-
return (value as! AggregateFunction).toBridge()
72-
} else if value is [String: Sendable?] {
73-
let mappedValue: [String: Sendable?] = (value as! [String: Sendable?]).mapValues {
74-
if $0 is AggregateFunction {
75-
return ($0 as! AggregateFunction).toBridge()
66+
if let exprValue = value as? Expr {
67+
return exprValue.toBridge()
68+
} else if let aggregateFunctionValue = value as? AggregateFunction {
69+
return aggregateFunctionValue.toBridge()
70+
} else if let dictionaryValue = value as? [String: Sendable?] {
71+
let mappedValue: [String: Sendable] = dictionaryValue.mapValues {
72+
if let aggFunc = $0 as? AggregateFunction {
73+
return aggFunc.toBridge()
7674
}
7775
return sendableToExpr($0).toBridge()
7876
}

Firestore/Swift/Source/SwiftAPI/Pipeline/Expr.swift

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -68,37 +68,6 @@ public protocol Expr: Sendable {
6868
/// - Returns: A new `FunctionExpr` representing the addition operation.
6969
func add(_ value: Sendable) -> FunctionExpr
7070

71-
/// Creates an expression that adds this expression to one or more other expressions.
72-
/// Assumes `self` and all parameters evaluate to compatible types for addition (e.g., numbers, or
73-
/// string/array concatenation if supported by the specific "add" implementation).
74-
///
75-
/// ```swift
76-
/// // Add the value of the 'quantity' field and the 'reserve' field.
77-
/// Field("quantity").add(Field("reserve"))
78-
///
79-
/// // Add multiple numeric fields
80-
/// Field("subtotal").add(Field("tax"), Field("shipping"))
81-
/// ```
82-
///
83-
/// - Parameter values: Expr` values to add.
84-
/// - Returns: A new `FunctionExpr` representing the addition operation.
85-
func add(_ values: [Expr]) -> FunctionExpr
86-
87-
/// Creates an expression that adds this expression to one or more literal values.
88-
/// Assumes `self` and all parameters evaluate to compatible types for addition.
89-
///
90-
/// ```swift
91-
/// // Add 5 to the 'count' field
92-
/// Field("count").add(5)
93-
///
94-
/// // Add multiple literal numbers
95-
/// Field("score").add(10, 20, -5)
96-
/// ```
97-
///
98-
/// - Parameter values: Expr` values to add.
99-
/// - Returns: A new `FunctionExpr` representing the addition operation.
100-
func add(_ values: [Sendable]) -> FunctionExpr
101-
10271
/// Creates an expression that subtracts another expression from this expression.
10372
/// Assumes `self` and `other` evaluate to numeric types.
10473
///
@@ -153,36 +122,6 @@ public protocol Expr: Sendable {
153122
/// - Returns: A new `FunctionExpr` representing the multiplication operation.
154123
func multiply(_ value: Sendable) -> FunctionExpr
155124

156-
/// Creates an expression that multiplies this expression by one or more other expressions.
157-
/// Assumes `self` and all parameters evaluate to numeric types.
158-
///
159-
/// ```swift
160-
/// // Multiply the 'quantity' field by the 'price' field
161-
/// Field("quantity").multiply(Field("price"))
162-
///
163-
/// // Multiply 'rate' by 'time' and 'conversionFactor' fields
164-
/// Field("rate").multiply(Field("time"), Field("conversionFactor"))
165-
/// ```
166-
///
167-
/// - Parameter values: `Expr` values to multiply by.
168-
/// - Returns: A new `FunctionExpr` representing the multiplication operation.
169-
func multiply(_ values: [Expr]) -> FunctionExpr
170-
171-
/// Creates an expression that multiplies this expression by one or more literal values.
172-
/// Assumes `self` evaluates to a numeric type.
173-
///
174-
/// ```swift
175-
/// // Multiply the 'score' by 1.1
176-
/// Field("score").multiply(1.1)
177-
///
178-
/// // Multiply 'base' by 2 and then by 3.0
179-
/// Field("base").multiply(2, 3.0)
180-
/// ```
181-
///
182-
/// - Parameter values: `Sendable` literal values to multiply by.
183-
/// - Returns: A new `FunctionExpr` representing the multiplication operation.
184-
func multiply(_ values: [Sendable]) -> FunctionExpr
185-
186125
/// Creates an expression that divides this expression by another expression.
187126
/// Assumes `self` and `other` evaluate to numeric types.
188127
///

Firestore/Swift/Tests/Integration/PipelineTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1900,8 +1900,8 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
19001900
Field("published").subtract(1900).as("yearsSince1900"),
19011901
Field("rating").multiply(10).as("ratingTimesTen"),
19021902
Field("rating").divide(2).as("ratingDividedByTwo"),
1903-
Field("rating").multiply([10, 2]).as("ratingTimes20"),
1904-
Field("rating").add([1, 2]).as("ratingPlus3"),
1903+
Field("rating").multiply(20).as("ratingTimes20"),
1904+
Field("rating").add(3).as("ratingPlus3"),
19051905
Field("rating").mod(2).as("ratingMod2")
19061906
)
19071907
.limit(1)

0 commit comments

Comments
 (0)