From bd122a10802455f30a441d031530e005110643a1 Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki <dariusz@elixxir.io> Date: Tue, 26 Jul 2022 17:00:25 +0100 Subject: [PATCH] Add FileSend model --- Sources/ElixxirDAppsSDK/Models/FileSend.swift | 37 +++++++++++++++++++ .../Models/FileSendTests.swift | 35 ++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 Sources/ElixxirDAppsSDK/Models/FileSend.swift create mode 100644 Tests/ElixxirDAppsSDKTests/Models/FileSendTests.swift diff --git a/Sources/ElixxirDAppsSDK/Models/FileSend.swift b/Sources/ElixxirDAppsSDK/Models/FileSend.swift new file mode 100644 index 00000000..1d925e6f --- /dev/null +++ b/Sources/ElixxirDAppsSDK/Models/FileSend.swift @@ -0,0 +1,37 @@ +import Foundation + +public struct FileSend: Equatable { + public init( + name: String, + type: String, + preview: Data, + contents: Data + ) { + self.name = name + self.type = type + self.preview = preview + self.contents = contents + } + + public var name: String + public var type: String + public var preview: Data + public var contents: Data +} + +extension FileSend: Codable { + enum CodingKeys: String, CodingKey { + case name = "Name" + case type = "Type" + case preview = "Preview" + case contents = "Contents" + } + + 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/FileSendTests.swift b/Tests/ElixxirDAppsSDKTests/Models/FileSendTests.swift new file mode 100644 index 00000000..62296de5 --- /dev/null +++ b/Tests/ElixxirDAppsSDKTests/Models/FileSendTests.swift @@ -0,0 +1,35 @@ +import CustomDump +import XCTest +@testable import ElixxirDAppsSDK + +final class FileSendTests: XCTestCase { + func testCoding() throws { + let name = "testfile.txt" + let type = "text file" + let previewB64 = "aXQncyBtZSBhIHByZXZpZXc=" + let contentsB64 = "VGhpcyBpcyB0aGUgZnVsbCBjb250ZW50cyBvZiB0aGUgZmlsZSBpbiBieXRlcw==" + let jsonString = """ + { + "Name": "\(name)", + "Type": "\(type)", + "Preview": "\(previewB64)", + "Contents": "\(contentsB64)" + } + """ + let jsonData = jsonString.data(using: .utf8)! + + let fileSend = try FileSend.decode(jsonData) + + XCTAssertNoDifference(fileSend, FileSend( + name: name, + type: type, + preview: Data(base64Encoded: previewB64)!, + contents: Data(base64Encoded: contentsB64)! + )) + + let encodedFileSend = try fileSend.encode() + let decodedFileSend = try FileSend.decode(encodedFileSend) + + XCTAssertNoDifference(decodedFileSend, fileSend) + } +} -- GitLab