Skip to content

Commit

Permalink
Update decode method
Browse files Browse the repository at this point in the history
  • Loading branch information
lmcmz committed Nov 24, 2022
1 parent 101a415 commit ffc5fb5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Sources/Flow/Decode/FlowArgument+Decode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Foundation
protocol FlowCodable {
func decode() -> Any?

func decode<T: Decodable>(_ decodable: T.Type) throws -> T?
func decode<T: Decodable>(_ decodable: T.Type) throws -> T

func decode<T: Decodable>() throws -> T
}
Expand Down Expand Up @@ -50,7 +50,7 @@ extension Flow.Argument: FlowCodable {
}
}

public func decode<T: Decodable>(_: T.Type) throws -> T? {
public func decode<T: Decodable>(_: T.Type) throws -> T {
do {
let result: T = try decode()
return result
Expand Down
7 changes: 5 additions & 2 deletions Sources/Flow/Models/FlowEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,11 @@ extension Flow.Event.Payload: FlowCodable {
return fields?.decode()
}

public func decode<T: Decodable>(_ decodable: T.Type) throws -> T? {
return try fields?.decode(decodable)
public func decode<T: Decodable>(_ decodable: T.Type) throws -> T {
guard let result: T = try? fields?.decode() else {
throw Flow.FError.decodeFailure
}
return result
}

public func decode<T: Decodable>() throws -> T {
Expand Down
7 changes: 5 additions & 2 deletions Sources/Flow/Models/FlowScript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ extension Flow.ScriptResponse: FlowCodable {
return fields?.decode()
}

public func decode<T: Decodable>(_ decodable: T.Type) throws -> T? {
return try fields?.decode(decodable)
public func decode<T: Decodable>(_ decodable: T.Type) throws -> T {
guard let result: T = try? fields?.decode() else {
throw Flow.FError.decodeFailure
}
return result
}

public func decode<T: Decodable>() throws -> T {
Expand Down
5 changes: 5 additions & 0 deletions Tests/FlowAccessAPIOnSandboxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ final class FlowAccessAPIOnSandboxTests: XCTestCase {
let isConnected = try await flowAPI.ping()
XCTAssertTrue(isConnected)
}

func testFlowAccount() async throws {
let account = try await flow.getAccountAtLatestBlock(address: "0x4e8e130b4fb9aee2")
XCTAssertNotNil(account)
}
}

0 comments on commit ffc5fb5

Please sign in to comment.