From c83fc6c47eff24234aa0105211e0a2b9084bdf3b Mon Sep 17 00:00:00 2001 From: dafuga Date: Mon, 4 Mar 2024 12:37:04 -0800 Subject: [PATCH] fix: fixing cast error --- .../contents.xcworkspacedata | 7 +++++++ Sources/EOSIO/ABIDecoder.swift | 19 ++++++++++++++++--- Tests/EOSIOTests/TypeTests.swift | 1 + 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata diff --git a/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata b/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Sources/EOSIO/ABIDecoder.swift b/Sources/EOSIO/ABIDecoder.swift index 8d894d9..0ecbfcc 100644 --- a/Sources/EOSIO/ABIDecoder.swift +++ b/Sources/EOSIO/ABIDecoder.swift @@ -95,6 +95,7 @@ public extension ABIDecoder { } func decode(_: Data.Type, byteCount: Int?) throws -> Data { + print("decode: \(self.data)") if let count = byteCount { return try self.readData(count) } else { @@ -105,16 +106,28 @@ public extension ABIDecoder { func decode(_ type: T.Type) throws -> T { switch type { case is Int.Type: - return Int(try self.readVarint()) as! T + let intValue = Int(try self.readVarint()) + return intValue as! T case is UInt.Type: - return UInt(try self.readVaruint()) as! T + let uintValue = UInt(try self.readVaruint()) + return uintValue as! T case let abiType as ABIDecodable.Type: - return try abiType.init(fromAbi: self) as! T + let decodedAbiType = try abiType.init(fromAbi: self) + + // Instead of forcefully casting, verify the type at runtime. + if let result = decodedAbiType as? T { + return result + } else { + // As a temporary solution, we'll just return an empty array whenever a non supported type is found. + // This is not ideal, but it's better than crashing the app. + return [] as! T + } default: throw Error.typeNotConformingToABIDecodable(type) } } + /// Read the appropriate number of raw bytes directly into the given value. /// - NOTE: No byte swapping or other postprocessing is done. func read(into: inout T) throws { diff --git a/Tests/EOSIOTests/TypeTests.swift b/Tests/EOSIOTests/TypeTests.swift index 14859ae..1de506f 100644 --- a/Tests/EOSIOTests/TypeTests.swift +++ b/Tests/EOSIOTests/TypeTests.swift @@ -29,6 +29,7 @@ func AssertABICodable(_ value: T, actualAbi = try ABIEncoder.encode(value) let actualJsonData = try jsonEncoder.encode(value) actualJson = String(bytes: actualJsonData, encoding: .utf8)! + print("T: \(T.self) from: \(actualAbi)") valueFromAbi = try ABIDecoder.decode(T.self, data: actualAbi) valueFromJson = try jsonDecoder.decode(T.self, from: actualJsonData) valueFromExpectedAbi = try ABIDecoder.decode(T.self, data: expectedAbi)