Skip to content

Commit 939542b

Browse files
committed
fix: toSwiftRawValue for decimal
1 parent 8a5cc96 commit 939542b

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Sources/Cadence/Value.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@ extension Value {
379379
result[$0.name] = bigInt.description
380380
} else if let bigUInt = value as? BigUInt {
381381
result[$0.name] = bigUInt.description
382+
} else if let decimal = value as? Decimal {
383+
result[$0.name] = decimal
382384
} else if let encodable = value as? Encodable {
383385
let value = try encodable.encoded(with: JSONEncoder())
384386
result[$0.name] = try JSONSerialization.jsonObject(

Tests/CadenceTests/ValueTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,4 +657,24 @@ final class ValueTests: XCTestCase {
657657
).description, "Capability<Int>(address: 0x0000000102030405, path: /storage/foo)")
658658
}
659659

660+
func testToSwiftValueDecodable() throws {
661+
// Arrange
662+
struct TestStruct: Codable, Equatable {
663+
let value: Decimal
664+
}
665+
let decimalValue = Decimal(string: "0.007601")!
666+
let value: Cadence.Value = .optional(.struct(
667+
id: "id",
668+
fields: [
669+
.init(name: "value", value: .ufix64(decimalValue)),
670+
])
671+
)
672+
673+
// Act
674+
let result: TestStruct = try value.toSwiftValue()
675+
676+
// Assert
677+
XCTAssertEqual(result, TestStruct(value: decimalValue))
678+
}
679+
660680
}

0 commit comments

Comments
 (0)