Skip to content

Commit

Permalink
Merge pull request #7 from swhitty/convert-double-int
Browse files Browse the repository at this point in the history
Decode BinaryInteger types from Doubles when exactly represented
  • Loading branch information
swhitty authored Jun 26, 2024
2 parents 91665a2 + 556379a commit 258299e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
10 changes: 9 additions & 1 deletion Sources/KeyValueDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,15 @@ private extension KeyValueDecoder {
throw DecodingError.typeMismatch(type, context)
}
return val
} else {
} else if let double = (value as? NSNumber)?.getDoubleValue() {
let val = T(double)
guard Double(val) == double else {
let context = DecodingError.Context(codingPath: codingPath, debugDescription: "\(valueDescription) at \(codingPath.makeKeyPath()), cannot be exactly represented by \(type)")
throw DecodingError.typeMismatch(type, context)
}
return val
}
else {
let context = DecodingError.Context(codingPath: codingPath, debugDescription: "Expected BinaryInteger at \(codingPath.makeKeyPath()), found \(valueDescription)")
if decodeNil() {
throw DecodingError.valueNotFound(type, context)
Expand Down
36 changes: 32 additions & 4 deletions Tests/KeyValueDecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ final class KeyValueDecoderTests: XCTestCase {
try KeyValueDecoder.decode(Int16.self, from: NSNumber(10)),
10
)
XCTAssertEqual(
try KeyValueDecoder.decode(Int16.self, from: 10.0),
10
)
XCTAssertEqual(
try KeyValueDecoder.decode(Int16.self, from: NSNumber(10.0)),
10
)
XCTAssertThrowsError(
try KeyValueDecoder.decode(Int8.self, from: Int16.max)
)
Expand All @@ -118,6 +126,12 @@ final class KeyValueDecoderTests: XCTestCase {
XCTAssertThrowsError(
try KeyValueDecoder.decode(Int16.self, from: NSNull())
)
XCTAssertThrowsError(
try KeyValueDecoder.decode(Int8.self, from: 10.1)
)
XCTAssertThrowsError(
try KeyValueDecoder.decode(Int8.self, from: NSNumber(10.1))
)
}

func testDecodes_UInts() {
Expand All @@ -129,6 +143,14 @@ final class KeyValueDecoderTests: XCTestCase {
try KeyValueDecoder.decode(UInt8.self, from: NSNumber(10)),
10
)
XCTAssertEqual(
try KeyValueDecoder.decode(UInt8.self, from: 10.0),
10
)
XCTAssertEqual(
try KeyValueDecoder.decode(UInt8.self, from: NSNumber(10.0)),
10
)
XCTAssertThrowsError(
try KeyValueDecoder.decode(UInt8.self, from: UInt16.max)
)
Expand All @@ -141,6 +163,12 @@ final class KeyValueDecoderTests: XCTestCase {
XCTAssertThrowsError(
try KeyValueDecoder.decode(UInt8.self, from: NSNull())
)
XCTAssertThrowsError(
try KeyValueDecoder.decode(UInt8.self, from: 10.1)
)
XCTAssertThrowsError(
try KeyValueDecoder.decode(UInt8.self, from: NSNumber(10.1))
)
}

func testDecodes_Float(){
Expand Down Expand Up @@ -480,8 +508,8 @@ final class KeyValueDecoderTests: XCTestCase {

func testDecodes_UnkeyedInts() {
XCTAssertEqual(
try KeyValueDecoder.decode([Int].self, from: [-10, 20, 30]),
[-10, 20, 30]
try KeyValueDecoder.decode([Int].self, from: [-10, 20, 30, 40.0, -50.0]),
[-10, 20, 30, 40, -50]
)
XCTAssertEqual(
try KeyValueDecoder.decode([Int8].self, from: [10, -20, 30]),
Expand Down Expand Up @@ -610,8 +638,8 @@ final class KeyValueDecoderTests: XCTestCase {

func testDecodes_UnkeyedUInts() {
XCTAssertEqual(
try KeyValueDecoder.decode([UInt].self, from: [10, 20, 30]),
[10, 20, 30]
try KeyValueDecoder.decode([UInt].self, from: [10, 20, 30, 40.0]),
[10, 20, 30, 40]
)
XCTAssertEqual(
try KeyValueDecoder.decode([UInt8].self, from: [10, 20, 30]),
Expand Down

0 comments on commit 258299e

Please sign in to comment.