-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMockKeychain.swift
More file actions
47 lines (40 loc) · 1.65 KB
/
Copy pathMockKeychain.swift
File metadata and controls
47 lines (40 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import Foundation
public actor MockKeychain: KeychainProtocol {
private var storage: [String: Data] = [:]
//
// Data accessors
//
public func data(for key: String) throws -> Data? { storage[key] }
public func set(data: Data, for key: String) throws { storage[key] = data }
public func removeData(for key: String) throws { storage[key] = nil }
public func reset() throws { storage.removeAll() }
//
// Codable accessors
//
public func string(for key: String) throws -> String? { try decode(key: key) }
public func set(string: String, for key: String) throws { try store(string, for: key)}
public func bool(for key: String) throws -> Bool? { try decode(key: key) }
public func set(bool: Bool, for key: String) throws { try store(bool, for: key) }
public func int(for key: String) throws -> Int? { try decode(key: key) }
public func set(int: Int, for key: String) throws { try store(int, for: key) }
//
// JSONSerialization accessors
//
public func value(for key: String) throws -> Any? {
guard let data = storage[key] else { return nil }
return try JSONSerialization.jsonObject(with: data, options: [.fragmentsAllowed])
}
public func set(value: Any, for key: String) throws {
storage[key] = try JSONSerialization.data(withJSONObject: data, options: [.fragmentsAllowed])
}
//
// Private helpers
//
private func decode<T: Codable>(key: String, as: T.Type = T.self) throws -> T? {
guard let data = storage[key] else { return nil }
return try JSONDecoder().decode(T.self, from: data)
}
private func store<T: Codable>(_ value: T, for key: String) throws {
storage[key] = try JSONEncoder().encode(value)
}
}