Skip to content

Commit 3d0a3d3

Browse files
committed
hide replace expression
1 parent 69ced35 commit 3d0a3d3

File tree

3 files changed

+107
-114
lines changed

3 files changed

+107
-114
lines changed

Firestore/Swift/Source/ExpressionImplementation.swift

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,79 @@ extension Expression {
271271
func manhattanDistance(_ vector: [Double]) -> FunctionExpression {
272272
return FunctionExpression("manhattan_distance", [self, Helper.sendableToExpr(vector)])
273273
}
274+
275+
/// Creates an expression that replaces the first occurrence of a literal substring within this
276+
/// string expression with another literal substring.
277+
/// Assumes `self` evaluates to a string.
278+
///
279+
/// ```swift
280+
/// // Replace the first "hello" with "hi" in the "message" field
281+
/// Field("message").replaceFirst("hello", "hi")
282+
/// ```
283+
///
284+
/// - Parameter find: The literal string substring to search for.
285+
/// - Parameter replace: The literal string substring to replace the first occurrence with.
286+
/// - Returns: A new `FunctionExpr` representing the string with the first occurrence replaced.
287+
func replaceFirst(_ find: String, with replace: String) -> FunctionExpression{
288+
return FunctionExpression(
289+
"replace_first",
290+
[self, Helper.sendableToExpr(find), Helper.sendableToExpr(replace)]
291+
)
292+
}
293+
294+
/// Creates an expression that replaces the first occurrence of a substring (from an expression)
295+
/// within this string expression with another substring (from an expression).
296+
/// Assumes `self` evaluates to a string, and `find`/`replace` evaluate to strings.
297+
///
298+
/// ```swift
299+
/// // Replace first occurrence of field "findPattern" with field "replacePattern" in "text"
300+
/// Field("text").replaceFirst(Field("findPattern"), Field("replacePattern"))
301+
/// ```
302+
///
303+
/// - Parameter find: An `Expr` (evaluating to a string) for the substring to search for.
304+
/// - Parameter replace: An `Expr` (evaluating to a string) for the substring to replace the first
305+
/// occurrence with.
306+
/// - Returns: A new `FunctionExpr` representing the string with the first occurrence replaced.
307+
func replaceFirst(_ find: Expression, with replace: Expression) -> FunctionExpression{
308+
return FunctionExpression("replace_first", [self, find, replace])
309+
}
310+
311+
/// Creates an expression that replaces all occurrences of a literal substring within this string
312+
/// expression with another literal substring.
313+
/// Assumes `self` evaluates to a string.
314+
///
315+
/// ```swift
316+
/// // Replace all occurrences of " " with "_" in "description"
317+
/// Field("description").stringReplace(" ", "_")
318+
/// ```
319+
///
320+
/// - Parameter find: The literal string substring to search for.
321+
/// - Parameter replace: The literal string substring to replace all occurrences with.
322+
/// - Returns: A new `FunctionExpr` representing the string with all occurrences replaced.
323+
func stringReplace(_ find: String, with replace: String) -> FunctionExpression{
324+
return FunctionExpression(
325+
"string_replace",
326+
[self, Helper.sendableToExpr(find), Helper.sendableToExpr(replace)]
327+
)
328+
}
329+
330+
/// Creates an expression that replaces all occurrences of a substring (from an expression) within
331+
/// this string expression with another substring (from an expression).
332+
/// Assumes `self` evaluates to a string, and `find`/`replace` evaluate to strings.
333+
///
334+
/// ```swift
335+
/// // Replace all occurrences of field "target" with field "replacement" in "content"
336+
/// Field("content").stringReplace(Field("target"), Field("replacement"))
337+
/// ```
338+
///
339+
/// - Parameter find: An `Expr` (evaluating to a string) for the substring to search for.
340+
/// - Parameter replace: An `Expr` (evaluating to a string) for the substring to replace all
341+
/// occurrences with.
342+
/// - Returns: A new `FunctionExpr` representing the string with all occurrences replaced.
343+
func stringReplace(_ find: Expression, with replace: Expression) -> FunctionExpression{
344+
return FunctionExpression("string_replace", [self, find, replace])
345+
}
346+
274347
}
275348

276349
public extension Expression {
@@ -594,28 +667,6 @@ public extension Expression {
594667
return FunctionExpression("reverse", [self])
595668
}
596669

597-
func replaceFirst(_ find: String, with replace: String) -> FunctionExpression {
598-
return FunctionExpression(
599-
"replace_first",
600-
[self, Helper.sendableToExpr(find), Helper.sendableToExpr(replace)]
601-
)
602-
}
603-
604-
func replaceFirst(_ find: Expression, with replace: Expression) -> FunctionExpression {
605-
return FunctionExpression("replace_first", [self, find, replace])
606-
}
607-
608-
func replaceAll(_ find: String, with replace: String) -> FunctionExpression {
609-
return FunctionExpression(
610-
"replace_all",
611-
[self, Helper.sendableToExpr(find), Helper.sendableToExpr(replace)]
612-
)
613-
}
614-
615-
func replaceAll(_ find: Expression, with replace: Expression) -> FunctionExpression {
616-
return FunctionExpression("replace_all", [self, find, replace])
617-
}
618-
619670
func byteLength() -> FunctionExpression {
620671
return FunctionExpression("byte_length", [self])
621672
}

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

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -931,64 +931,6 @@ public protocol Expression: Sendable {
931931
/// - Returns: A new `FunctionExpr` representing the reversed string.
932932
func reverse() -> FunctionExpression
933933

934-
/// Creates an expression that replaces the first occurrence of a literal substring within this
935-
/// string expression with another literal substring.
936-
/// Assumes `self` evaluates to a string.
937-
///
938-
/// ```swift
939-
/// // Replace the first "hello" with "hi" in the "message" field
940-
/// Field("message").replaceFirst("hello", "hi")
941-
/// ```
942-
///
943-
/// - Parameter find: The literal string substring to search for.
944-
/// - Parameter replace: The literal string substring to replace the first occurrence with.
945-
/// - Returns: A new `FunctionExpr` representing the string with the first occurrence replaced.
946-
func replaceFirst(_ find: String, with replace: String) -> FunctionExpression
947-
948-
/// Creates an expression that replaces the first occurrence of a substring (from an expression)
949-
/// within this string expression with another substring (from an expression).
950-
/// Assumes `self` evaluates to a string, and `find`/`replace` evaluate to strings.
951-
///
952-
/// ```swift
953-
/// // Replace first occurrence of field "findPattern" with field "replacePattern" in "text"
954-
/// Field("text").replaceFirst(Field("findPattern"), Field("replacePattern"))
955-
/// ```
956-
///
957-
/// - Parameter find: An `Expr` (evaluating to a string) for the substring to search for.
958-
/// - Parameter replace: An `Expr` (evaluating to a string) for the substring to replace the first
959-
/// occurrence with.
960-
/// - Returns: A new `FunctionExpr` representing the string with the first occurrence replaced.
961-
func replaceFirst(_ find: Expression, with replace: Expression) -> FunctionExpression
962-
963-
/// Creates an expression that replaces all occurrences of a literal substring within this string
964-
/// expression with another literal substring.
965-
/// Assumes `self` evaluates to a string.
966-
///
967-
/// ```swift
968-
/// // Replace all occurrences of " " with "_" in "description"
969-
/// Field("description").replaceAll(" ", "_")
970-
/// ```
971-
///
972-
/// - Parameter find: The literal string substring to search for.
973-
/// - Parameter replace: The literal string substring to replace all occurrences with.
974-
/// - Returns: A new `FunctionExpr` representing the string with all occurrences replaced.
975-
func replaceAll(_ find: String, with replace: String) -> FunctionExpression
976-
977-
/// Creates an expression that replaces all occurrences of a substring (from an expression) within
978-
/// this string expression with another substring (from an expression).
979-
/// Assumes `self` evaluates to a string, and `find`/`replace` evaluate to strings.
980-
///
981-
/// ```swift
982-
/// // Replace all occurrences of field "target" with field "replacement" in "content"
983-
/// Field("content").replaceAll(Field("target"), Field("replacement"))
984-
/// ```
985-
///
986-
/// - Parameter find: An `Expr` (evaluating to a string) for the substring to search for.
987-
/// - Parameter replace: An `Expr` (evaluating to a string) for the substring to replace all
988-
/// occurrences with.
989-
/// - Returns: A new `FunctionExpr` representing the string with all occurrences replaced.
990-
func replaceAll(_ find: Expression, with replace: Expression) -> FunctionExpression
991-
992934
/// Creates an expression that calculates the length of this string or bytes expression in bytes.
993935
/// Assumes `self` evaluates to a string or bytes.
994936
///

Firestore/Swift/Tests/Integration/PipelineTests.swift

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3060,41 +3060,41 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
30603060
}
30613061
}
30623062

3063-
func testReplaceFirst() async throws {
3064-
try XCTSkipIf(true, "Skip this test since backend has not yet supported.")
3065-
let collRef = collectionRef(withDocuments: bookDocs)
3066-
let db = collRef.firestore
3067-
3068-
let pipeline = db.pipeline()
3069-
.collection(collRef.path)
3070-
.where(Field("title").equal("The Lord of the Rings"))
3071-
.limit(1)
3072-
.select([Field("title").replaceFirst("o", with: "0").as("newName")])
3073-
let snapshot = try await pipeline.execute()
3074-
TestHelper.compare(
3075-
pipelineSnapshot: snapshot,
3076-
expected: [["newName": "The L0rd of the Rings"]],
3077-
enforceOrder: false
3078-
)
3079-
}
3080-
3081-
func testReplaceAll() async throws {
3082-
try XCTSkipIf(true, "Skip this test since backend has not yet supported.")
3083-
let collRef = collectionRef(withDocuments: bookDocs)
3084-
let db = collRef.firestore
3063+
// func testReplaceFirst() async throws {
3064+
// try XCTSkipIf(true, "Skip this test since backend has not yet supported.")
3065+
// let collRef = collectionRef(withDocuments: bookDocs)
3066+
// let db = collRef.firestore
3067+
//
3068+
// let pipeline = db.pipeline()
3069+
// .collection(collRef.path)
3070+
// .where(Field("title").equal("The Lord of the Rings"))
3071+
// .limit(1)
3072+
// .select([Field("title").replaceFirst("o", with: "0").as("newName")])
3073+
// let snapshot = try await pipeline.execute()
3074+
// TestHelper.compare(
3075+
// pipelineSnapshot: snapshot,
3076+
// expected: [["newName": "The L0rd of the Rings"]],
3077+
// enforceOrder: false
3078+
// )
3079+
// }
30853080

3086-
let pipeline = db.pipeline()
3087-
.collection(collRef.path)
3088-
.where(Field("title").equal("The Lord of the Rings"))
3089-
.limit(1)
3090-
.select([Field("title").replaceAll("o", with: "0").as("newName")])
3091-
let snapshot = try await pipeline.execute()
3092-
TestHelper.compare(
3093-
pipelineSnapshot: snapshot,
3094-
expected: [["newName": "The L0rd 0f the Rings"]],
3095-
enforceOrder: false
3096-
)
3097-
}
3081+
// func testStringReplace() async throws {
3082+
// try XCTSkipIf(true, "Skip this test since backend has not yet supported.")
3083+
// let collRef = collectionRef(withDocuments: bookDocs)
3084+
// let db = collRef.firestore
3085+
//
3086+
// let pipeline = db.pipeline()
3087+
// .collection(collRef.path)
3088+
// .where(Field("title").equal("The Lord of the Rings"))
3089+
// .limit(1)
3090+
// .select([Field("title").stringReplace("o", with: "0").as("newName")])
3091+
// let snapshot = try await pipeline.execute()
3092+
// TestHelper.compare(
3093+
// pipelineSnapshot: snapshot,
3094+
// expected: [["newName": "The L0rd 0f the Rings"]],
3095+
// enforceOrder: false
3096+
// )
3097+
// }
30983098

30993099
// func testBitAnd() async throws {
31003100
// try XCTSkipIf(true, "Skip this test since backend has not yet supported.")

0 commit comments

Comments
 (0)