-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #189 from hyperoslo/refactor/generic_storage
Refactor to generic Storage
- Loading branch information
Showing
27 changed files
with
758 additions
and
618 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Foundation | ||
|
||
/// Helper class to hold cached instance and expiry date. | ||
/// Used in memory storage to work with NSCache. | ||
class MemoryCapsule: NSObject { | ||
/// Object to be cached | ||
let object: Any | ||
/// Expiration date | ||
let expiry: Expiry | ||
|
||
/** | ||
Creates a new instance of Capsule. | ||
- Parameter value: Object to be cached | ||
- Parameter expiry: Expiration date | ||
*/ | ||
init(value: Any, expiry: Expiry) { | ||
self.object = value | ||
self.expiry = expiry | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Foundation | ||
|
||
public extension Optional { | ||
func unwrapOrThrow(error: Error) throws -> Wrapped { | ||
if let value = self { | ||
return value | ||
} else { | ||
throw error | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Foundation | ||
|
||
public class Transformer<T> { | ||
let toData: (T) throws -> Data | ||
let fromData: (Data) throws -> T | ||
|
||
public init(toData: @escaping (T) throws -> Data, fromData: @escaping (Data) throws -> T) { | ||
self.toData = toData | ||
self.fromData = fromData | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Foundation | ||
|
||
public class TransformerFactory { | ||
public static func forData() -> Transformer<Data> { | ||
let toData: (Data) throws -> Data = { $0 } | ||
|
||
let fromData: (Data) throws -> Data = { $0 } | ||
|
||
return Transformer<Data>(toData: toData, fromData: fromData) | ||
} | ||
|
||
public static func forImage() -> Transformer<Image> { | ||
let toData: (Image) throws -> Data = { image in | ||
return try image.cache_toData().unwrapOrThrow(error: StorageError.transformerFail) | ||
} | ||
|
||
let fromData: (Data) throws -> Image = { data in | ||
return try Image(data: data).unwrapOrThrow(error: StorageError.transformerFail) | ||
} | ||
|
||
return Transformer<Image>(toData: toData, fromData: fromData) | ||
} | ||
|
||
public static func forCodable<U: Codable>(ofType: U.Type) -> Transformer<U> { | ||
let toData: (U) throws -> Data = { object in | ||
let wrapper = TypeWrapper<U>(object: object) | ||
let encoder = JSONEncoder() | ||
return try encoder.encode(wrapper) | ||
} | ||
|
||
let fromData: (Data) throws -> U = { data in | ||
let decoder = JSONDecoder() | ||
return try decoder.decode(TypeWrapper<U>.self, from: data).object | ||
} | ||
|
||
return Transformer<U>(toData: toData, fromData: fromData) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Foundation | ||
|
||
/// Used to wrap Codable object | ||
public struct TypeWrapper<T: Codable>: Codable { | ||
enum CodingKeys: String, CodingKey { | ||
case object | ||
} | ||
|
||
public let object: T | ||
|
||
public init(object: T) { | ||
self.object = object | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.