diff --git a/Sources/ElixxirDAppsSDK/Models/BroadcastReport.swift b/Sources/ElixxirDAppsSDK/Models/BroadcastReport.swift new file mode 100644 index 0000000000000000000000000000000000000000..3114a12b733bd4f44829e7d55f8ed4534b2155cc --- /dev/null +++ b/Sources/ElixxirDAppsSDK/Models/BroadcastReport.swift @@ -0,0 +1,29 @@ +import Foundation + +public struct BroadcastReport: Equatable { + public init( + roundId: Int, + ephId: [Int] + ) { + self.roundId = roundId + self.ephId = ephId + } + + public var roundId: Int + public var ephId: [Int] +} + +extension BroadcastReport: Codable { + enum CodingKeys: String, CodingKey { + case roundId = "RoundID" + case ephId = "EphID" + } + + public static func decode(_ data: Data) throws -> Self { + try JSONDecoder().decode(Self.self, from: data) + } + + public func encode() throws -> Data { + try JSONEncoder().encode(self) + } +} diff --git a/Tests/ElixxirDAppsSDKTests/Models/BroadcastReportTests.swift b/Tests/ElixxirDAppsSDKTests/Models/BroadcastReportTests.swift new file mode 100644 index 0000000000000000000000000000000000000000..b94ca4a3cffdf8c87e557cfe82d318b985de03b8 --- /dev/null +++ b/Tests/ElixxirDAppsSDKTests/Models/BroadcastReportTests.swift @@ -0,0 +1,28 @@ +import CustomDump +import XCTest +@testable import ElixxirDAppsSDK + +final class BroadcastReportTests: XCTestCase { + func testCoding() throws { + let roundId: Int = 42 + let ephId: [Int] = [0, 0, 0, 0, 0, 0, 24, 61] + let jsonString = """ + { + "RoundID": \(roundId), + "EphID": [\(ephId.map { "\($0)" }.joined(separator: ", "))] + } + """ + let jsonData = jsonString.data(using: .utf8)! + let report = try BroadcastReport.decode(jsonData) + + XCTAssertNoDifference(report, BroadcastReport( + roundId: roundId, + ephId: ephId + )) + + let encodedReport = try report.encode() + let decodedReport = try BroadcastReport.decode(encodedReport) + + XCTAssertNoDifference(decodedReport, report) + } +}