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

Add MessageSendReport codable model

parent 0f945331
Branches
Tags
1 merge request!3Bindings models wrapper
import Foundation
public struct MessageSendReport: Equatable {
public init(
roundList: [Int],
messageId: Data,
timestamp: Int
) {
self.roundList = roundList
self.messageId = messageId
self.timestamp = timestamp
}
public var roundList: [Int]
public var messageId: Data
public var timestamp: Int
}
extension MessageSendReport: Codable {
enum CodingKeys: String, CodingKey {
case roundList = "RoundList"
case messageId = "MessageID"
case timestamp = "Timestamp"
}
enum RoundListCodingKeys: String, CodingKey {
case rounds = "Rounds"
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
messageId = try container.decode(Data.self, forKey: .messageId)
timestamp = try container.decode(Int.self, forKey: .timestamp)
let roundListContainer = try container.nestedContainer(
keyedBy: RoundListCodingKeys.self,
forKey: .roundList
)
roundList = try roundListContainer.decode([Int].self, forKey: .rounds)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(messageId, forKey: .messageId)
try container.encode(timestamp, forKey: .timestamp)
var roundListContainer = container.nestedContainer(
keyedBy: RoundListCodingKeys.self,
forKey: .roundList
)
try roundListContainer.encode(roundList, forKey: .rounds)
}
}
import CustomDump
import XCTest
@testable import ElixxirDAppsSDK
final class MessageSendReportTests: XCTestCase {
func testCoding() throws {
let messageId = secureRandomData(count: 32)
let jsonString = """
{
"RoundList": {
"Rounds": [1,5,9]
},
"MessageID": \(messageId.jsonEncodedBase64()),
"Timestamp": 1653582683183384000
}
"""
let jsonData = jsonString.data(using: .utf8)!
let decoder = JSONDecoder()
decoder.dataDecodingStrategy = .base64
let report = try decoder.decode(MessageSendReport.self, from: jsonData)
XCTAssertNoDifference(report, MessageSendReport(
roundList: [1, 5, 9],
messageId: messageId,
timestamp: 1_653_582_683_183_384_000
))
let encoder = JSONEncoder()
encoder.dataEncodingStrategy = .base64
let encodedReport = try encoder.encode(report)
let decodedReport = try decoder.decode(MessageSendReport.self, from: encodedReport)
XCTAssertNoDifference(decodedReport, report)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment