From 25b4efee9631dd94f0fe396fcf87d95cac94ebed Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 17 Aug 2021 01:05:17 +0200 Subject: [PATCH] Code style fixes --- .../Defaults/Defaults+AnySerializable.swift | 40 +++++++++---------- Sources/Defaults/Defaults.swift | 2 +- .../Migration/v5/Migration+Extensions.swift | 22 +++++++++- Sources/Defaults/Observation.swift | 2 +- .../DefaultsMigrationTests.swift | 13 +++--- .../DefaultsNSSecureCodingTests.swift | 5 +-- .../DefaultsSetAlgebraTests.swift | 9 ++--- 7 files changed, 53 insertions(+), 40 deletions(-) diff --git a/Sources/Defaults/Defaults+AnySerializable.swift b/Sources/Defaults/Defaults+AnySerializable.swift index 5842970..88a15b1 100644 --- a/Sources/Defaults/Defaults+AnySerializable.swift +++ b/Sources/Defaults/Defaults+AnySerializable.swift @@ -57,7 +57,7 @@ extension Defaults { extension Defaults.AnySerializable: Hashable { public func hash(into hasher: inout Hasher) { - switch self.value { + switch value { case let value as Data: return hasher.combine(value) case let value as Date: @@ -105,43 +105,43 @@ extension Defaults.AnySerializable: Hashable { extension Defaults.AnySerializable: Equatable { public static func == (lhs: Self, rhs: Self) -> Bool { switch (lhs.value, rhs.value) { - case let (lhs as Data, rhs as Data): + case (let lhs as Data, let rhs as Data): return lhs == rhs - case let (lhs as Date, rhs as Date): + case (let lhs as Date, let rhs as Date): return lhs == rhs - case let (lhs as Bool, rhs as Bool): + case (let lhs as Bool, let rhs as Bool): return lhs == rhs - case let (lhs as UInt8, rhs as UInt8): + case (let lhs as UInt8, let rhs as UInt8): return lhs == rhs - case let (lhs as Int8, rhs as Int8): + case (let lhs as Int8, let rhs as Int8): return lhs == rhs - case let (lhs as UInt16, rhs as UInt16): + case (let lhs as UInt16, let rhs as UInt16): return lhs == rhs - case let (lhs as Int16, rhs as Int16): + case (let lhs as Int16, let rhs as Int16): return lhs == rhs - case let (lhs as UInt32, rhs as UInt32): + case (let lhs as UInt32, let rhs as UInt32): return lhs == rhs - case let (lhs as Int32, rhs as Int32): + case (let lhs as Int32, let rhs as Int32): return lhs == rhs - case let (lhs as UInt64, rhs as UInt64): + case (let lhs as UInt64, let rhs as UInt64): return lhs == rhs - case let (lhs as Int64, rhs as Int64): + case (let lhs as Int64, let rhs as Int64): return lhs == rhs - case let (lhs as UInt, rhs as UInt): + case (let lhs as UInt, let rhs as UInt): return lhs == rhs - case let (lhs as Int, rhs as Int): + case (let lhs as Int, let rhs as Int): return lhs == rhs - case let (lhs as Float, rhs as Float): + case (let lhs as Float, let rhs as Float): return lhs == rhs - case let (lhs as Double, rhs as Double): + case (let lhs as Double, let rhs as Double): return lhs == rhs - case let (lhs as CGFloat, rhs as CGFloat): + case (let lhs as CGFloat, let rhs as CGFloat): return lhs == rhs - case let (lhs as String, rhs as String): + case (let lhs as String, let rhs as String): return lhs == rhs - case let (lhs as [AnyHashable: Any], rhs as [AnyHashable: Any]): + case (let lhs as [AnyHashable: Any], let rhs as [AnyHashable: Any]): return lhs.toDictionary() == rhs.toDictionary() - case let (lhs as [Any], rhs as [Any]): + case (let lhs as [Any], let rhs as [Any]): return lhs.toSequence() == rhs.toSequence() default: return false diff --git a/Sources/Defaults/Defaults.swift b/Sources/Defaults/Defaults.swift index c88d28f..fde900e 100644 --- a/Sources/Defaults/Defaults.swift +++ b/Sources/Defaults/Defaults.swift @@ -53,7 +53,7 @@ public enum Defaults { } // Sets the default value in the actual UserDefaults, so it can be used in other contexts, like binding. - suite.register(defaults: [self.name: serialized]) + suite.register(defaults: [name: serialized]) } } diff --git a/Sources/Defaults/Migration/v5/Migration+Extensions.swift b/Sources/Defaults/Migration/v5/Migration+Extensions.swift index 47988ab..0b124a9 100644 --- a/Sources/Defaults/Migration/v5/Migration+Extensions.swift +++ b/Sources/Defaults/Migration/v5/Migration+Extensions.swift @@ -9,6 +9,7 @@ extension Defaults { extension Data: Defaults.NativeType { public typealias CodableForm = Self } + extension Data: Defaults.CodableType { public typealias NativeForm = Self @@ -18,6 +19,7 @@ extension Data: Defaults.CodableType { extension Date: Defaults.NativeType { public typealias CodableForm = Self } + extension Date: Defaults.CodableType { public typealias NativeForm = Self @@ -27,6 +29,7 @@ extension Date: Defaults.CodableType { extension Bool: Defaults.NativeType { public typealias CodableForm = Self } + extension Bool: Defaults.CodableType { public typealias NativeForm = Self @@ -36,6 +39,7 @@ extension Bool: Defaults.CodableType { extension Int: Defaults.NativeType { public typealias CodableForm = Self } + extension Int: Defaults.CodableType { public typealias NativeForm = Self @@ -45,6 +49,7 @@ extension Int: Defaults.CodableType { extension UInt: Defaults.NativeType { public typealias CodableForm = Self } + extension UInt: Defaults.CodableType { public typealias NativeForm = Self @@ -54,6 +59,7 @@ extension UInt: Defaults.CodableType { extension Double: Defaults.NativeType { public typealias CodableForm = Self } + extension Double: Defaults.CodableType { public typealias NativeForm = Self @@ -63,6 +69,7 @@ extension Double: Defaults.CodableType { extension Float: Defaults.NativeType { public typealias CodableForm = Self } + extension Float: Defaults.CodableType { public typealias NativeForm = Self @@ -72,6 +79,7 @@ extension Float: Defaults.CodableType { extension String: Defaults.NativeType { public typealias CodableForm = Self } + extension String: Defaults.CodableType { public typealias NativeForm = Self @@ -81,6 +89,7 @@ extension String: Defaults.CodableType { extension CGFloat: Defaults.NativeType { public typealias CodableForm = Self } + extension CGFloat: Defaults.CodableType { public typealias NativeForm = Self @@ -90,6 +99,7 @@ extension CGFloat: Defaults.CodableType { extension Int8: Defaults.NativeType { public typealias CodableForm = Self } + extension Int8: Defaults.CodableType { public typealias NativeForm = Self @@ -99,6 +109,7 @@ extension Int8: Defaults.CodableType { extension UInt8: Defaults.NativeType { public typealias CodableForm = Self } + extension UInt8: Defaults.CodableType { public typealias NativeForm = Self @@ -108,6 +119,7 @@ extension UInt8: Defaults.CodableType { extension Int16: Defaults.NativeType { public typealias CodableForm = Self } + extension Int16: Defaults.CodableType { public typealias NativeForm = Self @@ -117,6 +129,7 @@ extension Int16: Defaults.CodableType { extension UInt16: Defaults.NativeType { public typealias CodableForm = Self } + extension UInt16: Defaults.CodableType { public typealias NativeForm = Self @@ -126,6 +139,7 @@ extension UInt16: Defaults.CodableType { extension Int32: Defaults.NativeType { public typealias CodableForm = Self } + extension Int32: Defaults.CodableType { public typealias NativeForm = Self @@ -135,6 +149,7 @@ extension Int32: Defaults.CodableType { extension UInt32: Defaults.NativeType { public typealias CodableForm = Self } + extension UInt32: Defaults.CodableType { public typealias NativeForm = Self @@ -144,6 +159,7 @@ extension UInt32: Defaults.CodableType { extension Int64: Defaults.NativeType { public typealias CodableForm = Self } + extension Int64: Defaults.CodableType { public typealias NativeForm = Self @@ -153,6 +169,7 @@ extension Int64: Defaults.CodableType { extension UInt64: Defaults.NativeType { public typealias CodableForm = Self } + extension UInt64: Defaults.CodableType { public typealias NativeForm = Self @@ -162,6 +179,7 @@ extension UInt64: Defaults.CodableType { extension URL: Defaults.NativeType { public typealias CodableForm = Self } + extension URL: Defaults.CodableType { public typealias NativeForm = Self @@ -182,7 +200,7 @@ extension Defaults.SetAlgebraSerializable where Self: Defaults.NativeType, Eleme extension Defaults.CodableType where Self: RawRepresentable, NativeForm: RawRepresentable, RawValue == NativeForm.RawValue { public func toNative() -> NativeForm { - NativeForm(rawValue: self.rawValue)! + NativeForm(rawValue: rawValue)! } } @@ -193,6 +211,7 @@ extension Set: Defaults.NativeType where Element: Defaults.NativeType { extension Array: Defaults.NativeType where Element: Defaults.NativeType { public typealias CodableForm = [Element.CodableForm] } + extension Array: Defaults.CodableType where Element: Defaults.CodableType { public typealias NativeForm = [Element.NativeForm] @@ -204,6 +223,7 @@ extension Array: Defaults.CodableType where Element: Defaults.CodableType { extension Dictionary: Defaults.NativeType where Key: LosslessStringConvertible & Hashable, Value: Defaults.NativeType { public typealias CodableForm = [String: Value.CodableForm] } + extension Dictionary: Defaults.CodableType where Key == String, Value: Defaults.CodableType { public typealias NativeForm = [String: Value.NativeForm] diff --git a/Sources/Defaults/Observation.swift b/Sources/Defaults/Observation.swift index 88e9a17..ac68287 100644 --- a/Sources/Defaults/Observation.swift +++ b/Sources/Defaults/Observation.swift @@ -350,4 +350,4 @@ extension Defaults.ObservationOptions { } } -extension Defaults.KeyChange: Equatable where Value: Equatable { } +extension Defaults.KeyChange: Equatable where Value: Equatable {} diff --git a/Tests/DefaultsTests/DefaultsMigrationTests.swift b/Tests/DefaultsTests/DefaultsMigrationTests.swift index 508db9c..60d678c 100644 --- a/Tests/DefaultsTests/DefaultsMigrationTests.swift +++ b/Tests/DefaultsTests/DefaultsMigrationTests.swift @@ -159,23 +159,20 @@ extension SetForm { Self(store.union(other.store)) } - func intersection(_ other: Self) - -> Self { + func intersection(_ other: Self) -> Self { var setForm = Self() setForm.store = store.intersection(other.store) return setForm } - func symmetricDifference(_ other: Self) - -> Self { + func symmetricDifference(_ other: Self) -> Self { var setForm = Self() setForm.store = store.symmetricDifference(other.store) return setForm } @discardableResult - mutating func insert(_ newMember: Element) - -> (inserted: Bool, memberAfterInsert: Element) { + mutating func insert(_ newMember: Element) -> (inserted: Bool, memberAfterInsert: Element) { store.insert(newMember) } @@ -208,7 +205,7 @@ private struct MySet: SetForm, Defaults var store: Set init() { - store = [] + self.store = [] } init(_ elements: [Element]) { @@ -220,7 +217,7 @@ private struct CodableSet: var store: Set init() { - store = [] + self.store = [] } init(_ elements: [Element]) { diff --git a/Tests/DefaultsTests/DefaultsNSSecureCodingTests.swift b/Tests/DefaultsTests/DefaultsNSSecureCodingTests.swift index b10f2fb..2245c98 100644 --- a/Tests/DefaultsTests/DefaultsNSSecureCodingTests.swift +++ b/Tests/DefaultsTests/DefaultsNSSecureCodingTests.swift @@ -25,8 +25,7 @@ private final class ExamplePersistentHistory: NSPersistentHistoryToken, Defaults } // NSSecureCoding -@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) -private let persistentHistoryValue = ExamplePersistentHistory(value: "ExampleToken") +@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) private let persistentHistoryValue = ExamplePersistentHistory(value: "ExampleToken") @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) extension Defaults.Keys { @@ -440,7 +439,7 @@ final class DefaultsNSSecureCodingTests: XCTestCase { var observation: Defaults.Observation! observation = Defaults.observe(key, options: []) { change in XCTAssertEqual(change.oldValue[0].value, persistentHistoryValue.value) - XCTAssertEqual(change.newValue.map { $0.value }, [persistentHistoryValue, newPersistentHistory].map { $0.value }) + XCTAssertEqual(change.newValue.map(\.value), [persistentHistoryValue, newPersistentHistory].map(\.value)) observation.invalidate() expect.fulfill() } diff --git a/Tests/DefaultsTests/DefaultsSetAlgebraTests.swift b/Tests/DefaultsTests/DefaultsSetAlgebraTests.swift index 5b72ca8..f4b62ea 100644 --- a/Tests/DefaultsTests/DefaultsSetAlgebraTests.swift +++ b/Tests/DefaultsTests/DefaultsSetAlgebraTests.swift @@ -23,23 +23,20 @@ struct DefaultsSetAlgebra: SetAlgebra DefaultsSetAlgebra(store.union(other.store)) } - func intersection(_ other: DefaultsSetAlgebra) - -> DefaultsSetAlgebra { + func intersection(_ other: DefaultsSetAlgebra) -> DefaultsSetAlgebra { var defaultsSetAlgebra = DefaultsSetAlgebra() defaultsSetAlgebra.store = store.intersection(other.store) return defaultsSetAlgebra } - func symmetricDifference(_ other: DefaultsSetAlgebra) - -> DefaultsSetAlgebra { + func symmetricDifference(_ other: DefaultsSetAlgebra) -> DefaultsSetAlgebra { var defaultedSetAlgebra = DefaultsSetAlgebra() defaultedSetAlgebra.store = store.symmetricDifference(other.store) return defaultedSetAlgebra } @discardableResult - mutating func insert(_ newMember: Element) - -> (inserted: Bool, memberAfterInsert: Element) { + mutating func insert(_ newMember: Element) -> (inserted: Bool, memberAfterInsert: Element) { store.insert(newMember) }