Skip to content

Commit

Permalink
Add missing documentation (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
omaralbeik authored Aug 25, 2022
1 parent 9005399 commit f8c5883
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.3
// swift-tools-version:5.4

import PackageDescription

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A typed key-value storage solution to store `Codable` types in various persisten

## Features

- [x] macOS Catalina+, iOS 13+, tvOS 13+, watchOS 6+.
- [x] macOS Catalina+, iOS 13+, tvOS 13+, watchOS 6+, any Linux supporting Swift 5.4+.
- [x] Store any `Codable` type.
- [x] Single API with implementations using User Default, file system, Core Data, Keychain, and fakes for testing.
- [x] Thread-safe implementations.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Blueprints/AnyMultiObjectStore.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// A type erased `MultiObjectStore`
/// A type erased `MultiObjectStore`.
public final class AnyMultiObjectStore<
Object: Codable & Identifiable
>: MultiObjectStore {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Blueprints/AnySingleObjectStore.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// A type erased `SingleObjectStore`
/// A type erased `SingleObjectStore`.
public final class AnySingleObjectStore<Object: Codable>: SingleObjectStore {
/// Create any store from a given store.
/// - Parameter store: store to erase its type.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Blueprints/MultiObjectStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public extension MultiObjectStore {
public extension MultiObjectStore where Object: Hashable {
/// Saves a set of objects to store.
/// - Parameter objects: array of objects to be saved.
/// - Throws error: any encoding errors.
/// - Throws error: any error that might occur during the save operation.
func save(_ objects: Set<Object>) throws {
try save(Array(objects))
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/CoreData/MultiCoreDataStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class MultiCoreDataStore<

/// Saves an object to store.
/// - Parameter object: object to be saved.
/// - Throws error: any encoding errors.
/// - Throws error: any error that might occur during the save operation.
public func save(_ object: Object) throws {
try sync {
let data = try encoder.encode(object)
Expand All @@ -62,7 +62,7 @@ public final class MultiCoreDataStore<

/// Saves an array of objects to store.
/// - Parameter objects: array of objects to be saved.
/// - Throws error: any encoding errors.
/// - Throws error: any error that might occur during the save operation.
public func save(_ objects: [Object]) throws {
try sync {
let pairs = try objects.map { object -> (key: String, data: Data) in
Expand Down
4 changes: 2 additions & 2 deletions Sources/FileSystem/MultiFileSystemStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public final class MultiFileSystemStore<

/// Saves an object to store.
/// - Parameter object: object to be saved.
/// - Throws error: any encoding errors.
/// - Throws error: any error that might occur during the save operation.
public func save(_ object: Object) throws {
try sync {
_ = try storeURL().path
Expand All @@ -58,7 +58,7 @@ public final class MultiFileSystemStore<

/// Saves an array of objects to store.
/// - Parameter objects: array of objects to be saved.
/// - Throws error: any encoding errors.
/// - Throws error: any error that might occur during the save operation.
public func save(_ objects: [Object]) throws {
try sync {
let pairs = try objects.map { object -> (url: URL, data: Data) in
Expand Down
2 changes: 1 addition & 1 deletion Sources/FileSystem/SingleFileSystemStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public final class SingleFileSystemStore<Object: Codable>: SingleObjectStore {

/// Saves an object to store.
/// - Parameter object: object to be saved.
/// - Throws error: any encoding errors.
/// - Throws error: any error that might occur during the save operation.
public func save(_ object: Object) throws {
try sync {
let data = try encoder.encode(object)
Expand Down
19 changes: 17 additions & 2 deletions Sources/Keychain/MultiKeychainStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public final class MultiKeychainStore<

/// Saves an object to store.
/// - Parameter object: object to be saved.
/// - Throws error: any encoding errors.
/// - Throws error: any error that might occur during the save operation.
public func save(_ object: Object) throws {
try sync {
let data = try encoder.encode(object)
Expand All @@ -58,7 +58,7 @@ public final class MultiKeychainStore<

/// Saves an array of objects to store.
/// - Parameter objects: array of objects to be saved.
/// - Throws error: any encoding errors.
/// - Throws error: any error that might occur during the save operation.
public func save(_ objects: [Object]) throws {
try sync {
let pairs = try objects.map { (try encoder.encode($0), $0.id) }
Expand All @@ -67,6 +67,9 @@ public final class MultiKeychainStore<
}

/// The number of all objects stored in store.
///
/// > Note: Errors thrown while performing the security query will be ignored and logged out to console
/// in DEBUG.
public var objectsCount: Int {
let query = generateQuery {
$0[kSecMatchLimit] = kSecMatchLimitAll
Expand All @@ -90,6 +93,10 @@ public final class MultiKeychainStore<
}

/// Wether the store contains a saved object with the given id.
///
/// > Note: Errors thrown while performing the security query will be ignored and logged out to console
/// in DEBUG.
///
/// - Parameter id: object id.
/// - Returns: true if store contains an object with the given id.
public func containsObject(withId id: Object.ID) -> Bool {
Expand All @@ -109,6 +116,10 @@ public final class MultiKeychainStore<
}

/// Returns an object for the given id, or `nil` if no object is found.
///
/// > Note: Errors thrown while performing the security query will be ignored and logged out to console
/// in DEBUG.
///
/// - Parameter id: object id.
/// - Returns: object with the given id, or`nil` if no object with the given id is found.
public func object(withId id: Object.ID) -> Object? {
Expand All @@ -131,6 +142,10 @@ public final class MultiKeychainStore<
}

/// Returns all objects in the store.
///
/// > Note: Errors thrown while performing the security query will be ignored and logged out to console
/// in DEBUG.
///
/// - Returns: collection containing all objects stored in store.
public func allObjects() -> [Object] {
let query = generateQuery {
Expand Down
8 changes: 6 additions & 2 deletions Sources/Keychain/SingleKeychainStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,19 @@ public final class SingleKeychainStore<Object: Codable>: SingleObjectStore {

/// Saves an object to store.
/// - Parameter object: object to be saved.
/// - Throws error: any encoding errors.
/// - Throws error: any error that might occur during the save operation.
public func save(_ object: Object) throws {
try sync {
let data = try encoder.encode(object)
try addOrUpdate(data)
}
}

/// Returns the object saved in the store
/// Returns the object saved in the store.
///
/// > Note: Errors thrown while performing the security query will be ignored and logged out to console
/// in DEBUG.
///
/// - Returns: object saved in the store. `nil` if no object is saved in store.
public func object() -> Object? {
let query = generateQuery {
Expand Down
4 changes: 2 additions & 2 deletions Sources/UserDefaults/MultiUserDefaultsStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final class MultiUserDefaultsStore<

/// Saves an object to store.
/// - Parameter object: object to be saved.
/// - Throws error: any encoding errors.
/// - Throws error: any error that might occur during the save operation.
public func save(_ object: Object) throws {
try sync {
let data = try encoder.encode(object)
Expand All @@ -61,7 +61,7 @@ public final class MultiUserDefaultsStore<

/// Saves an array of objects to store.
/// - Parameter objects: array of objects to be saved.
/// - Throws error: any encoding errors.
/// - Throws error: any error that might occur during the save operation.
public func save(_ objects: [Object]) throws {
try sync {
let pairs = try objects.map { object -> (key: String, data: Data) in
Expand Down
2 changes: 1 addition & 1 deletion Sources/UserDefaults/SingleUserDefaultsStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public final class SingleUserDefaultsStore<Object: Codable>: SingleObjectStore {

/// Saves an object to store.
/// - Parameter object: object to be saved.
/// - Throws error: any encoding errors.
/// - Throws error: any error that might occur during the save operation.
public func save(_ object: Object) throws {
try sync {
let data = try encoder.encode(object)
Expand Down
14 changes: 11 additions & 3 deletions Tests/Utils/MultiObjectStoreFake.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public final class MultiObjectStoreFake<

/// Saves an object to store.
/// - Parameter object: object to be saved.
/// - Throws error: any encoding errors.
/// - Throws error: any error that might occur during the save operation.
public func save(_ object: Object) throws {
if let error = error {
throw error
Expand Down Expand Up @@ -58,12 +58,20 @@ public final class MultiObjectStoreFake<

/// Removes object with the given id —if found—.
/// - Parameter id: id for the object to be deleted.
public func remove(withId id: Object.ID) {
/// - Throws error: any error that might occur during the removal operation.
public func remove(withId id: Object.ID) throws {
if let error = error {
throw error
}
dictionary[id] = nil
}

/// Removes all objects in store.
public func removeAll() {
/// - Throws error: any error that might occur during the removal operation.
public func removeAll() throws {
if let error = error {
throw error
}
dictionary = [:]
}
}
11 changes: 9 additions & 2 deletions Tests/Utils/SingleObjectStoreFake.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ public final class SingleObjectStoreFake<Object: Codable>: SingleObjectStore {

/// Saves an object to store.
/// - Parameter object: object to be saved.
/// - Throws error: any encoding errors.
/// - Throws error: any error that might occur during the save operation.
public func save(_ object: Object) throws {
if let error = error {
throw error
}
underlyingObject = object
}

Expand All @@ -38,7 +41,11 @@ public final class SingleObjectStoreFake<Object: Codable>: SingleObjectStore {
}

/// Removes any saved object in the store.
public func remove() {
/// - Throws error: any error that might occur during the removal operation.
public func remove() throws {
if let error = error {
throw error
}
underlyingObject = nil
}
}

0 comments on commit f8c5883

Please sign in to comment.