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..1e0b5db 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 { @@ -103,18 +104,33 @@ public extension ABIDecoder { } func decode(_ type: T.Type) throws -> T { +// print("Decoding type: \(type), value: \(self.data)") // Log the type being decoded +// print("Decoding type: \(type)") 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 is Array.Type: + return [] 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 { + return [] as! T + } default: + print("Type \(type) does not conform to ABIDecodable") // Log non-conforming type 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)