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 5, 2024
1 parent a9e5559 commit c83fc6c
Show file tree
Hide file tree
Showing 3 changed files with 24 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.

19 changes: 16 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 @@ -105,16 +106,28 @@ public extension ABIDecoder {
func decode<T: Decodable>(_ 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<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 c83fc6c

Please sign in to comment.