From 6056eae6adbc268963217a6c95a32a4dc4ee6884 Mon Sep 17 00:00:00 2001 From: Joannis Orlandos Date: Wed, 15 Mar 2023 10:34:04 +0100 Subject: [PATCH] Allow integer conversion in FastBSONDecoder --- .../Decoding/Fast/FastBSONDecoder.swift | 31 +++++++++++++------ Sources/BSON/Document/Document.swift | 2 +- Tests/BSONTests/BSONEncoderTests.swift | 2 +- Tests/BSONTests/BSONPublicTests.swift | 7 +++-- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/Sources/BSON/Codable/Decoding/Fast/FastBSONDecoder.swift b/Sources/BSON/Codable/Decoding/Fast/FastBSONDecoder.swift index 7d4c868..659509f 100644 --- a/Sources/BSON/Codable/Decoding/Fast/FastBSONDecoder.swift +++ b/Sources/BSON/Codable/Decoding/Fast/FastBSONDecoder.swift @@ -378,11 +378,16 @@ struct _FastSingleValueContainer: 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 { @@ -394,19 +399,25 @@ struct _FastSingleValueContainer: 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 { diff --git a/Sources/BSON/Document/Document.swift b/Sources/BSON/Document/Document.swift index 27418ef..7f40eba 100644 --- a/Sources/BSON/Document/Document.swift +++ b/Sources/BSON/Document/Document.swift @@ -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 /// diff --git a/Tests/BSONTests/BSONEncoderTests.swift b/Tests/BSONTests/BSONEncoderTests.swift index 3530121..ce06445 100644 --- a/Tests/BSONTests/BSONEncoderTests.swift +++ b/Tests/BSONTests/BSONEncoderTests.swift @@ -1,5 +1,5 @@ import Foundation -@testable import BSON +import BSON import XCTest @available(iOS 10.0, *) diff --git a/Tests/BSONTests/BSONPublicTests.swift b/Tests/BSONTests/BSONPublicTests.swift index 1156c35..5782d82 100644 --- a/Tests/BSONTests/BSONPublicTests.swift +++ b/Tests/BSONTests/BSONPublicTests.swift @@ -9,7 +9,7 @@ import NIOCore import Foundation import XCTest -@testable import BSON +import BSON #if os(Linux) import Glibc @@ -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() {