Skip to content

Commit 6056eae

Browse files
committed
Allow integer conversion in FastBSONDecoder
1 parent 3ca41d3 commit 6056eae

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

Sources/BSON/Codable/Decoding/Fast/FastBSONDecoder.swift

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,16 @@ struct _FastSingleValueContainer<P: Primitive>: SingleValueDecodingContainer, An
378378
}
379379

380380
func decode(_ type: Int.Type) throws -> Int {
381-
if let value = value as? Int {
381+
switch value {
382+
case let value as Int:
382383
return value
384+
case let value as Int32:
385+
return Int(value)
386+
case let value as _BSON64BitInteger where value >= Int.min && value <= Int.max:
387+
return Int(value)
388+
default:
389+
throw BSONValueNotFound(type: Int.self, path: codingPath.map(\.stringValue))
383390
}
384-
385-
throw BSONValueNotFound(type: Int.self, path: codingPath.map(\.stringValue))
386391
}
387392

388393
func decode(_ type: Int8.Type) throws -> Int8 {
@@ -394,19 +399,25 @@ struct _FastSingleValueContainer<P: Primitive>: SingleValueDecodingContainer, An
394399
}
395400

396401
func decode(_ type: Int32.Type) throws -> Int32 {
397-
if let value = value as? Int32 {
402+
switch value {
403+
case let value as Int32:
398404
return value
405+
case let value as _BSON64BitInteger where value >= Int32.min && value <= Int32.max:
406+
return Int32(value)
407+
default:
408+
throw BSONValueNotFound(type: Int32.self, path: codingPath.map(\.stringValue))
399409
}
400-
401-
throw BSONValueNotFound(type: Int32.self, path: codingPath.map(\.stringValue))
402410
}
403411

404412
func decode(_ type: Int64.Type) throws -> Int64 {
405-
if let value = value as? Int64 {
406-
return value
413+
switch value {
414+
case let value as Int64:
415+
return Int64(value)
416+
case let value as _BSON64BitInteger:
417+
return Int64(value)
418+
default:
419+
throw BSONValueNotFound(type: Int32.self, path: codingPath.map(\.stringValue))
407420
}
408-
409-
throw BSONValueNotFound(type: Int64.self, path: codingPath.map(\.stringValue))
410421
}
411422

412423
func decode(_ type: UInt.Type) throws -> UInt {

Sources/BSON/Document/Document.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public struct Document: Primitive, @unchecked Sendable {
2525
}
2626

2727
/// Dictates whether this `Document` is an `Array` or `Dictionary`-like type
28-
public internal(set) var isArray: Bool
28+
public var isArray: Bool
2929

3030
/// Creates a new empty BSONDocument
3131
///

Tests/BSONTests/BSONEncoderTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Foundation
2-
@testable import BSON
2+
import BSON
33
import XCTest
44

55
@available(iOS 10.0, *)

Tests/BSONTests/BSONPublicTests.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import NIOCore
1010
import Foundation
1111
import XCTest
12-
@testable import BSON
12+
import BSON
1313

1414
#if os(Linux)
1515
import Glibc
@@ -160,7 +160,10 @@ final class BSONPublicTests: XCTestCase {
160160
}
161161

162162
XCTAssertEqual(array.count, 17)
163-
XCTAssertEqual(copy, array)
163+
164+
for _ in 0..<10_00_000 {
165+
XCTAssertEqual(copy, array)
166+
}
164167
}
165168

166169
func testEquatability() {

0 commit comments

Comments
 (0)