From 6a540463535f8f38523e61ad5f9e6e2345921514 Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki <dariusz@elixxir.io> Date: Mon, 25 Jul 2022 18:39:48 +0100 Subject: [PATCH] Add SetFactsOnContact functor --- .../Legacy/ContactFactsSetter.swift | 37 ------------------- .../ElixxirDAppsSDK/SetFactsOnContact.swift | 34 +++++++++++++++++ 2 files changed, 34 insertions(+), 37 deletions(-) delete mode 100644 Sources/ElixxirDAppsSDK/Legacy/ContactFactsSetter.swift create mode 100644 Sources/ElixxirDAppsSDK/SetFactsOnContact.swift diff --git a/Sources/ElixxirDAppsSDK/Legacy/ContactFactsSetter.swift b/Sources/ElixxirDAppsSDK/Legacy/ContactFactsSetter.swift deleted file mode 100644 index 8dd0ed98..00000000 --- a/Sources/ElixxirDAppsSDK/Legacy/ContactFactsSetter.swift +++ /dev/null @@ -1,37 +0,0 @@ -import Bindings - -public struct ContactFactsSetter { - public var set: (Data, [Fact]) throws -> Data - - public func callAsFunction( - contact: Data, - facts: [Fact] - ) throws -> Data { - try set(contact, facts) - } -} - -extension ContactFactsSetter { - public static let live = ContactFactsSetter { contact, facts in - let encoder = JSONEncoder() - let factsData = try encoder.encode(facts) - var error: NSError? - let updatedContact = BindingsSetFactsOnContact(contact, factsData, &error) - if let error = error { - throw error - } - guard let updatedContact = updatedContact else { - fatalError("BindingsSetFactsOnContact returned `nil` without providing error") - } - return updatedContact - } -} - -#if DEBUG -extension ContactFactsSetter { - public static let failing = ContactFactsSetter { _, _ in - struct NotImplemented: Error {} - throw NotImplemented() - } -} -#endif diff --git a/Sources/ElixxirDAppsSDK/SetFactsOnContact.swift b/Sources/ElixxirDAppsSDK/SetFactsOnContact.swift new file mode 100644 index 00000000..739a4f17 --- /dev/null +++ b/Sources/ElixxirDAppsSDK/SetFactsOnContact.swift @@ -0,0 +1,34 @@ +import Bindings +import XCTestDynamicOverlay + +public struct SetFactsOnContact { + public var run: (Data, [Fact]) throws -> Data + + public func callAsFunction( + contact: Data, + facts: [Fact] + ) throws -> Data { + try run(contact, facts) + } +} + +extension SetFactsOnContact { + public static let live = SetFactsOnContact { contact, facts in + let factsData = try facts.encode() + var error: NSError? + let updatedContact = BindingsSetFactsOnContact(contact, factsData, &error) + if let error = error { + throw error + } + guard let updatedContact = updatedContact else { + fatalError("BindingsSetFactsOnContact returned `nil` without providing error") + } + return updatedContact + } +} + +extension SetFactsOnContact { + public static let unimplemented = SetFactsOnContact( + run: XCTUnimplemented("\(Self.self)") + ) +} -- GitLab