Skip to content

Commit

Permalink
Make value protocols alike
Browse files Browse the repository at this point in the history
  • Loading branch information
ApolloZhu committed Sep 18, 2019
1 parent cd391c2 commit e1109db
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public enum AsIsKeychainAccessStorable {
}

public protocol KeychainAccessStorable {
func asKeychainStorable() -> Result<AsIsKeychainAccessStorable, Error>
func asKeychainAccessStorable() -> Result<AsIsKeychainAccessStorable, Error>
static func fromKeychain(_ keychain: Keychain, forKey key: String) -> Self?
}

Expand All @@ -60,7 +60,7 @@ public class EFStorageKeychainAccessRef<Content: KeychainAccessStorable>: EFSing
try? storage.remove(key)
return
}
switch newValue.asKeychainStorable() {
switch newValue.asKeychainAccessStorable() {
case .success(.string(let string)):
do {
try storage.set(string, key: key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import KeychainAccess
import Foundation

public extension KeychainAccessStorable where Self: NSCoding {
func asKeychainStorable() -> Result<AsIsKeychainAccessStorable, Error> {
func asKeychainAccessStorable() -> Result<AsIsKeychainAccessStorable, Error> {
return .success(.data(NSKeyedArchiver.archivedData(withRootObject: self)))
}
static func fromKeychain(_ keychain: Keychain, forKey key: String) -> Self? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import KeychainAccess
import Foundation

extension String: KeychainAccessStorable {
public func asKeychainStorable() -> Result<AsIsKeychainAccessStorable, Error> {
public func asKeychainAccessStorable() -> Result<AsIsKeychainAccessStorable, Error> {
return .success(.string(self))
}
public static func fromKeychain(_ keychain: Keychain, forKey key: String) -> Self? {
Expand All @@ -18,7 +18,7 @@ extension String: KeychainAccessStorable {
}

extension Data: KeychainAccessStorable {
public func asKeychainStorable() -> Result<AsIsKeychainAccessStorable, Error> {
public func asKeychainAccessStorable() -> Result<AsIsKeychainAccessStorable, Error> {
return .success(.data(self))
}
public static func fromKeychain(_ keychain: Keychain, forKey key: String) -> Self? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import KeychainAccess

public extension KeychainAccessStorable where Self: RawRepresentable, Self.RawValue: KeychainAccessStorable {
func asKeychainStorable() -> Result<AsIsKeychainAccessStorable, Error> {
return rawValue.asKeychainStorable()
func asKeychainAccessStorable() -> Result<AsIsKeychainAccessStorable, Error> {
return rawValue.asKeychainAccessStorable()
}
static func fromKeychain(_ keychain: Keychain, forKey key: String) -> Self? {
return RawValue.fromKeychain(keychain, forKey: key)
Expand Down
13 changes: 7 additions & 6 deletions Sources/EFStorageUserDefaults/EFStorage+UserDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension UserDefaults: EFUnderlyingStorage {

/// NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary
public protocol UserDefaultsStorable {
func asUserDefaultsStorable() -> AsIsUserDefaultsStorable!
func asUserDefaultsStorable() -> Result<AsIsUserDefaultsStorable, Error>
static func fromUserDefaults(_ userDefaults: UserDefaults, forKey key: String) -> Self?
}

Expand All @@ -43,18 +43,19 @@ public class EFStorageUserDefaultsRef<Content: UserDefaultsStorable>: EFSingleIn
guard let newValue = content else {
return storage.removeObject(forKey: key)
}
if let storable = newValue.asUserDefaultsStorable() {
switch newValue.asUserDefaultsStorable() {
case .success(let storable):
storage.set(storable, forKey: key)
} else {
onConversionFailure(for: newValue)
case .failure(let error):
onConversionFailure(for: newValue, dueTo: error)
}
}
}

public dynamic func onConversionFailure(for content: Content) {
public dynamic func onConversionFailure(for content: Content, dueTo error: Error) {
assertionFailure("""
\(content) of type \(type(of: content)) \
is not storable in user defaults.
is not storable in user defaults because \(error).
""")
}

Expand Down
13 changes: 2 additions & 11 deletions Sources/EFStorageUserDefaults/UserDefaults+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,8 @@
import Foundation

public extension UserDefaultsStorable where Self: Codable {
func asUserDefaultsStorable() -> UserDefaultsStorable! {
do {
return try JSONEncoder().encode(self)
} catch {
return onConversionFailure(dueTo: error)
}
}

dynamic func onConversionFailure(dueTo error: Error) -> UserDefaultsStorable! {
assertionFailure(error.localizedDescription)
return nil
func asUserDefaultsStorable() -> Result<AsIsUserDefaultsStorable, Error> {
return Result { try JSONEncoder().encode(self) }
}

static func fromUserDefaults(_ userDefaults: UserDefaults, forKey key: String) -> Self? {
Expand Down
4 changes: 2 additions & 2 deletions Sources/EFStorageUserDefaults/UserDefaults+NSCoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import Foundation

public extension UserDefaultsStorable where Self: NSCoding {
func asUserDefaultsStorable() -> AsIsUserDefaultsStorable! {
return NSKeyedArchiver.archivedData(withRootObject: self)
func asUserDefaultsStorable() -> Result<AsIsUserDefaultsStorable, Error> {
return NSKeyedArchiver.archivedData(withRootObject: self).asUserDefaultsStorable()
}
static func fromUserDefaults(_ userDefaults: UserDefaults, forKey key: String) -> Self? {
guard let data = userDefaults.data(forKey: key) else { return nil }
Expand Down
8 changes: 4 additions & 4 deletions Sources/EFStorageUserDefaults/UserDefaults+Primitives.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import Foundation

extension URL: UserDefaultsStorable {
public func asUserDefaultsStorable() -> AsIsUserDefaultsStorable! {
return absoluteString
public func asUserDefaultsStorable() -> Result<AsIsUserDefaultsStorable, Error> {
return absoluteString.asUserDefaultsStorable()
}
public static func fromUserDefaults(_ userDefaults: UserDefaults, forKey key: String) -> URL? {
return userDefaults.url(forKey: key)
Expand All @@ -20,8 +20,8 @@ extension URL: UserDefaultsStorable {
public protocol AsIsUserDefaultsStorable: UserDefaultsStorable { }

extension AsIsUserDefaultsStorable {
public func asUserDefaultsStorable() -> AsIsUserDefaultsStorable! {
return self
public func asUserDefaultsStorable() -> Result<AsIsUserDefaultsStorable, Error> {
return .success(self)
}
public static func fromUserDefaults(_ userDefaults: UserDefaults, forKey key: String) -> Self? {
guard let object = userDefaults.object(forKey: key) else { return nil }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

public extension UserDefaultsStorable where Self: RawRepresentable, Self.RawValue: UserDefaultsStorable {
func asUserDefaultsStorable() -> UserDefaultsStorable! {
func asUserDefaultsStorable() -> Result<AsIsUserDefaultsStorable, Error> {
return rawValue.asUserDefaultsStorable()
}
static func fromUserDefaults(_ userDefaults: UserDefaults, forKey key: String) -> Self? {
Expand Down

0 comments on commit e1109db

Please sign in to comment.