Skip to content

Commit

Permalink
Add ComposableKeychain
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilherme Souza committed Jan 15, 2022
0 parents commit 35b209f
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
79 changes: 79 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"object": {
"pins": [
{
"package": "combine-schedulers",
"repositoryURL": "https://github.com/pointfreeco/combine-schedulers",
"state": {
"branch": null,
"revision": "4cf088c29a20f52be0f2ca54992b492c54e0076b",
"version": "0.5.3"
}
},
{
"package": "KeychainAccess",
"repositoryURL": "https://github.com/kishikawakatsumi/KeychainAccess",
"state": {
"branch": null,
"revision": "84e546727d66f1adc5439debad16270d0fdd04e7",
"version": "4.2.2"
}
},
{
"package": "swift-case-paths",
"repositoryURL": "https://github.com/pointfreeco/swift-case-paths",
"state": {
"branch": null,
"revision": "241301b67d8551c26d8f09bd2c0e52cc49f18007",
"version": "0.8.0"
}
},
{
"package": "swift-collections",
"repositoryURL": "https://github.com/apple/swift-collections",
"state": {
"branch": null,
"revision": "48254824bb4248676bf7ce56014ff57b142b77eb",
"version": "1.0.2"
}
},
{
"package": "swift-composable-architecture",
"repositoryURL": "https://github.com/pointfreeco/swift-composable-architecture",
"state": {
"branch": null,
"revision": "ba9c626ab1b2b6af8cf684eebb2ab472fa5b6753",
"version": "0.33.1"
}
},
{
"package": "swift-custom-dump",
"repositoryURL": "https://github.com/pointfreeco/swift-custom-dump",
"state": {
"branch": null,
"revision": "51698ece74ecf31959d3fa81733f0a5363ef1b4e",
"version": "0.3.0"
}
},
{
"package": "swift-identified-collections",
"repositoryURL": "https://github.com/pointfreeco/swift-identified-collections",
"state": {
"branch": null,
"revision": "680bf440178a78a627b1c2c64c0855f6523ad5b9",
"version": "0.3.2"
}
},
{
"package": "xctest-dynamic-overlay",
"repositoryURL": "https://github.com/pointfreeco/xctest-dynamic-overlay",
"state": {
"branch": null,
"revision": "50a70a9d3583fe228ce672e8923010c8df2deddd",
"version": "0.2.1"
}
}
]
},
"version": 1
}
34 changes: 34 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "ComposableKeychain",
platforms: [
.iOS(.v13),
.macOS(.v10_15),
.tvOS(.v13),
.watchOS(.v6),
],
products: [
.library(
name: "ComposableKeychain",
targets: ["ComposableKeychain"])
],
dependencies: [
.package(url: "https://github.com/kishikawakatsumi/KeychainAccess", from: "4.2.2"),
.package(url: "https://github.com/pointfreeco/swift-composable-architecture", from: "0.33.0"),
],
targets: [
.target(
name: "ComposableKeychain",
dependencies: [
"KeychainAccess",
.product(name: "ComposableArchitecture", package: "swift-composable-architecture"),
]),
.testTarget(
name: "ComposableKeychainTests",
dependencies: ["ComposableKeychain"]),
]
)
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ComposableKeychain

A description of this package.
29 changes: 29 additions & 0 deletions Sources/ComposableKeychain/Interface.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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 removeAll: () throws -> Void
public var contains: (_ key: String) 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,
removeAll: @escaping () throws -> Void,
contains: @escaping (_ key: String) throws -> Bool
) {
self.getString = getString
self.getData = getData
self.setString = setString
self.setData = setData
self.remove = remove
self.removeAll = removeAll
self.contains = contains
}
}
16 changes: 16 additions & 0 deletions Sources/ComposableKeychain/Live.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import KeychainAccess

extension 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) },
removeAll: { try keychain.removeAll() },
contains: { try keychain.contains($0) }
)
}
}
44 changes: 44 additions & 0 deletions Sources/ComposableKeychain/Mocks.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
extension KeychainClient {
public static let noop = KeychainClient(
getString: { _ in nil },
getData: { _ in nil },
setString: { _, _ in },
setData: { _, _ in },
remove: { _ in },
removeAll: {},
contains: { _ in false }
)
}

#if DEBUG
import XCTestDynamicOverlay

extension KeychainClient {
public static let failing = KeychainClient(
getString: { key in
XCTFail("\(Self.self).getString(\(key)) is unimplemented")
return nil
},
getData: { key in
XCTFail("\(Self.self).getData(\(key)) is unimplemented")
return nil
},
setString: { _, key in
XCTFail("\(Self.self).setString(_,\(key)) is unimplemented")
},
setData: { _, key in
XCTFail("\(Self.self).setData(_,\(key)) is unimplemented")
},
remove: { key in
XCTFail("\(Self.self).remove(\(key)) is unimplemented")
},
removeAll: {
XCTFail("\(Self.self).removeAll() is unimplemented")
},
contains: { key in
XCTFail("\(Self.self).contains(\(key)) is unimplemented")
return false
}
)
}
#endif
8 changes: 8 additions & 0 deletions Tests/ComposableKeychainTests/ComposableKeychainTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import XCTest

@testable import ComposableKeychain

final class ComposableKeychainTests: XCTestCase {
func testExample() throws {
}
}

0 comments on commit 35b209f

Please sign in to comment.