parent
55ec93004e
commit
d8a9f51056
|
@ -12,13 +12,10 @@ extension Defaults.CodableBridge {
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
// 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
|
|
||||||
// We need this: https://forums.swift.org/t/allowing-top-level-fragments-in-jsondecoder/11750
|
|
||||||
let jsonEncoder = JSONEncoder()
|
let jsonEncoder = JSONEncoder()
|
||||||
jsonEncoder.outputFormatting = .sortedKeys
|
jsonEncoder.outputFormatting = .sortedKeys
|
||||||
let data = try jsonEncoder.encode([value])
|
let data = try jsonEncoder.encode(value)
|
||||||
return String(String(data: data, encoding: .utf8)!.dropFirst().dropLast())
|
return String(data: data, encoding: .utf8)
|
||||||
} catch {
|
} catch {
|
||||||
print(error)
|
print(error)
|
||||||
return nil
|
return nil
|
||||||
|
@ -30,7 +27,7 @@ extension Defaults.CodableBridge {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return [Value].init(jsonString: "[\(object)]")?.first
|
return Value(jsonString: object)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -128,6 +128,19 @@ final class DefaultsCodableTests: XCTestCase {
|
||||||
XCTAssertFalse(Defaults[key]["0"]?[1].isUnicorn ?? true)
|
XCTAssertFalse(Defaults[key]["0"]?[1].isUnicorn ?? true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testCodableAndRawRepresentable() {
|
||||||
|
struct Unicorn: Codable, RawRepresentable, Defaults.Serializable {
|
||||||
|
var rawValue: String
|
||||||
|
}
|
||||||
|
|
||||||
|
let fixture = Unicorn(rawValue: "x")
|
||||||
|
|
||||||
|
let key = Defaults.Key<Unicorn?>("independentKey_codableAndRawRepresentable")
|
||||||
|
Defaults[key] = fixture
|
||||||
|
XCTAssertEqual(Defaults[key]?.rawValue, fixture.rawValue)
|
||||||
|
XCTAssertEqual(UserDefaults.standard.string(forKey: key.name), #""\#(fixture.rawValue)""#)
|
||||||
|
}
|
||||||
|
|
||||||
func testType() {
|
func testType() {
|
||||||
XCTAssertTrue(Defaults[.codable].isUnicorn)
|
XCTAssertTrue(Defaults[.codable].isUnicorn)
|
||||||
Defaults[.codable] = Unicorn(isUnicorn: false)
|
Defaults[.codable] = Unicorn(isUnicorn: false)
|
||||||
|
|
Loading…
Reference in New Issue