KeychainClient #1604
-
I have create KeychainClient work with DependencyKey but having some issue and like to get some feed back and if need to improve it
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Hey @saroar! struct KeychainClient {
var save: @Sendable (_ data: Data, _ service: ServiceKeys, _ account: String) async throws
var update: @Sendable (_ password: Data, _ service: ServiceKeys, _ account: String) async throws
var read: @Sendable (_ service: ServiceKeys, _ account: String) throws -> Data
var deletePassword: @Sendable (_ service: ServiceKeys, _ account: String) async throws
func readCodable<T: Codable>(_ service: ServiceKeys, _ account: String, _ type: T.Type) throws -> T {
do {
let data = try read(service, account)
let item = try JSONDecoder().decode(type, from: data)
return item
} catch {
throw KeychainError.itemNotFound
}
}
} And you then create types of this value: static let liveValue = KeychainClient(
save: { … },
update: { … },
read: { … },
deletePassword: { … }
) and the same for The generic function would be tricky to model using closures, but I don't think this is something that is parametric here, once you have a way to serialize to data, so you can define it directly once and for all in the |
Beta Was this translation helpful? Give feedback.
Hey @saroar!
testValue
should return the same type asliveValue
, and in your case,KeychainClientMock
is not aKeychainClient
. It could probably work usingany KeychainProtocol
instead. Another convenient approach would be to remove theKeychainProtocol
and use this kind of construct: