Add support for `UUID` (#92)
This commit is contained in:
parent
8c0d80e783
commit
c9198bb1d0
|
@ -297,6 +297,25 @@ extension Defaults {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension Defaults {
|
||||||
|
public struct UUIDBridge: Bridge {
|
||||||
|
public typealias Value = UUID
|
||||||
|
public typealias Serializable = String
|
||||||
|
|
||||||
|
public func serialize(_ value: Value?) -> Serializable? {
|
||||||
|
value?.uuidString
|
||||||
|
}
|
||||||
|
|
||||||
|
public func deserialize(_ object: Serializable?) -> Value? {
|
||||||
|
guard let object = object else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return .init(uuidString: object)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
extension Defaults {
|
extension Defaults {
|
||||||
@available(iOS 15.0, macOS 11.0, tvOS 15.0, watchOS 8.0, iOSApplicationExtension 15.0, macOSApplicationExtension 11.0, tvOSApplicationExtension 15.0, watchOSApplicationExtension 8.0, *)
|
@available(iOS 15.0, macOS 11.0, tvOS 15.0, watchOS 8.0, iOSApplicationExtension 15.0, macOSApplicationExtension 11.0, tvOSApplicationExtension 15.0, watchOSApplicationExtension 8.0, *)
|
||||||
public struct ColorBridge: Bridge {
|
public struct ColorBridge: Bridge {
|
||||||
|
|
|
@ -137,6 +137,9 @@ extension Dictionary: Defaults.Serializable where Key: LosslessStringConvertible
|
||||||
public static var bridge: Defaults.DictionaryBridge<Key, Value> { Defaults.DictionaryBridge() }
|
public static var bridge: Defaults.DictionaryBridge<Key, Value> { Defaults.DictionaryBridge() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension UUID: Defaults.Serializable {
|
||||||
|
public static let bridge = Defaults.UUIDBridge()
|
||||||
|
}
|
||||||
|
|
||||||
@available(iOS 15.0, macOS 11.0, tvOS 15.0, watchOS 8.0, iOSApplicationExtension 15.0, macOSApplicationExtension 11.0, tvOSApplicationExtension 15.0, watchOSApplicationExtension 8.0, *)
|
@available(iOS 15.0, macOS 11.0, tvOS 15.0, watchOS 8.0, iOSApplicationExtension 15.0, macOSApplicationExtension 11.0, tvOSApplicationExtension 15.0, watchOSApplicationExtension 8.0, *)
|
||||||
extension Color: Defaults.Serializable {
|
extension Color: Defaults.Serializable {
|
||||||
|
|
|
@ -14,6 +14,7 @@ extension Defaults.Keys {
|
||||||
static let file = Key<URL>("fileURL", default: fixtureFileURL)
|
static let file = Key<URL>("fileURL", default: fixtureFileURL)
|
||||||
static let data = Key<Data>("data", default: Data([]))
|
static let data = Key<Data>("data", default: Data([]))
|
||||||
static let date = Key<Date>("date", default: fixtureDate)
|
static let date = Key<Date>("date", default: fixtureDate)
|
||||||
|
static let uuid = Key<UUID?>("uuid")
|
||||||
}
|
}
|
||||||
|
|
||||||
final class DefaultsTests: XCTestCase {
|
final class DefaultsTests: XCTestCase {
|
||||||
|
@ -103,6 +104,12 @@ final class DefaultsTests: XCTestCase {
|
||||||
XCTAssertEqual(Defaults[.file], fixtureFileURL)
|
XCTAssertEqual(Defaults[.file], fixtureFileURL)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testUUIDType() {
|
||||||
|
let fixture = UUID()
|
||||||
|
Defaults[.uuid] = fixture
|
||||||
|
XCTAssertEqual(Defaults[.uuid], fixture)
|
||||||
|
}
|
||||||
|
|
||||||
func testRemoveAll() {
|
func testRemoveAll() {
|
||||||
let key = Defaults.Key<Bool>("removeAll", default: false)
|
let key = Defaults.Key<Bool>("removeAll", default: false)
|
||||||
let key2 = Defaults.Key<Bool>("removeAll2", default: false)
|
let key2 = Defaults.Key<Bool>("removeAll2", default: false)
|
||||||
|
|
Loading…
Reference in New Issue