Skip to content

Commit

Permalink
Fix error when decoding int as double (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgomberg authored Apr 29, 2024
1 parent c148eb5 commit bf7d15c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,21 @@ extension AutomergeKeyedDecodingContainer {
) throws -> T {
let value = try getValue(forKey: key)

guard case let .Scalar(.F64(number)) = value else {
let description: String
switch value {
case let .Scalar(.F64(number)):
description = number.description
case let .Scalar(.Int(number)):
description = number.description
default:
throw createTypeMismatchError(type: T.self, forKey: key, value: value)
}

guard let floatingPoint = T(number.description) else {
guard let floatingPoint = T(description) else {
throw DecodingError.dataCorruptedError(
forKey: key,
in: self,
debugDescription: "Parsed Automerge number <\(number)> does not fit in \(T.self)."
debugDescription: "Parsed Automerge number <\(description)> does not fit in \(T.self)."
)
}

Expand Down
13 changes: 13 additions & 0 deletions Tests/AutomergeTests/CodableTests/AutomergeDecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ final class AutomergeDecoderTests: XCTestCase {
XCTAssertEqual(decodedStruct.votes, [3, 4, 5])
}

func testIntToDoubleDecode() throws {
struct StructWithDouble: Codable {
let count: Double
}
let decoder = AutomergeDecoder(doc: doc)

XCTAssertNoThrow(try decoder.decode(StructWithDouble.self))

let decodedStruct = try decoder.decode(StructWithDouble.self)

XCTAssertEqual(decodedStruct.count, 5)
}

func testListOfTextDecode() throws {
doc = Document()
let list = try! doc.putObject(obj: ObjId.ROOT, key: "list", ty: .List)
Expand Down

0 comments on commit bf7d15c

Please sign in to comment.