This commit is contained in:
Sindre Sorhus 2023-09-15 02:36:02 +07:00
parent 3efef5a28e
commit 55ec93004e
8 changed files with 37 additions and 35 deletions

View File

@ -7,7 +7,7 @@ jobs:
runs-on: macos-13 runs-on: macos-13
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- run: sudo xcode-select -switch /Applications/Xcode_14.3.app - run: sudo xcode-select -switch /Applications/Xcode_15.0.app
- run: swift test - run: swift test
lint: lint:
runs-on: ubuntu-latest runs-on: ubuntu-latest

View File

@ -1,4 +1,4 @@
// swift-tools-version:5.8 // swift-tools-version:5.9
import PackageDescription import PackageDescription
let package = Package( let package = Package(

View File

@ -106,45 +106,45 @@ extension Defaults.AnySerializable: Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool { public static func == (lhs: Self, rhs: Self) -> Bool {
switch (lhs.value, rhs.value) { switch (lhs.value, rhs.value) {
case (let lhs as Data, let rhs as Data): case (let lhs as Data, let rhs as Data):
return lhs == rhs lhs == rhs
case (let lhs as Date, let rhs as Date): case (let lhs as Date, let rhs as Date):
return lhs == rhs lhs == rhs
case (let lhs as Bool, let rhs as Bool): case (let lhs as Bool, let rhs as Bool):
return lhs == rhs lhs == rhs
case (let lhs as UInt8, let rhs as UInt8): case (let lhs as UInt8, let rhs as UInt8):
return lhs == rhs lhs == rhs
case (let lhs as Int8, let rhs as Int8): case (let lhs as Int8, let rhs as Int8):
return lhs == rhs lhs == rhs
case (let lhs as UInt16, let rhs as UInt16): case (let lhs as UInt16, let rhs as UInt16):
return lhs == rhs lhs == rhs
case (let lhs as Int16, let rhs as Int16): case (let lhs as Int16, let rhs as Int16):
return lhs == rhs lhs == rhs
case (let lhs as UInt32, let rhs as UInt32): case (let lhs as UInt32, let rhs as UInt32):
return lhs == rhs lhs == rhs
case (let lhs as Int32, let rhs as Int32): case (let lhs as Int32, let rhs as Int32):
return lhs == rhs lhs == rhs
case (let lhs as UInt64, let rhs as UInt64): case (let lhs as UInt64, let rhs as UInt64):
return lhs == rhs lhs == rhs
case (let lhs as Int64, let rhs as Int64): case (let lhs as Int64, let rhs as Int64):
return lhs == rhs lhs == rhs
case (let lhs as UInt, let rhs as UInt): case (let lhs as UInt, let rhs as UInt):
return lhs == rhs lhs == rhs
case (let lhs as Int, let rhs as Int): case (let lhs as Int, let rhs as Int):
return lhs == rhs lhs == rhs
case (let lhs as Float, let rhs as Float): case (let lhs as Float, let rhs as Float):
return lhs == rhs lhs == rhs
case (let lhs as Double, let rhs as Double): case (let lhs as Double, let rhs as Double):
return lhs == rhs lhs == rhs
case (let lhs as CGFloat, let rhs as CGFloat): case (let lhs as CGFloat, let rhs as CGFloat):
return lhs == rhs lhs == rhs
case (let lhs as String, let rhs as String): case (let lhs as String, let rhs as String):
return lhs == rhs lhs == rhs
case (let lhs as [AnyHashable: Any], let rhs as [AnyHashable: Any]): case (let lhs as [AnyHashable: Any], let rhs as [AnyHashable: Any]):
return lhs.toDictionary() == rhs.toDictionary() lhs.toDictionary() == rhs.toDictionary()
case (let lhs as [Any], let rhs as [Any]): case (let lhs as [Any], let rhs as [Any]):
return lhs.toSequence() == rhs.toSequence() lhs.toSequence() == rhs.toSequence()
default: default:
return false false
} }
} }
} }
@ -193,7 +193,7 @@ extension Defaults.AnySerializable: ExpressibleByDictionaryLiteral {
extension Defaults.AnySerializable: _DefaultsOptionalProtocol { extension Defaults.AnySerializable: _DefaultsOptionalProtocol {
// Since `nil` cannot be assigned to `Any`, we use `Void` instead of `nil`. // Since `nil` cannot be assigned to `Any`, we use `Void` instead of `nil`.
public var isNil: Bool { value is Void } public var _defaults_isNil: Bool { value is Void }
} }
extension Sequence { extension Sequence {

View File

@ -15,7 +15,9 @@ extension Defaults.CodableBridge {
// Some codable values like URL and enum are encoded as a top-level // Some codable values like URL and enum are encoded as a top-level
// string which JSON can't handle, so we need to wrap it in an array // string which JSON can't handle, so we need to wrap it in an array
// We need this: https://forums.swift.org/t/allowing-top-level-fragments-in-jsondecoder/11750 // We need this: https://forums.swift.org/t/allowing-top-level-fragments-in-jsondecoder/11750
let data = try JSONEncoder().encode([value]) let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = .sortedKeys
let data = try jsonEncoder.encode([value])
return String(String(data: data, encoding: .utf8)!.dropFirst().dropLast()) return String(String(data: data, encoding: .utf8)!.dropFirst().dropLast())
} catch { } catch {
print(error) print(error)

View File

@ -95,7 +95,7 @@ extension Defaults {
- A `bridge` cannot deserialize `Value` from `UserDefaults` - A `bridge` cannot deserialize `Value` from `UserDefaults`
*/ */
@usableFromInline @usableFromInline
internal let defaultValueGetter: () -> Value let defaultValueGetter: () -> Value
public var defaultValue: Value { defaultValueGetter() } public var defaultValue: Value { defaultValueGetter() }
@ -116,7 +116,7 @@ extension Defaults {
super.init(name: name, suite: suite) super.init(name: name, suite: suite)
if (defaultValue as? _DefaultsOptionalProtocol)?.isNil == true { if (defaultValue as? _DefaultsOptionalProtocol)?._defaults_isNil == true {
return return
} }

View File

@ -10,7 +10,7 @@ extension UserDefaults {
} }
func _set<Value: Defaults.Serializable>(_ key: String, to value: Value) { func _set<Value: Defaults.Serializable>(_ key: String, to value: Value) {
if (value as? _DefaultsOptionalProtocol)?.isNil == true { if (value as? _DefaultsOptionalProtocol)?._defaults_isNil == true {
removeObject(forKey: key) removeObject(forKey: key)
return return
} }

View File

@ -132,11 +132,11 @@ public protocol _DefaultsOptionalProtocol: ExpressibleByNilLiteral {
/** /**
This is useful as you cannot compare `_OptionalType` to `nil`. This is useful as you cannot compare `_OptionalType` to `nil`.
*/ */
var isNil: Bool { get } var _defaults_isNil: Bool { get }
} }
extension Optional: _DefaultsOptionalProtocol { extension Optional: _DefaultsOptionalProtocol {
public var isNil: Bool { self == nil } public var _defaults_isNil: Bool { self == nil }
} }
@ -166,7 +166,7 @@ extension Collection {
extension Defaults { extension Defaults {
@usableFromInline @usableFromInline
internal static func isValidKeyPath(name: String) -> Bool { static func isValidKeyPath(name: String) -> Bool {
// The key must be ASCII, not start with @, and cannot contain a dot. // The key must be ASCII, not start with @, and cannot contain a dot.
!name.starts(with: "@") && name.allSatisfy { $0 != "." && $0.isASCII } !name.starts(with: "@") && name.allSatisfy { $0 != "." && $0.isASCII }
} }
@ -215,7 +215,7 @@ extension Defaults.Serializable {
``` ```
*/ */
@usableFromInline @usableFromInline
internal static func toSerializable<T: Defaults.Serializable>(_ value: T) -> Any? { static func toSerializable<T: Defaults.Serializable>(_ value: T) -> Any? {
if T.isNativelySupportedType { if T.isNativelySupportedType {
return value return value
} }
@ -240,7 +240,7 @@ Get SwiftUI dynamic shared object.
Reference: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dyld.3.html Reference: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dyld.3.html
*/ */
@usableFromInline @usableFromInline
internal let dynamicSharedObject: UnsafeMutableRawPointer = { let dynamicSharedObject: UnsafeMutableRawPointer = {
let imageCount = _dyld_image_count() let imageCount = _dyld_image_count()
for imageIndex in 0..<imageCount { for imageIndex in 0..<imageCount {
guard guard
@ -261,7 +261,7 @@ internal let dynamicSharedObject: UnsafeMutableRawPointer = {
@_transparent @_transparent
@usableFromInline @usableFromInline
internal func runtimeWarn( func runtimeWarn(
_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String _ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String
) { ) {
#if DEBUG #if DEBUG

View File

@ -205,7 +205,7 @@ final class DefaultsCustomBridge: XCTestCase {
XCTFail("rawValue should not be nil") XCTFail("rawValue should not be nil")
return return
} }
XCTAssertEqual(rawValue, [#"{"minute":0,"hour":1}"#, #"{"minute":0,"hour":2}"#]) XCTAssertEqual(rawValue, [#"{"hour":1,"minute":0}"#, #"{"hour":2,"minute":0}"#])
let next_start = PlainHourMinuteTime(hour: 3, minute: 58) let next_start = PlainHourMinuteTime(hour: 3, minute: 58)
let next_end = PlainHourMinuteTime(hour: 4, minute: 59) let next_end = PlainHourMinuteTime(hour: 4, minute: 59)
let next_range = PlainHourMinuteTimeRange(start: next_start, end: next_end) let next_range = PlainHourMinuteTimeRange(start: next_start, end: next_end)
@ -218,7 +218,7 @@ final class DefaultsCustomBridge: XCTestCase {
XCTFail("nextRawValue should not be nil") XCTFail("nextRawValue should not be nil")
return return
} }
XCTAssertEqual(nextRawValue, [#"{"minute":58,"hour":3}"#, #"{"minute":59,"hour":4}"#]) XCTAssertEqual(nextRawValue, [#"{"hour":3,"minute":58}"#, #"{"hour":4,"minute":59}"#])
} }
func testType() { func testType() {