89 lines
4.0 KiB
Swift
89 lines
4.0 KiB
Swift
//
|
|
// Copyright (c) 2023 Touch Instinct
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the Software), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
//
|
|
|
|
import TISwiftUtils
|
|
|
|
public extension MigratingBackingStore where MigratingStorages: CodableKeyValueStorage,
|
|
StoreContent: Codable {
|
|
|
|
init<Value: Codable>(key: StorageKey<Value>,
|
|
storageContainer: MigratingStorages,
|
|
decoder: CodableKeyValueDecoder = JSONKeyValueDecoder(),
|
|
encoder: CodableKeyValueEncoder = JSONKeyValueEncoder())
|
|
where StoreContent == Value? {
|
|
|
|
let getClosure: GetClosure = { container in
|
|
Self.getValue(from: container, forKey: key, decoder: decoder, encoder: encoder)
|
|
}
|
|
|
|
let setClosure: SetClosure = { container, value in
|
|
try? container.setOrRemove(codableObject: value, forKey: key, encoder: encoder).get()
|
|
}
|
|
|
|
self.init(storageContainer: storageContainer, getClosure: getClosure, setClosure: setClosure)
|
|
}
|
|
|
|
init<Value: Codable>(wrappedValue: Value,
|
|
key: StorageKey<Value>,
|
|
storageContainer: MigratingStorages,
|
|
decoder: CodableKeyValueDecoder = JSONKeyValueDecoder(),
|
|
encoder: CodableKeyValueEncoder = JSONKeyValueEncoder())
|
|
where StoreContent == Value? {
|
|
|
|
let getClosure: GetClosure = { container in
|
|
Self.getValue(from: container, forKey: key, wrappedValue: wrappedValue, decoder: decoder, encoder: encoder)
|
|
}
|
|
|
|
let setClosure: SetClosure = { container, value in
|
|
try? container.setOrRemove(codableObject: value, forKey: key, encoder: encoder).get()
|
|
}
|
|
|
|
self.init(storageContainer: storageContainer, getClosure: getClosure, setClosure: setClosure)
|
|
}
|
|
|
|
static func getValue<Value: Codable>(from container: MigratingStorages,
|
|
forKey key: StorageKey<Value>,
|
|
wrappedValue: Value? = nil,
|
|
decoder: CodableKeyValueDecoder,
|
|
encoder: CodableKeyValueEncoder) -> StoreContent where StoreContent == Value? {
|
|
|
|
// Storage container can't handle migration when source storage has value and target doesn't.
|
|
// This happens because of Decodable restrictions for Value in method `codableObject(forKey:decoder)`
|
|
// and absence of encoder.
|
|
try? container.codableObject(forKey: key, decoder: decoder)
|
|
.flatMap {
|
|
let _ = container.set(encodableObject: $0, forKey: key, encoder: encoder)
|
|
|
|
return .success($0)
|
|
}
|
|
.flatMapError {
|
|
guard let wrappedValue else {
|
|
return .failure($0)
|
|
}
|
|
|
|
return container.set(encodableObject: wrappedValue, forKey: key, encoder: encoder)
|
|
.flatMap { _ in .success(wrappedValue) }
|
|
}
|
|
.get()
|
|
}
|
|
}
|