Support types that conform to both `Codable` and `NSSecureCoding` (#80)
This commit is contained in:
parent
e27b3666d2
commit
04f9b3f45e
|
@ -46,6 +46,13 @@ extension Defaults {
|
||||||
public struct RawRepresentableCodableBridge<Value: RawRepresentable & Codable>: CodableBridge {}
|
public struct RawRepresentableCodableBridge<Value: RawRepresentable & Codable>: CodableBridge {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
This exists to avoid compiler ambiguity.
|
||||||
|
*/
|
||||||
|
extension Defaults {
|
||||||
|
public struct CodableNSSecureCodingBridge<Value: Codable & NSSecureCoding>: CodableBridge {}
|
||||||
|
}
|
||||||
|
|
||||||
extension Defaults {
|
extension Defaults {
|
||||||
public struct URLBridge: CodableBridge {
|
public struct URLBridge: CodableBridge {
|
||||||
public typealias Value = URL
|
public typealias Value = URL
|
||||||
|
|
|
@ -86,6 +86,10 @@ extension Defaults.Serializable where Self: Codable {
|
||||||
public static var bridge: Defaults.TopLevelCodableBridge<Self> { Defaults.TopLevelCodableBridge() }
|
public static var bridge: Defaults.TopLevelCodableBridge<Self> { Defaults.TopLevelCodableBridge() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension Defaults.Serializable where Self: Codable & NSSecureCoding {
|
||||||
|
public static var bridge: Defaults.CodableNSSecureCodingBridge<Self> { Defaults.CodableNSSecureCodingBridge() }
|
||||||
|
}
|
||||||
|
|
||||||
extension Defaults.Serializable where Self: RawRepresentable {
|
extension Defaults.Serializable where Self: RawRepresentable {
|
||||||
public static var bridge: Defaults.RawRepresentableBridge<Self> { Defaults.RawRepresentableBridge() }
|
public static var bridge: Defaults.RawRepresentableBridge<Self> { Defaults.RawRepresentableBridge() }
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,19 @@ private struct Unicorn: Codable, Defaults.Serializable {
|
||||||
|
|
||||||
private let fixtureCodable = Unicorn(isUnicorn: true)
|
private let fixtureCodable = Unicorn(isUnicorn: true)
|
||||||
|
|
||||||
|
@objc(UnicornCodableAndNSSecureCoding)
|
||||||
|
private final class UnicornCodableAndNSSecureCoding: NSObject, NSSecureCoding, Codable, Defaults.Serializable {
|
||||||
|
static let supportsSecureCoding = true
|
||||||
|
|
||||||
|
func encode(with coder: NSCoder) {}
|
||||||
|
|
||||||
|
init?(coder: NSCoder) {}
|
||||||
|
|
||||||
|
override init() {
|
||||||
|
super.init()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
extension Defaults.Keys {
|
extension Defaults.Keys {
|
||||||
fileprivate static let codable = Key<Unicorn>("codable", default: fixtureCodable)
|
fileprivate static let codable = Key<Unicorn>("codable", default: fixtureCodable)
|
||||||
fileprivate static let codableArray = Key<[Unicorn]>("codable", default: [fixtureCodable])
|
fileprivate static let codableArray = Key<[Unicorn]>("codable", default: [fixtureCodable])
|
||||||
|
@ -120,6 +133,11 @@ final class DefaultsCodableTests: XCTestCase {
|
||||||
XCTAssertFalse(Defaults[.codableDictionary]["0"]?.isUnicorn ?? true)
|
XCTAssertFalse(Defaults[.codableDictionary]["0"]?.isUnicorn ?? true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testCodableAndNSSecureCoding() {
|
||||||
|
let fixture = UnicornCodableAndNSSecureCoding()
|
||||||
|
_ = Defaults.Key<UnicornCodableAndNSSecureCoding>("testCodableAndNSSecureCoding", default: fixture)
|
||||||
|
}
|
||||||
|
|
||||||
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
|
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
|
||||||
func testObserveKeyCombine() {
|
func testObserveKeyCombine() {
|
||||||
let key = Defaults.Key<Unicorn>("observeCodableKeyCombine", default: fixtureCodable)
|
let key = Defaults.Key<Unicorn>("observeCodableKeyCombine", default: fixtureCodable)
|
||||||
|
|
|
@ -80,6 +80,7 @@ Add `https://github.com/sindresorhus/Defaults` in the [“Swift Package Manager
|
||||||
- `NSColor` (macOS)
|
- `NSColor` (macOS)
|
||||||
- `UIColor` (iOS)
|
- `UIColor` (iOS)
|
||||||
- `Codable`
|
- `Codable`
|
||||||
|
- `NSSecureCoding`
|
||||||
|
|
||||||
Defaults also support the above types wrapped in `Array`, `Set`, `Dictionary`, and even wrapped in nested types. For example, `[[String: Set<[String: Int]>]]`.
|
Defaults also support the above types wrapped in `Array`, `Set`, `Dictionary`, and even wrapped in nested types. For example, `[[String: Set<[String: Int]>]]`.
|
||||||
|
|
||||||
|
@ -87,6 +88,8 @@ For more types, see the [enum example](#enum-example), [`Codable` example](#coda
|
||||||
|
|
||||||
You can easily add support for any custom type.
|
You can easily add support for any custom type.
|
||||||
|
|
||||||
|
If a type conforms to both `NSSecureCoding` and `Codable`, then `Codable` will be used for the serialization.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
You declare the defaults keys upfront with type and default value.
|
You declare the defaults keys upfront with type and default value.
|
||||||
|
|
Loading…
Reference in New Issue