Add support for `UUID` (#92)

This commit is contained in:
Sindre Sorhus 2022-01-27 22:14:21 +07:00 committed by GitHub
parent 8c0d80e783
commit c9198bb1d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 0 deletions

View File

@ -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 {
@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 {

View File

@ -137,6 +137,9 @@ extension Dictionary: Defaults.Serializable where Key: LosslessStringConvertible
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, *)
extension Color: Defaults.Serializable {

View File

@ -14,6 +14,7 @@ extension Defaults.Keys {
static let file = Key<URL>("fileURL", default: fixtureFileURL)
static let data = Key<Data>("data", default: Data([]))
static let date = Key<Date>("date", default: fixtureDate)
static let uuid = Key<UUID?>("uuid")
}
final class DefaultsTests: XCTestCase {
@ -103,6 +104,12 @@ final class DefaultsTests: XCTestCase {
XCTAssertEqual(Defaults[.file], fixtureFileURL)
}
func testUUIDType() {
let fixture = UUID()
Defaults[.uuid] = fixture
XCTAssertEqual(Defaults[.uuid], fixture)
}
func testRemoveAll() {
let key = Defaults.Key<Bool>("removeAll", default: false)
let key2 = Defaults.Key<Bool>("removeAll2", default: false)

View File

@ -88,6 +88,7 @@ Add `https://github.com/sindresorhus/Defaults` in the [“Swift Package Manager
- `Date`
- `Data`
- `URL`
- `UUID`
- `NSColor` (macOS)
- `UIColor` (iOS)
- `Color` (SwiftUI)