Skip to content

Commit b47a5cd

Browse files
committed
Fix bytes to data
1 parent 97599fd commit b47a5cd

File tree

4 files changed

+109
-162
lines changed

4 files changed

+109
-162
lines changed

Firestore/Swift/Source/Helper/PipelineHelper.swift

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -81,30 +81,14 @@ enum Helper {
8181
return Constant(value).bridge
8282
}
8383
}
84-
84+
8585
static func convertObjCToSwift(_ objValue: Sendable) -> Sendable? {
8686
switch objValue {
87-
case is NSNumber, is NSString:
88-
return objValue
89-
90-
case is NSNull:
91-
return nil
87+
case is NSNull:
88+
return nil
9289

93-
case let data as NSData:
94-
return [UInt8](data)
95-
96-
case let data as NSArray:
97-
return data.map{ convertObjCToSwift($0) }
98-
99-
case let data as NSDictionary:
100-
var swiftDict = [String: Sendable?]()
101-
for (key, value) in data {
102-
swiftDict[key as! String] = convertObjCToSwift(value)
103-
}
104-
return swiftDict
105-
106-
default:
107-
return objValue
108-
}
90+
default:
91+
return objValue
92+
}
10993
}
11094
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public struct Constant: Expr, BridgeWrapper, @unchecked Sendable {
5454
}
5555

5656
// Initializer for Bytes
57-
public init(_ value: [UInt8]) {
57+
public init(_ value: Data) {
5858
self.init(value as Any)
5959
}
6060

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public struct PipelineResult: @unchecked Sendable {
2727
self.bridge = bridge
2828
ref = self.bridge.reference
2929
id = self.bridge.documentID
30-
data = self.bridge.data().mapValues{ Helper.convertObjCToSwift($0)}
30+
data = self.bridge.data().mapValues { Helper.convertObjCToSwift($0) }
3131
createTime = self.bridge.create_time
3232
updateTime = self.bridge.update_time
3333
}

Firestore/Swift/Tests/Integration/PipelineTests.swift

Lines changed: 101 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,9 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
485485
Constant(GeoPoint(latitude: 0.1, longitude: 0.2)).as("geoPoint"),
486486
Constant(refTimestamp).as("timestamp"),
487487
Constant(refDate).as("date"), // Firestore will convert this to a Timestamp
488-
Constant([1, 2, 3, 4, 5, 6, 7, 0] as [UInt8]).as("bytes"),
488+
Constant(Data([1, 2, 3, 4, 5, 6, 7, 0])).as("bytes"),
489489
Constant(db.document("foo/bar")).as("documentReference"),
490490
Constant(VectorValue([1, 2, 3])).as("vectorValue"),
491-
Constant([1, 2, 3]).as("arrayValue"), // Treated as an array of numbers
492491
]
493492

494493
let constantsSecond: [Selectable] = [
@@ -500,7 +499,7 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
500499
"geoPoint": GeoPoint(latitude: 0.1, longitude: 0.2),
501500
"timestamp": refTimestamp,
502501
"date": refDate,
503-
"uint8Array": Data([1, 2, 3, 4, 5, 6, 7, 0]),
502+
"bytesArray": Data([1, 2, 3, 4, 5, 6, 7, 0]),
504503
"documentReference": Constant(db.document("foo/bar")),
505504
"vectorValue": VectorValue([1, 2, 3]),
506505
"map": [
@@ -517,7 +516,7 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
517516
GeoPoint(latitude: 10.1, longitude: 20.2),
518517
Timestamp(date: Date(timeIntervalSince1970: 1_700_000_000)), // Different timestamp
519518
Date(timeIntervalSince1970: 1_700_000_000), // Different date
520-
[11, 22, 33] as [UInt8],
519+
Data([11, 22, 33]),
521520
db.document("another/doc"),
522521
VectorValue([7, 8, 9]),
523522
[
@@ -536,7 +535,7 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
536535
"geoPoint": GeoPoint(latitude: 0.1, longitude: 0.2),
537536
"timestamp": refTimestamp,
538537
"date": refTimestamp, // Dates are converted to Timestamps
539-
"bytes": [1, 2, 3, 4, 5, 6, 7, 0] as [UInt8],
538+
"bytes": Data([1, 2, 3, 4, 5, 6, 7, 0]),
540539
"documentReference": db.document("foo/bar"),
541540
"vectorValue": VectorValue([1, 2, 3]),
542541
"arrayValue": [1, 2, 3],
@@ -548,7 +547,7 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
548547
"geoPoint": GeoPoint(latitude: 0.1, longitude: 0.2),
549548
"timestamp": refTimestamp,
550549
"date": refTimestamp,
551-
"uint8Array": Data([1, 2, 3, 4, 5, 6, 7, 0]),
550+
"bytesArray": Data([1, 2, 3, 4, 5, 6, 7, 0]),
552551
"documentReference": db.document("foo/bar"),
553552
"vectorValue": VectorValue([1, 2, 3]),
554553
"map": [
@@ -565,7 +564,7 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
565564
GeoPoint(latitude: 10.1, longitude: 20.2),
566565
Timestamp(date: Date(timeIntervalSince1970: 1_700_000_000)),
567566
Timestamp(date: Date(timeIntervalSince1970: 1_700_000_000)), // Dates are converted
568-
[11, 22, 33] as [UInt8],
567+
Data([11, 22, 33]),
569568
db.document("another/doc"),
570569
VectorValue([7, 8, 9]),
571570
[
@@ -586,45 +585,6 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
586585

587586
TestHelper.compare(pipelineResult: snapshot.results.first!, expected: expectedResultsMap)
588587
}
589-
590-
591-
func testFailed() async throws {
592-
let db = firestore()
593-
let randomCol = collectionRef() // Ensure a unique collection for the test
594-
595-
// Add a dummy document to the collection.
596-
// A pipeline query with .select against an empty collection might not behave as expected.
597-
try await randomCol.document("dummyDoc").setData(["field": "value"])
598-
599-
let constantsFirst: [Selectable] = [
600-
//Constant([1, 2, 3, 4, 5, 6, 7, 0] as [UInt8]).as("bytes"),
601-
Constant([1, 2, 3]).as("arrayValue"), // Treated as an array of numbers
602-
]
603-
604-
// let constantsSecond: [Selectable] = [
605-
// ArrayExpression([
606-
// [11, 22, 33] as [UInt8]
607-
// ]).as("array")
608-
// ]
609-
610-
let expectedResultsMap: [String: Sendable?] = [
611-
// "bytes": [1, 2, 3, 4, 5, 6, 7, 0] as [UInt8],
612-
// "array": [
613-
// [11, 22, 33] as [UInt8]
614-
// ],
615-
"arrayValue": [1, 2, 3]
616-
]
617-
618-
let pipeline = db.pipeline()
619-
.collection(randomCol.path)
620-
.limit(1)
621-
.select(
622-
constantsFirst
623-
)
624-
let snapshot = try await pipeline.execute()
625-
626-
TestHelper.compare(pipelineResult: snapshot.results.first!, expected: expectedResultsMap)
627-
}
628588

629589
func testAcceptsAndReturnsNil() async throws {
630590
let db = firestore()
@@ -2632,117 +2592,120 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
26322592
XCTFail("No document retrieved for testSupportsTimestampConversions")
26332593
}
26342594
}
2635-
2595+
26362596
func testSupportsTimestampMath() async throws {
2637-
let db = firestore()
2638-
let randomCol = collectionRef()
2639-
try await randomCol.document("dummyDoc").setData(["field": "value"])
2597+
let db = firestore()
2598+
let randomCol = collectionRef()
2599+
try await randomCol.document("dummyDoc").setData(["field": "value"])
26402600

2641-
let initialTimestamp = Timestamp(seconds: 1_741_380_235, nanoseconds: 0)
2601+
let initialTimestamp = Timestamp(seconds: 1_741_380_235, nanoseconds: 0)
26422602

2643-
let pipeline = db.pipeline()
2644-
.collection(randomCol.path)
2645-
.limit(1)
2646-
.select(
2647-
Constant(initialTimestamp).as("timestamp")
2648-
)
2649-
.select(
2650-
Field("timestamp").timestampAdd(.day, 10).as("plus10days"),
2651-
Field("timestamp").timestampAdd(.hour, 10).as("plus10hours"),
2652-
Field("timestamp").timestampAdd(.minute, 10).as("plus10minutes"),
2653-
Field("timestamp").timestampAdd(.second, 10).as("plus10seconds"),
2654-
Field("timestamp").timestampAdd(.microsecond, 10).as("plus10micros"),
2655-
Field("timestamp").timestampAdd(.millisecond, 10).as("plus10millis"),
2656-
Field("timestamp").timestampSub(.day, 10).as("minus10days"),
2657-
Field("timestamp").timestampSub(.hour, 10).as("minus10hours"),
2658-
Field("timestamp").timestampSub(.minute, 10).as("minus10minutes"),
2659-
Field("timestamp").timestampSub(.second, 10).as("minus10seconds"),
2660-
Field("timestamp").timestampSub(.microsecond, 10).as("minus10micros"),
2661-
Field("timestamp").timestampSub(.millisecond, 10).as("minus10millis")
2662-
)
2603+
let pipeline = db.pipeline()
2604+
.collection(randomCol.path)
2605+
.limit(1)
2606+
.select(
2607+
Constant(initialTimestamp).as("timestamp")
2608+
)
2609+
.select(
2610+
Field("timestamp").timestampAdd(.day, 10).as("plus10days"),
2611+
Field("timestamp").timestampAdd(.hour, 10).as("plus10hours"),
2612+
Field("timestamp").timestampAdd(.minute, 10).as("plus10minutes"),
2613+
Field("timestamp").timestampAdd(.second, 10).as("plus10seconds"),
2614+
Field("timestamp").timestampAdd(.microsecond, 10).as("plus10micros"),
2615+
Field("timestamp").timestampAdd(.millisecond, 10).as("plus10millis"),
2616+
Field("timestamp").timestampSub(.day, 10).as("minus10days"),
2617+
Field("timestamp").timestampSub(.hour, 10).as("minus10hours"),
2618+
Field("timestamp").timestampSub(.minute, 10).as("minus10minutes"),
2619+
Field("timestamp").timestampSub(.second, 10).as("minus10seconds"),
2620+
Field("timestamp").timestampSub(.microsecond, 10).as("minus10micros"),
2621+
Field("timestamp").timestampSub(.millisecond, 10).as("minus10millis")
2622+
)
26632623

2664-
let snapshot = try await pipeline.execute()
2624+
let snapshot = try await pipeline.execute()
26652625

2666-
let expectedResults: [String: Timestamp] = [
2667-
"plus10days": Timestamp(seconds: 1_742_244_235, nanoseconds: 0),
2668-
"plus10hours": Timestamp(seconds: 1_741_416_235, nanoseconds: 0),
2669-
"plus10minutes": Timestamp(seconds: 1_741_380_835, nanoseconds: 0),
2670-
"plus10seconds": Timestamp(seconds: 1_741_380_245, nanoseconds: 0),
2671-
"plus10micros": Timestamp(seconds: 1_741_380_235, nanoseconds: 10_000),
2672-
"plus10millis": Timestamp(seconds: 1_741_380_235, nanoseconds: 10_000_000),
2673-
"minus10days": Timestamp(seconds: 1_740_516_235, nanoseconds: 0),
2674-
"minus10hours": Timestamp(seconds: 1_741_344_235, nanoseconds: 0),
2675-
"minus10minutes": Timestamp(seconds: 1_741_379_635, nanoseconds: 0),
2676-
"minus10seconds": Timestamp(seconds: 1_741_380_225, nanoseconds: 0),
2677-
"minus10micros": Timestamp(seconds: 1_741_380_234, nanoseconds: 999_990_000),
2678-
"minus10millis": Timestamp(seconds: 1_741_380_234, nanoseconds: 990_000_000)
2679-
]
2626+
let expectedResults: [String: Timestamp] = [
2627+
"plus10days": Timestamp(seconds: 1_742_244_235, nanoseconds: 0),
2628+
"plus10hours": Timestamp(seconds: 1_741_416_235, nanoseconds: 0),
2629+
"plus10minutes": Timestamp(seconds: 1_741_380_835, nanoseconds: 0),
2630+
"plus10seconds": Timestamp(seconds: 1_741_380_245, nanoseconds: 0),
2631+
"plus10micros": Timestamp(seconds: 1_741_380_235, nanoseconds: 10000),
2632+
"plus10millis": Timestamp(seconds: 1_741_380_235, nanoseconds: 10_000_000),
2633+
"minus10days": Timestamp(seconds: 1_740_516_235, nanoseconds: 0),
2634+
"minus10hours": Timestamp(seconds: 1_741_344_235, nanoseconds: 0),
2635+
"minus10minutes": Timestamp(seconds: 1_741_379_635, nanoseconds: 0),
2636+
"minus10seconds": Timestamp(seconds: 1_741_380_225, nanoseconds: 0),
2637+
"minus10micros": Timestamp(seconds: 1_741_380_234, nanoseconds: 999_990_000),
2638+
"minus10millis": Timestamp(seconds: 1_741_380_234, nanoseconds: 990_000_000),
2639+
]
26802640

2681-
XCTAssertEqual(snapshot.results.count, 1, "Should retrieve one document")
2682-
if let resultDoc = snapshot.results.first {
2683-
TestHelper.compare(pipelineResult: resultDoc, expected: expectedResults)
2684-
} else {
2685-
XCTFail("No document retrieved for timestamp math test")
2686-
}
2641+
XCTAssertEqual(snapshot.results.count, 1, "Should retrieve one document")
2642+
if let resultDoc = snapshot.results.first {
2643+
TestHelper.compare(pipelineResult: resultDoc, expected: expectedResults)
2644+
} else {
2645+
XCTFail("No document retrieved for timestamp math test")
26872646
}
2688-
2647+
}
2648+
26892649
func testSupportsByteLength() async throws {
2690-
let db = firestore()
2691-
let randomCol = collectionRef()
2692-
try await randomCol.document("dummyDoc").setData(["field": "value"])
2650+
let db = firestore()
2651+
let randomCol = collectionRef()
2652+
try await randomCol.document("dummyDoc").setData(["field": "value"])
26932653

2694-
let bytes : [UInt8] = [1, 2, 3, 4, 5, 6, 7, 0]
2654+
let bytes = Data([1, 2, 3, 4, 5, 6, 7, 0])
26952655

2696-
let pipeline = db.pipeline()
2697-
.collection(randomCol.path)
2698-
.limit(1)
2699-
.select(
2700-
Constant(bytes).as("bytes")
2701-
)
2702-
.select(
2703-
Field("bytes").byteLength().as("byteLength")
2704-
)
2656+
let pipeline = db.pipeline()
2657+
.collection(randomCol.path)
2658+
.limit(1)
2659+
.select(
2660+
Constant(bytes).as("bytes")
2661+
)
2662+
.select(
2663+
Field("bytes").byteLength().as("byteLength")
2664+
)
27052665

2706-
let snapshot = try await pipeline.execute()
2666+
let snapshot = try await pipeline.execute()
27072667

27082668
let expectedResults: [String: Sendable] = [
2709-
"byteLength": 8
2710-
]
2669+
"byteLength": 8,
2670+
]
27112671

2712-
XCTAssertEqual(snapshot.results.count, 1, "Should retrieve one document")
2713-
if let resultDoc = snapshot.results.first {
2714-
TestHelper.compare(pipelineResult: resultDoc, expected: expectedResults.mapValues { $0 as Sendable })
2715-
} else {
2716-
XCTFail("No document retrieved for byte length test")
2717-
}
2672+
XCTAssertEqual(snapshot.results.count, 1, "Should retrieve one document")
2673+
if let resultDoc = snapshot.results.first {
2674+
TestHelper.compare(
2675+
pipelineResult: resultDoc,
2676+
expected: expectedResults.mapValues { $0 as Sendable }
2677+
)
2678+
} else {
2679+
XCTFail("No document retrieved for byte length test")
27182680
}
2681+
}
27192682

2720-
func testSupportsNot() async throws {
2721-
let db = firestore()
2722-
let randomCol = collectionRef()
2723-
try await randomCol.document("dummyDoc").setData(["field": "value"])
2683+
func testSupportsNot() async throws {
2684+
let db = firestore()
2685+
let randomCol = collectionRef()
2686+
try await randomCol.document("dummyDoc").setData(["field": "value"])
27242687

2725-
let pipeline = db.pipeline()
2726-
.collection(randomCol.path)
2727-
.limit(1)
2728-
.select(Constant(true).as("trueField"))
2729-
.select(
2730-
Field("trueField"),
2731-
(!(Field("trueField").eq(true))).as("falseField")
2732-
)
2688+
let pipeline = db.pipeline()
2689+
.collection(randomCol.path)
2690+
.limit(1)
2691+
.select(Constant(true).as("trueField"))
2692+
.select(
2693+
Field("trueField"),
2694+
(!(Field("trueField").eq(true))).as("falseField")
2695+
)
27332696

2734-
let snapshot = try await pipeline.execute()
2697+
let snapshot = try await pipeline.execute()
27352698

2736-
let expectedResults: [String: Bool] = [
2737-
"trueField": true,
2738-
"falseField": false
2739-
]
2699+
let expectedResults: [String: Bool] = [
2700+
"trueField": true,
2701+
"falseField": false,
2702+
]
27402703

2741-
XCTAssertEqual(snapshot.results.count, 1, "Should retrieve one document")
2742-
if let resultDoc = snapshot.results.first {
2743-
TestHelper.compare(pipelineResult: resultDoc, expected: expectedResults)
2744-
} else {
2745-
XCTFail("No document retrieved for not operator test")
2746-
}
2704+
XCTAssertEqual(snapshot.results.count, 1, "Should retrieve one document")
2705+
if let resultDoc = snapshot.results.first {
2706+
TestHelper.compare(pipelineResult: resultDoc, expected: expectedResults)
2707+
} else {
2708+
XCTFail("No document retrieved for not operator test")
27472709
}
2710+
}
27482711
}

0 commit comments

Comments
 (0)