From af361ece03ea07f19969f8744e4d07b5bd643b0d Mon Sep 17 00:00:00 2001 From: Guilherme Souza Date: Sat, 15 Jan 2022 08:24:52 -0300 Subject: [PATCH] Add code docs --- Sources/ComposableKeychain/Interface.swift | 47 ++++++++++++++++------ Sources/ComposableKeychain/Live.swift | 16 ++++---- Sources/ComposableKeychain/Mocks.swift | 4 ++ 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/Sources/ComposableKeychain/Interface.swift b/Sources/ComposableKeychain/Interface.swift index d95b5d0..87ca519 100644 --- a/Sources/ComposableKeychain/Interface.swift +++ b/Sources/ComposableKeychain/Interface.swift @@ -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 @@ -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) + } + } } diff --git a/Sources/ComposableKeychain/Live.swift b/Sources/ComposableKeychain/Live.swift index fe24130..2219f8a 100644 --- a/Sources/ComposableKeychain/Live.swift +++ b/Sources/ComposableKeychain/Live.swift @@ -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) } ) } } diff --git a/Sources/ComposableKeychain/Mocks.swift b/Sources/ComposableKeychain/Mocks.swift index 0605280..e4a7fdf 100644 --- a/Sources/ComposableKeychain/Mocks.swift +++ b/Sources/ComposableKeychain/Mocks.swift @@ -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 }, @@ -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")