Skip to content
Snippets Groups Projects
Commit 71ba6b7f authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Add BroadcastMessage model

parent f66d4801
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!18Update Bindings
import Foundation
public struct BroadcastMessage: Equatable {
public init(
roundId: Int,
ephId: [Int],
payload: Data
) {
self.roundId = roundId
self.ephId = ephId
self.payload = payload
}
public var roundId: Int
public var ephId: [Int]
public var payload: Data
}
extension BroadcastMessage: Codable {
enum CodingKeys: String, CodingKey {
case roundId = "RoundID"
case ephId = "EphID"
case payload = "Payload"
}
public static func decode(_ data: Data) throws -> Self {
try JSONDecoder().decode(Self.self, from: data)
}
public func encode() throws -> Data {
try JSONEncoder().encode(self)
}
}
import CustomDump
import XCTest
@testable import ElixxirDAppsSDK
final class BroadcastMessageTests: XCTestCase {
func testCoding() throws {
let roundId: Int = 42
let ephId: [Int] = [0, 0, 0, 0, 0, 0, 24, 61]
let payloadB64 = "SGVsbG8sIGJyb2FkY2FzdCBmcmllbmRzIQ=="
let jsonString = """
{
"RoundID": \(roundId),
"EphID": [\(ephId.map { "\($0)" }.joined(separator: ", "))],
"Payload": "\(payloadB64)"
}
"""
let jsonData = jsonString.data(using: .utf8)!
let message = try BroadcastMessage.decode(jsonData)
XCTAssertNoDifference(message, BroadcastMessage(
roundId: roundId,
ephId: ephId,
payload: Data(base64Encoded: payloadB64)!
))
let encodedMessage = try message.encode()
let decodedMessage = try BroadcastMessage.decode(encodedMessage)
XCTAssertNoDifference(decodedMessage, message)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment