Skip to content

Commit

Permalink
fix: fixing cast error
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Mar 4, 2024
1 parent a9e5559 commit f413a71
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 19 additions & 3 deletions Sources/EOSIO/ABIDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -103,18 +104,33 @@ public extension ABIDecoder {
}

func decode<T: Decodable>(_ 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<Never>.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<T>(into: inout T) throws {
Expand Down
1 change: 1 addition & 0 deletions Tests/EOSIOTests/TypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func AssertABICodable<T: ABICodable & Equatable>(_ 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)
Expand Down

0 comments on commit f413a71

Please sign in to comment.