Skip to content

Commit

Permalink
Allow integer conversion in FastBSONDecoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Joannis committed Mar 15, 2023
1 parent 3ca41d3 commit 6056eae
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
31 changes: 21 additions & 10 deletions Sources/BSON/Codable/Decoding/Fast/FastBSONDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,16 @@ struct _FastSingleValueContainer<P: Primitive>: SingleValueDecodingContainer, An
}

func decode(_ type: Int.Type) throws -> Int {
if let value = value as? Int {
switch value {
case let value as Int:
return value
case let value as Int32:
return Int(value)
case let value as _BSON64BitInteger where value >= Int.min && value <= Int.max:
return Int(value)
default:
throw BSONValueNotFound(type: Int.self, path: codingPath.map(\.stringValue))
}

throw BSONValueNotFound(type: Int.self, path: codingPath.map(\.stringValue))
}

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

func decode(_ type: Int32.Type) throws -> Int32 {
if let value = value as? Int32 {
switch value {
case let value as Int32:
return value
case let value as _BSON64BitInteger where value >= Int32.min && value <= Int32.max:
return Int32(value)
default:
throw BSONValueNotFound(type: Int32.self, path: codingPath.map(\.stringValue))
}

throw BSONValueNotFound(type: Int32.self, path: codingPath.map(\.stringValue))
}

func decode(_ type: Int64.Type) throws -> Int64 {
if let value = value as? Int64 {
return value
switch value {
case let value as Int64:
return Int64(value)
case let value as _BSON64BitInteger:
return Int64(value)
default:
throw BSONValueNotFound(type: Int32.self, path: codingPath.map(\.stringValue))
}

throw BSONValueNotFound(type: Int64.self, path: codingPath.map(\.stringValue))
}

func decode(_ type: UInt.Type) throws -> UInt {
Expand Down
2 changes: 1 addition & 1 deletion Sources/BSON/Document/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public struct Document: Primitive, @unchecked Sendable {
}

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

/// Creates a new empty BSONDocument
///
Expand Down
2 changes: 1 addition & 1 deletion Tests/BSONTests/BSONEncoderTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation
@testable import BSON
import BSON
import XCTest

@available(iOS 10.0, *)
Expand Down
7 changes: 5 additions & 2 deletions Tests/BSONTests/BSONPublicTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import NIOCore
import Foundation
import XCTest
@testable import BSON
import BSON

#if os(Linux)
import Glibc
Expand Down Expand Up @@ -160,7 +160,10 @@ final class BSONPublicTests: XCTestCase {
}

XCTAssertEqual(array.count, 17)
XCTAssertEqual(copy, array)

for _ in 0..<10_00_000 {
XCTAssertEqual(copy, array)
}
}

func testEquatability() {
Expand Down

0 comments on commit 6056eae

Please sign in to comment.