Skip to content

Commit e19e9fd

Browse files
committed
add Split
1 parent 857e71c commit e19e9fd

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

Firestore/Swift/Source/ExpressionImplementation.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,14 @@ public extension Expression {
638638
return FunctionExpression(functionName: "join", args: [self, Constant(delimiter)])
639639
}
640640

641+
func split(delimiter: String) -> FunctionExpression {
642+
return FunctionExpression(functionName: "split", args: [self, Constant(delimiter)])
643+
}
644+
645+
func split(delimiter: Expression) -> FunctionExpression {
646+
return FunctionExpression(functionName: "split", args: [self, delimiter])
647+
}
648+
641649
func length() -> FunctionExpression {
642650
return FunctionExpression(functionName: "length", args: [self])
643651
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,18 @@ public protocol Expression: Sendable {
704704
/// - Returns: A new `FunctionExpression` representing the joined string.
705705
func join(delimiter: String) -> FunctionExpression
706706

707+
/// Creates an expression that splits a string into an array of substrings based on a delimiter.
708+
///
709+
/// - Parameter delimiter: The string to split on.
710+
/// - Returns: A new `FunctionExpression` representing the array of substrings.
711+
func split(delimiter: String) -> FunctionExpression
712+
713+
/// Creates an expression that splits a string into an array of substrings based on a delimiter.
714+
///
715+
/// - Parameter delimiter: An expression that evaluates to a string or bytes to split on.
716+
/// - Returns: A new `FunctionExpression` representing the array of substrings.
717+
func split(delimiter: Expression) -> FunctionExpression
718+
707719
/// Creates an expression that returns the length of a string.
708720
///
709721
/// ```swift

Firestore/Swift/Tests/Integration/PipelineTests.swift

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3521,15 +3521,14 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
35213521
// }
35223522

35233523
func testDocumentId() async throws {
3524-
try XCTSkipIf(true, "Skip this test since backend has not yet supported.")
35253524
let collRef = collectionRef(withDocuments: bookDocs)
35263525
let db = collRef.firestore
35273526

35283527
let pipeline = db.pipeline()
35293528
.collection(collRef.path)
35303529
.sort([Field("rating").descending()])
35313530
.limit(1)
3532-
.select([Field("__path__").documentId().as("docId")])
3531+
.select([Field(FieldPath.documentID()).documentId().as("docId")])
35333532
let snapshot = try await pipeline.execute()
35343533
TestHelper.compare(
35353534
snapshot: snapshot,
@@ -3844,4 +3843,52 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
38443843
enforceOrder: true
38453844
)
38463845
}
3846+
3847+
func testSplitWorks() async throws {
3848+
let collRef = collectionRef(withDocuments: [
3849+
"doc1": ["text": "a-b-c"],
3850+
"doc2": ["text": "x,y,z", "delimiter": ","],
3851+
"doc3": ["text": Data([0x61, 0x00, 0x62, 0x00, 0x63]), "delimiter": Data([0x00])],
3852+
])
3853+
let db = collRef.firestore
3854+
3855+
// Test with string literal delimiter
3856+
var pipeline = db.pipeline()
3857+
.documents([collRef.document("doc1").path])
3858+
.select([
3859+
Field("text").split(delimiter: "-").as("split_text"),
3860+
])
3861+
var snapshot = try await pipeline.execute()
3862+
3863+
var expectedResults: [[String: Sendable]] = [
3864+
["split_text": ["a", "b", "c"]],
3865+
]
3866+
TestHelper.compare(snapshot: snapshot, expected: expectedResults, enforceOrder: false)
3867+
3868+
// Test with expression delimiter (string)
3869+
pipeline = db.pipeline()
3870+
.documents([collRef.document("doc2").path])
3871+
.select([
3872+
Field("text").split(delimiter: Field("delimiter")).as("split_text"),
3873+
])
3874+
snapshot = try await pipeline.execute()
3875+
3876+
expectedResults = [
3877+
["split_text": ["x", "y", "z"]],
3878+
]
3879+
TestHelper.compare(snapshot: snapshot, expected: expectedResults, enforceOrder: false)
3880+
3881+
// Test with expression delimiter (bytes)
3882+
pipeline = db.pipeline()
3883+
.documents([collRef.document("doc3").path])
3884+
.select([
3885+
Field("text").split(delimiter: Field("delimiter")).as("split_text"),
3886+
])
3887+
snapshot = try await pipeline.execute()
3888+
3889+
let expectedByteResults: [[String: Sendable]] = [
3890+
["split_text": [Data([0x61]), Data([0x62]), Data([0x63])]],
3891+
]
3892+
TestHelper.compare(snapshot: snapshot, expected: expectedByteResults, enforceOrder: false)
3893+
}
38473894
}

0 commit comments

Comments
 (0)