From 10b788a1b388dc41cd296d0cfb7c1395a9b27275 Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki <dariusz@elixxir.io> Date: Mon, 25 Jul 2022 18:34:36 +0100 Subject: [PATCH] Add GetFactsFromContact functor --- Sources/ElixxirDAppsSDK/Fact.swift | 39 +++++++++++++++++++ .../ElixxirDAppsSDK/GetFactsFromContact.swift | 30 ++++++++++++++ .../Legacy/ContactFactsProvider.swift | 34 ---------------- Sources/ElixxirDAppsSDK/Legacy/Fact.swift | 19 --------- 4 files changed, 69 insertions(+), 53 deletions(-) create mode 100644 Sources/ElixxirDAppsSDK/Fact.swift create mode 100644 Sources/ElixxirDAppsSDK/GetFactsFromContact.swift delete mode 100644 Sources/ElixxirDAppsSDK/Legacy/ContactFactsProvider.swift delete mode 100644 Sources/ElixxirDAppsSDK/Legacy/Fact.swift diff --git a/Sources/ElixxirDAppsSDK/Fact.swift b/Sources/ElixxirDAppsSDK/Fact.swift new file mode 100644 index 00000000..ecf0e64d --- /dev/null +++ b/Sources/ElixxirDAppsSDK/Fact.swift @@ -0,0 +1,39 @@ +import Foundation + +public struct Fact: Equatable { + public init( + fact: String, + type: Int + ) { + self.fact = fact + self.type = type + } + + public var fact: String + public var type: Int +} + +extension Fact: Codable { + enum CodingKeys: String, CodingKey { + case fact = "Fact" + case type = "Type" + } + + static func decode(_ data: Data) throws -> Fact { + try JSONDecoder().decode(Self.self, from: data) + } + + func encode() throws -> Data { + try JSONEncoder().encode(self) + } +} + +extension Array where Element == Fact { + static func decode(_ data: Data) throws -> [Fact] { + try JSONDecoder().decode(Self.self, from: data) + } + + func encode() throws -> Data { + try JSONEncoder().encode(self) + } +} diff --git a/Sources/ElixxirDAppsSDK/GetFactsFromContact.swift b/Sources/ElixxirDAppsSDK/GetFactsFromContact.swift new file mode 100644 index 00000000..697f7997 --- /dev/null +++ b/Sources/ElixxirDAppsSDK/GetFactsFromContact.swift @@ -0,0 +1,30 @@ +import Bindings +import XCTestDynamicOverlay + +public struct GetFactsFromContact { + public var run: (Data) throws -> [Fact] + + public func callAsFunction(contact: Data) throws -> [Fact] { + try run(contact) + } +} + +extension GetFactsFromContact { + public static let live = GetFactsFromContact { contact in + var error: NSError? + let data = BindingsGetFactsFromContact(contact, &error) + if let error = error { + throw error + } + guard let data = data else { + fatalError("BindingsGetFactsFromContact returned `nil` without providing error") + } + return try [Fact].decode(data) + } +} + +extension GetFactsFromContact { + public static let unimplemented = GetFactsFromContact( + run: XCTUnimplemented("\(Self.self)") + ) +} diff --git a/Sources/ElixxirDAppsSDK/Legacy/ContactFactsProvider.swift b/Sources/ElixxirDAppsSDK/Legacy/ContactFactsProvider.swift deleted file mode 100644 index 2029dc6f..00000000 --- a/Sources/ElixxirDAppsSDK/Legacy/ContactFactsProvider.swift +++ /dev/null @@ -1,34 +0,0 @@ -import Bindings - -public struct ContactFactsProvider { - public var get: (Data) throws -> [Fact] - - public func callAsFunction(contact: Data) throws -> [Fact] { - try get(contact) - } -} - -extension ContactFactsProvider { - public static let live = ContactFactsProvider { contact in - var error: NSError? - let factsData = BindingsGetFactsFromContact(contact, &error) - if let error = error { - throw error - } - guard let factsData = factsData else { - fatalError("BindingsGetFactsFromContact returned `nil` without providing error") - } - let decoder = JSONDecoder() - let facts = try decoder.decode([Fact].self, from: factsData) - return facts - } -} - -#if DEBUG -extension ContactFactsProvider { - public static let failing = ContactFactsProvider { _ in - struct NotImplemented: Error {} - throw NotImplemented() - } -} -#endif diff --git a/Sources/ElixxirDAppsSDK/Legacy/Fact.swift b/Sources/ElixxirDAppsSDK/Legacy/Fact.swift deleted file mode 100644 index fa4a277b..00000000 --- a/Sources/ElixxirDAppsSDK/Legacy/Fact.swift +++ /dev/null @@ -1,19 +0,0 @@ -public struct Fact: Equatable { - public init( - fact: String, - type: Int - ) { - self.fact = fact - self.type = type - } - - public var fact: String - public var type: Int -} - -extension Fact: Codable { - enum CodingKeys: String, CodingKey { - case fact = "Fact" - case type = "Type" - } -} -- GitLab