Skip to content

Commit

Permalink
Add support for UUID (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus authored Jan 27, 2022
1 parent 8c0d80e commit c9198bb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Sources/Defaults/Defaults+Bridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions Sources/Defaults/Defaults+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions Tests/DefaultsTests/DefaultsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c9198bb

Please sign in to comment.