-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update with changes for 2.0.0 * Remove unused workflow * Update PropertyWrappersTests * Remove ComposableCache from windows * Remove AnyCacheable from windows * Fix test to handle windows * Update test case * Add Logger typealias and Logging wrapper (#11) * Update README
- Loading branch information
Showing
13 changed files
with
1,006 additions
and
64 deletions.
There are no files selected for viewing
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,153 @@ | ||
#if !os(Windows) | ||
public class AnyCacheable: Cacheable { | ||
public typealias Key = AnyHashable | ||
public typealias Value = Any | ||
|
||
private var cache: any Cacheable | ||
|
||
private var cacheGet: ((AnyHashable) -> Any?)! | ||
private var cacheResolve: ((AnyHashable) throws -> Any)! | ||
private var cacheSet: ((Any, AnyHashable) -> Void)! | ||
private var cacheRemove: ((AnyHashable) -> Void)! | ||
private var cacheContains: ((AnyHashable) -> Bool)! | ||
private var cacheRequireKeys: ((Set<AnyHashable>) throws -> Void)! | ||
private var cacheRequireKey: ((AnyHashable) throws -> Void)! | ||
private var cacheValues: (() -> [AnyHashable: Any])! | ||
|
||
public init<InnerCache: Cacheable>(_ cache: InnerCache) { | ||
self.cache = cache | ||
|
||
self.cacheGet = { key in | ||
guard let key = key as? InnerCache.Key else { return nil } | ||
|
||
return cache.get(key, as: Any.self) | ||
} | ||
|
||
self.cacheResolve = { key in | ||
guard | ||
let key = key as? InnerCache.Key | ||
else { throw MissingRequiredKeysError(keys: [key]) } | ||
|
||
return try cache.resolve(key, as: Any.self) | ||
} | ||
|
||
self.cacheSet = { value, key in | ||
guard | ||
let value = value as? InnerCache.Value, | ||
let key = key as? InnerCache.Key | ||
else { return } | ||
|
||
var mutableCache = cache | ||
|
||
mutableCache.set(value: value, forKey: key) | ||
|
||
self.cache = mutableCache | ||
} | ||
|
||
self.cacheRemove = { key in | ||
guard let key = key as? InnerCache.Key else { return } | ||
|
||
var mutableCache = cache | ||
|
||
mutableCache.remove(key) | ||
|
||
self.cache = mutableCache | ||
} | ||
|
||
self.cacheContains = { key in | ||
guard | ||
let key = key as? InnerCache.Key | ||
else { return false } | ||
|
||
return cache.contains(key) | ||
} | ||
|
||
self.cacheRequireKeys = { keys in | ||
let validKeys: Set<InnerCache.Key> = Set(keys.compactMap { $0 as? InnerCache.Key }) | ||
|
||
guard | ||
validKeys.count == keys.count | ||
else { throw MissingRequiredKeysError(keys: keys.subtracting(validKeys)) } | ||
|
||
_ = try cache.require(keys: validKeys) | ||
} | ||
|
||
self.cacheRequireKey = { key in | ||
guard | ||
let key = key as? InnerCache.Key | ||
else { throw MissingRequiredKeysError(keys: [key]) } | ||
|
||
_ = try cache.require(key) | ||
} | ||
|
||
self.cacheValues = { | ||
cache.values(ofType: Any.self) | ||
} | ||
} | ||
|
||
required public convenience init(initialValues: [AnyHashable: Any]) { | ||
self.init(Cache(initialValues: initialValues)) | ||
} | ||
|
||
public func get<Output>( | ||
_ key: AnyHashable, | ||
as: Output.Type = Output.self | ||
) -> Output? { | ||
guard let value = cacheGet(key) else { | ||
return nil | ||
} | ||
|
||
guard let output = value as? Output else { | ||
return nil | ||
} | ||
|
||
return output | ||
} | ||
|
||
public func resolve<Output>( | ||
_ key: AnyHashable, | ||
as: Output.Type = Output.self | ||
) throws -> Output { | ||
let resolvedValue = try cacheResolve(key) | ||
|
||
guard let output = resolvedValue as? Output else { | ||
throw InvalidTypeError( | ||
expectedType: Output.self, | ||
actualType: type(of: get(key, as: Any.self)) | ||
) | ||
} | ||
|
||
return output | ||
} | ||
|
||
public func set(value: Value, forKey key: AnyHashable) { | ||
cacheSet(value, key) | ||
} | ||
|
||
public func remove(_ key: AnyHashable) { | ||
cacheRemove(key) | ||
} | ||
|
||
public func contains(_ key: AnyHashable) -> Bool { | ||
cacheContains(key) | ||
} | ||
|
||
public func require(keys: Set<AnyHashable>) throws -> Self { | ||
try cacheRequireKeys(keys) | ||
|
||
return self | ||
} | ||
|
||
public func require(_ key: AnyHashable) throws -> Self { | ||
try cacheRequireKey(key) | ||
|
||
return self | ||
} | ||
|
||
public func values<Output>(ofType: Output.Type) -> [AnyHashable: Output] { | ||
cacheValues().compactMapValues { value in | ||
value as? Output | ||
} | ||
} | ||
} | ||
#endif |
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,99 @@ | ||
#if !os(Windows) | ||
public struct ComposableCache<Key: Hashable>: Cacheable { | ||
private let caches: [AnyCacheable] | ||
|
||
public init(caches: [any Cacheable]) { | ||
self.caches = caches.map { AnyCacheable($0) } | ||
} | ||
|
||
public init(initialValues: [Key: Any]) { | ||
self.init(caches: [Cache(initialValues: initialValues)]) | ||
} | ||
|
||
public func get<Output>( | ||
_ key: Key, | ||
as: Output.Type = Output.self | ||
) -> Output? { | ||
for cache in caches { | ||
guard | ||
let output = cache.get(key, as: Output.self) | ||
else { | ||
continue | ||
} | ||
|
||
return output | ||
} | ||
|
||
return nil | ||
} | ||
|
||
public func resolve<Output>( | ||
_ key: Key, | ||
as: Output.Type = Output.self | ||
) throws -> Output { | ||
for cache in caches { | ||
guard | ||
let output = try? cache.resolve(key, as: Output.self) | ||
else { | ||
continue | ||
} | ||
|
||
return output | ||
} | ||
|
||
throw MissingRequiredKeysError(keys: [key]) | ||
} | ||
|
||
public func set(value: Any, forKey key: Key) { | ||
for cache in caches { | ||
cache.set(value: value, forKey: key) | ||
} | ||
} | ||
|
||
public func remove(_ key: Key) { | ||
for cache in caches { | ||
cache.remove(key) | ||
} | ||
} | ||
|
||
public func contains(_ key: Key) -> Bool { | ||
for cache in caches { | ||
if cache.contains(key) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
public func require(keys: Set<Key>) throws -> ComposableCache<Key> { | ||
for cache in caches { | ||
_ = try cache.require(keys: keys) | ||
} | ||
|
||
return self | ||
} | ||
|
||
public func require(_ key: Key) throws -> ComposableCache<Key> { | ||
for cache in caches { | ||
_ = try cache.require(key) | ||
} | ||
|
||
return self | ||
} | ||
|
||
public func values<Output>(ofType: Output.Type) -> [Key: Output] { | ||
for cache in caches { | ||
let values = cache.values(ofType: Output.self).compactMapKeys { $0 as? Key } | ||
|
||
guard values.keys.count != 0 else { | ||
continue | ||
} | ||
|
||
return values | ||
} | ||
|
||
return [:] | ||
} | ||
} | ||
#endif |
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,5 @@ | ||
public protocol CacheInitializable { | ||
associatedtype OriginCache: Cacheable | ||
|
||
init(cache: OriginCache) | ||
} |
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
Oops, something went wrong.