From c9198bb1d097486711a36269b191f4e96d93130b Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 27 Jan 2022 22:14:21 +0700 Subject: [PATCH] Add support for `UUID` (#92) --- Sources/Defaults/Defaults+Bridge.swift | 19 +++++++++++++++++++ Sources/Defaults/Defaults+Extensions.swift | 3 +++ Tests/DefaultsTests/DefaultsTests.swift | 7 +++++++ readme.md | 1 + 4 files changed, 30 insertions(+) diff --git a/Sources/Defaults/Defaults+Bridge.swift b/Sources/Defaults/Defaults+Bridge.swift index c4e3f29..760bf79 100644 --- a/Sources/Defaults/Defaults+Bridge.swift +++ b/Sources/Defaults/Defaults+Bridge.swift @@ -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 { diff --git a/Sources/Defaults/Defaults+Extensions.swift b/Sources/Defaults/Defaults+Extensions.swift index c2da474..1076fcd 100644 --- a/Sources/Defaults/Defaults+Extensions.swift +++ b/Sources/Defaults/Defaults+Extensions.swift @@ -137,6 +137,9 @@ extension Dictionary: Defaults.Serializable where Key: LosslessStringConvertible public static var bridge: Defaults.DictionaryBridge { 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 { diff --git a/Tests/DefaultsTests/DefaultsTests.swift b/Tests/DefaultsTests/DefaultsTests.swift index c0eee51..8483234 100644 --- a/Tests/DefaultsTests/DefaultsTests.swift +++ b/Tests/DefaultsTests/DefaultsTests.swift @@ -14,6 +14,7 @@ extension Defaults.Keys { static let file = Key("fileURL", default: fixtureFileURL) static let data = Key("data", default: Data([])) static let date = Key("date", default: fixtureDate) + static let uuid = Key("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("removeAll", default: false) let key2 = Defaults.Key("removeAll2", default: false) diff --git a/readme.md b/readme.md index 83dfd14..eb95e82 100644 --- a/readme.md +++ b/readme.md @@ -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)