Skip to content

Commit

Permalink
Add code docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilherme Souza committed Jan 15, 2022
1 parent 35b209f commit af361ec
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
47 changes: 35 additions & 12 deletions Sources/ComposableKeychain/Interface.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import Foundation

public struct KeychainClient {
public var getString: (_ key: String) throws -> String?
public var getData: (_ key: String) throws -> Data?
public var setString: (_ value: String, _ key: String) throws -> Void
public var setData: (_ value: Data, _ key: String) throws -> Void
public var remove: (_ key: String) throws -> Void
public var getString: (_ key: Key) throws -> String?
public var getData: (_ key: Key) throws -> Data?
public var setString: (_ value: String, _ key: Key) throws -> Void
public var setData: (_ value: Data, _ key: Key) throws -> Void
public var remove: (_ key: Key) throws -> Void
public var removeAll: () throws -> Void
public var contains: (_ key: String) throws -> Bool
public var contains: (_ key: Key) throws -> Bool

public init(
getString: @escaping (_ key: String) throws -> String?,
getData: @escaping (_ key: String) throws -> Data?,
setString: @escaping (_ value: String, _ key: String) throws -> Void,
setData: @escaping (_ value: Data, _ key: String) throws -> Void,
remove: @escaping (_ key: String) throws -> Void,
getString: @escaping (_ key: Key) throws -> String?,
getData: @escaping (_ key: Key) throws -> Data?,
setString: @escaping (_ value: String, _ key: Key) throws -> Void,
setData: @escaping (_ value: Data, _ key: Key) throws -> Void,
remove: @escaping (_ key: Key) throws -> Void,
removeAll: @escaping () throws -> Void,
contains: @escaping (_ key: String) throws -> Bool
contains: @escaping (_ key: Key) throws -> Bool
) {
self.getString = getString
self.getData = getData
Expand All @@ -26,4 +26,27 @@ public struct KeychainClient {
self.removeAll = removeAll
self.contains = contains
}

/// Type safe key used for looking up the Keychain.
///
/// Example:
/// ```swift
/// extension KeychainClient.Key {
/// static let currentSession = Key("current_session")
/// }
///
/// let keychain = KeychainClient.live(...)
/// let data = try keychain.getData(.currentSession)
/// ```
public struct Key: RawRepresentable {
public var rawValue: String

public init(rawValue: String) {
self.rawValue = rawValue
}

public init(_ rawValue: String) {
self.init(rawValue: rawValue)
}
}
}
16 changes: 9 additions & 7 deletions Sources/ComposableKeychain/Live.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import KeychainAccess

extension KeychainClient {

/// Creates a live implementation of the KeychainClient.
/// - Parameter keychain: Configured Keychain instance to use.
/// - Returns: The KeychainClient.
public static func live(keychain: Keychain) -> KeychainClient {
return KeychainClient(
getString: { try keychain.get($0) },
getData: { try keychain.getData($0) },
setString: { try keychain.set($0, key: $1) },
setData: { try keychain.set($0, key: $1) },
remove: { try keychain.remove($0) },
getString: { try keychain.get($0.rawValue) },
getData: { try keychain.getData($0.rawValue) },
setString: { try keychain.set($0, key: $1.rawValue) },
setData: { try keychain.set($0, key: $1.rawValue) },
remove: { try keychain.remove($0.rawValue) },
removeAll: { try keychain.removeAll() },
contains: { try keychain.contains($0) }
contains: { try keychain.contains($0.rawValue) }
)
}
}
4 changes: 4 additions & 0 deletions Sources/ComposableKeychain/Mocks.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
extension KeychainClient {
/// A KeychainClient implementation that does nothing when called.
/// Can be used on Xcode Previews.
public static let noop = KeychainClient(
getString: { _ in nil },
getData: { _ in nil },
Expand All @@ -14,6 +16,8 @@ extension KeychainClient {
import XCTestDynamicOverlay

extension KeychainClient {
/// A KeychainClient implementation that fails when called.
/// Used for testing purposes and available only in DEBUG builds.
public static let failing = KeychainClient(
getString: { key in
XCTFail("\(Self.self).getString(\(key)) is unimplemented")
Expand Down

0 comments on commit af361ec

Please sign in to comment.