From a56b7f1e9a958041a9322504cdf1411fa144c13a Mon Sep 17 00:00:00 2001
From: Dariusz Rybicki <dariusz@elixxir.io>
Date: Wed, 24 Aug 2022 13:19:01 +0100
Subject: [PATCH] Add UDSearchResult model

---
 Sources/XXClient/Models/UDSearchResult.swift  | 39 ++++++++++++++++
 .../Models/UDSearchResultTests.swift          | 45 +++++++++++++++++++
 2 files changed, 84 insertions(+)
 create mode 100644 Sources/XXClient/Models/UDSearchResult.swift
 create mode 100644 Tests/XXClientTests/Models/UDSearchResultTests.swift

diff --git a/Sources/XXClient/Models/UDSearchResult.swift b/Sources/XXClient/Models/UDSearchResult.swift
new file mode 100644
index 00000000..26e6bf4d
--- /dev/null
+++ b/Sources/XXClient/Models/UDSearchResult.swift
@@ -0,0 +1,39 @@
+import Foundation
+
+public struct UDSearchResult: Equatable {
+  public init(
+    id: Data,
+    facts: [Fact]
+  ) {
+    self.id = id
+    self.facts = facts
+  }
+
+  public var id: Data
+  public var facts: [Fact]
+}
+
+extension UDSearchResult: Codable {
+  enum CodingKeys: String, CodingKey {
+    case id = "ID"
+    case facts = "Facts"
+  }
+
+  public static func decode(_ data: Data) throws -> Self {
+    try JSONDecoder().decode(Self.self, from: data)
+  }
+
+  public func encode() throws -> Data {
+    try JSONEncoder().encode(self)
+  }
+}
+
+extension Array where Element == UDSearchResult {
+  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/XXClientTests/Models/UDSearchResultTests.swift b/Tests/XXClientTests/Models/UDSearchResultTests.swift
new file mode 100644
index 00000000..3037e773
--- /dev/null
+++ b/Tests/XXClientTests/Models/UDSearchResultTests.swift
@@ -0,0 +1,45 @@
+import CustomDump
+import XCTest
+@testable import XXClient
+
+final class UDSearchResultTests: XCTestCase {
+  func testCoding() throws {
+    let idB64 = "pYIpRwPy+FnOkl5tndkG8RC93W/t5b1lszqPpMDynlUD"
+    let facts: [Fact] = [
+      Fact(fact: "carlos_arimateias", type: 0),
+    ]
+    let jsonString = """
+    {
+      "ID": "\(idB64)",
+      "Facts": \(String(data: try! facts.encode(), encoding: .utf8)!)
+    }
+    """
+    let jsonData = jsonString.data(using: .utf8)!
+    let model = try UDSearchResult.decode(jsonData)
+
+    XCTAssertNoDifference(model, UDSearchResult(
+      id: Data(base64Encoded: idB64)!,
+      facts: facts
+    ))
+
+    let encodedModel = try model.encode()
+    let decodedModel = try UDSearchResult.decode(encodedModel)
+
+    XCTAssertNoDifference(decodedModel, model)
+  }
+
+  func testCodingArray() throws {
+    let models: [UDSearchResult] = [
+      UDSearchResult(
+        id: Data(base64Encoded: "pYIpRwPy+FnOkl5tndkG8RC93W/t5b1lszqPpMDynlUD")!,
+        facts: [
+          Fact(fact: "carlos_arimateias", type: 0),
+        ]
+      ),
+    ]
+    let encodedModels = try models.encode()
+    let decodedModels = try [UDSearchResult].decode(encodedModels)
+
+    XCTAssertNoDifference(decodedModels, models)
+  }
+}
-- 
GitLab