Fix support for non-string keys in dictionaries (#149)

This commit is contained in:
Sindre Sorhus 2023-09-14 20:42:48 +07:00 committed by GitHub
parent 11b6adbfcb
commit 3efef5a28e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -132,7 +132,7 @@ extension Array: Defaults.Serializable where Element: Defaults.Serializable {
} }
extension Dictionary: Defaults.Serializable where Key: LosslessStringConvertible & Hashable, Value: Defaults.Serializable { extension Dictionary: Defaults.Serializable where Key: LosslessStringConvertible & Hashable, Value: Defaults.Serializable {
public static var isNativelySupportedType: Bool { Value.isNativelySupportedType } public static var isNativelySupportedType: Bool { (Key.self is String.Type) && Value.isNativelySupportedType }
public static var bridge: Defaults.DictionaryBridge<Key, Value> { Defaults.DictionaryBridge() } public static var bridge: Defaults.DictionaryBridge<Key, Value> { Defaults.DictionaryBridge() }
} }

View File

@ -57,6 +57,15 @@ final class DefaultsDictionaryTests: XCTestCase {
XCTAssertEqual(Defaults[key]["0"], [newName, fixtureArray[1]]) XCTAssertEqual(Defaults[key]["0"], [newName, fixtureArray[1]])
} }
func testIntKey() {
let fixture = [1: "x"]
let key = Defaults.Key<[Int: String]>("independentDictionaryIntKey", default: fixture)
XCTAssertEqual(Defaults[key][1], fixture[1])
let newValue = "John"
Defaults[key][1] = newValue
XCTAssertEqual(Defaults[key][1], newValue)
}
func testType() { func testType() {
XCTAssertEqual(Defaults[.dictionary]["0"], fixtureDictionary["0"]) XCTAssertEqual(Defaults[.dictionary]["0"], fixtureDictionary["0"])
let newName = "Chen" let newName = "Chen"