mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
NSKeyed{Una,A}rchiver should support Codable
This commit is contained in:
@@ -51,8 +51,7 @@ internal extension DecodingError {
|
||||
} else if value is [String : Any] {
|
||||
return "a dictionary"
|
||||
} else {
|
||||
// This should never happen -- we somehow have a non-JSON type here.
|
||||
preconditionFailure("Invalid storage type \(type(of: value)).")
|
||||
return "\(type(of: value))"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,6 +129,20 @@ extension NSCoder {
|
||||
}
|
||||
}
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// NSKeyedArchiver
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension NSKeyedArchiver {
|
||||
@nonobjc
|
||||
@available(OSX 10.11, iOS 9.0, *)
|
||||
public func encodeEncodable<T : Encodable>(_ value: T, forKey key: String) throws {
|
||||
let plistEncoder = PropertyListEncoder()
|
||||
let plist = try plistEncoder.encodeToTopLevelContainer(value)
|
||||
self.encode(plist, forKey: key)
|
||||
}
|
||||
}
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// NSKeyedUnarchiver
|
||||
//===----------------------------------------------------------------------===//
|
||||
@@ -153,6 +167,48 @@ extension NSKeyedUnarchiver {
|
||||
try resolveError(error)
|
||||
return result
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
private static let __plistClasses: [AnyClass] = [
|
||||
NSArray.self,
|
||||
NSData.self,
|
||||
NSDate.self,
|
||||
NSDictionary.self,
|
||||
NSNumber.self,
|
||||
NSString.self
|
||||
]
|
||||
|
||||
@nonobjc
|
||||
@available(OSX 10.11, iOS 9.0, *)
|
||||
public func decodeDecodable<T : Decodable>(_ type: T.Type, forKey key: String) -> T? {
|
||||
guard let value = self.decodeObject(of: NSKeyedUnarchiver.__plistClasses, forKey: key) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let plistDecoder = PropertyListDecoder()
|
||||
do {
|
||||
return try plistDecoder.decode(T.self, fromTopLevel: value)
|
||||
} catch {
|
||||
self.failWithError(error)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@nonobjc
|
||||
@available(OSX 10.11, iOS 9.0, *)
|
||||
public func decodeTopLevelDecodable<T : Decodable>(_ type: T.Type, forKey key: String) throws -> T? {
|
||||
guard let value = try self.decodeTopLevelObject(of: NSKeyedUnarchiver.__plistClasses, forKey: key) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let plistDecoder = PropertyListDecoder()
|
||||
do {
|
||||
return try plistDecoder.decode(T.self, fromTopLevel: value)
|
||||
} catch {
|
||||
self.failWithError(error)
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -50,16 +50,7 @@ open class PropertyListEncoder {
|
||||
/// - throws: `EncodingError.invalidValue` if a non-conforming floating-point value is encountered during encoding, and the encoding strategy is `.throw`.
|
||||
/// - throws: An error if any value throws an error during encoding.
|
||||
open func encode<Value : Encodable>(_ value: Value) throws -> Data {
|
||||
let encoder = _PlistEncoder(options: self.options)
|
||||
try value.encode(to: encoder)
|
||||
|
||||
guard encoder.storage.count > 0 else {
|
||||
throw EncodingError.invalidValue(value,
|
||||
EncodingError.Context(codingPath: [],
|
||||
debugDescription: "Top-level \(Value.self) did not encode any values."))
|
||||
}
|
||||
|
||||
let topLevel = encoder.storage.popContainer()
|
||||
let topLevel = try encodeToTopLevelContainer(value)
|
||||
if topLevel is NSNumber {
|
||||
throw EncodingError.invalidValue(value,
|
||||
EncodingError.Context(codingPath: [],
|
||||
@@ -80,6 +71,26 @@ open class PropertyListEncoder {
|
||||
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Unable to encode the given top-level value as a property list", underlyingError: error))
|
||||
}
|
||||
}
|
||||
|
||||
/// Encodes the given top-level value and returns its plist-type representation.
|
||||
///
|
||||
/// - parameter value: The value to encode.
|
||||
/// - returns: A new top-level array or dictionary representing the value.
|
||||
/// - throws: `EncodingError.invalidValue` if a non-conforming floating-point value is encountered during encoding, and the encoding strategy is `.throw`.
|
||||
/// - throws: An error if any value throws an error during encoding.
|
||||
internal func encodeToTopLevelContainer<Value : Encodable>(_ value: Value) throws -> Any {
|
||||
let encoder = _PlistEncoder(options: self.options)
|
||||
try value.encode(to: encoder)
|
||||
|
||||
guard encoder.storage.count > 0 else {
|
||||
throw EncodingError.invalidValue(value,
|
||||
EncodingError.Context(codingPath: [],
|
||||
debugDescription: "Top-level \(Value.self) did not encode any values."))
|
||||
}
|
||||
|
||||
let topLevel = encoder.storage.popContainer()
|
||||
return topLevel
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - _PlistEncoder
|
||||
@@ -620,7 +631,19 @@ open class PropertyListDecoder {
|
||||
} catch {
|
||||
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "The given data was not a valid property list.", underlyingError: error))
|
||||
}
|
||||
let decoder = _PlistDecoder(referencing: topLevel, options: self.options)
|
||||
|
||||
return try decode(T.self, fromTopLevel: topLevel)
|
||||
}
|
||||
|
||||
/// Decodes a top-level value of the given type from the given property list container (top-level array or dictionary).
|
||||
///
|
||||
/// - parameter type: The type of the value to decode.
|
||||
/// - parameter container: The top-level plist container.
|
||||
/// - returns: A value of the requested type.
|
||||
/// - throws: `DecodingError.dataCorrupted` if values requested from the payload are corrupted, or if the given data is not a valid property list.
|
||||
/// - throws: An error if any value throws an error during decoding.
|
||||
internal func decode<T : Decodable>(_ type: T.Type, fromTopLevel container: Any) throws -> T {
|
||||
let decoder = _PlistDecoder(referencing: container, options: self.options)
|
||||
return try T(from: decoder)
|
||||
}
|
||||
}
|
||||
|
||||
183
test/stdlib/NSKeyedArchival.swift
Normal file
183
test/stdlib/NSKeyedArchival.swift
Normal file
@@ -0,0 +1,183 @@
|
||||
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// RUN: %target-run-simple-swift
|
||||
// REQUIRES: executable_test
|
||||
// REQUIRES: objc_interop
|
||||
|
||||
import Foundation
|
||||
import StdlibUnittest
|
||||
|
||||
// Since we test various apis with and without errors define these conveniences
|
||||
func expectError(_ verb: () throws -> ()) {
|
||||
do {
|
||||
try verb()
|
||||
expectUnreachable("Expected to throw error!")
|
||||
} catch {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
func expectNoError(_ verb: () throws -> ()) {
|
||||
do {
|
||||
try verb()
|
||||
} catch let error {
|
||||
expectUnreachable("Did not expect to throw error! \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
// Utilities for minimizing boiler plate in the tests
|
||||
@available(OSX 10.11, iOS 9.0, *)
|
||||
func archive(_ archival: (_: NSKeyedArchiver) throws -> Void) rethrows -> Data {
|
||||
let data = NSMutableData()
|
||||
let ka = NSKeyedArchiver(forWritingWith: data)
|
||||
do {
|
||||
defer { ka.finishEncoding() }
|
||||
try archival(ka)
|
||||
}
|
||||
expectNotEqual(data.length, 0)
|
||||
return data as Data
|
||||
}
|
||||
|
||||
@available(OSX 10.11, iOS 9.0, *)
|
||||
func unarchive(_ data: Data, _ unarchival: (_: NSKeyedUnarchiver) throws -> Void) rethrows -> Void {
|
||||
let ku = NSKeyedUnarchiver(forReadingWith: data as Data)
|
||||
ku.requiresSecureCoding = true
|
||||
ku.decodingFailurePolicy = .setErrorAndReturn
|
||||
try unarchival(ku)
|
||||
ku.finishDecoding()
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Data
|
||||
// simple struct comprised of entirely plist types
|
||||
struct Simple : Codable, Equatable {
|
||||
var number: Int = 5
|
||||
var string: String = "hello"
|
||||
var data: Data = Data(bytes: [0, 1, 2, 3])
|
||||
var array: [String] = ["stay", "a while", "and listen"]
|
||||
var dictionary: [String:String] = ["Deckard": "Cain"]
|
||||
static func ==(lhs: Simple, rhs: Simple) -> Bool {
|
||||
return lhs.number == rhs.number && lhs.string == rhs.string && lhs.data == rhs.data && lhs.array == rhs.array && lhs.dictionary == rhs.dictionary
|
||||
}
|
||||
}
|
||||
|
||||
// simple struct comprised of entirely plist types
|
||||
struct ThrowingCodable : Codable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
throw NSError(domain: "sad", code: 3)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK - Tests
|
||||
@available(OSX 10.11, iOS 9.0, *)
|
||||
func test_simpleCodableSupport() {
|
||||
let s = Simple()
|
||||
|
||||
var data: Data? = nil
|
||||
expectNoError {
|
||||
data = try archive { archiver in
|
||||
try archiver.encodeEncodable(s, forKey: "valid")
|
||||
try archiver.encodeEncodable(s, forKey: "wrong-type")
|
||||
}
|
||||
}
|
||||
|
||||
unarchive(data!) { unarchiver in
|
||||
// confirm we can roundtrip our data
|
||||
let roundtrip = unarchiver.decodeDecodable(Simple.self, forKey: "valid")
|
||||
expectNotNil(roundtrip)
|
||||
if let rt = roundtrip {
|
||||
expectEqual(rt, s)
|
||||
}
|
||||
|
||||
// also ask for something that is not there
|
||||
let notThere = unarchiver.decodeDecodable(Simple.self, forKey: "not-there")
|
||||
expectNil(notThere)
|
||||
expectNil(unarchiver.error)
|
||||
|
||||
// String != Simple so this should fail at the type level
|
||||
let wrongType = unarchiver.decodeDecodable(String.self, forKey: "wrong-type")
|
||||
expectNil(wrongType)
|
||||
expectNotNil(unarchiver.error)
|
||||
}
|
||||
}
|
||||
|
||||
@available(OSX 10.11, iOS 9.0, *)
|
||||
func test_encodableErrorHandling() {
|
||||
expectError {
|
||||
_ = try archive { archiver in
|
||||
try archiver.encodeEncodable(ThrowingCodable(), forKey: "non-codable")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(OSX 10.11, iOS 9.0, *)
|
||||
func test_readingNonCodableFromDecodeDecodable() {
|
||||
var data: Data? = nil
|
||||
expectNoError {
|
||||
data = archive { archiver in
|
||||
archiver.encode(NSDate(), forKey: "non-codable")
|
||||
}
|
||||
}
|
||||
|
||||
unarchive(data!) { unarchiver in
|
||||
let nonCodable = unarchiver.decodeDecodable(Simple.self, forKey: "non-codable")
|
||||
expectNil(nonCodable)
|
||||
expectNotNil(unarchiver.error)
|
||||
}
|
||||
}
|
||||
|
||||
@available(OSX 10.11, iOS 9.0, *)
|
||||
func test_toplevelAPIVariants() {
|
||||
let s = Simple()
|
||||
var data: Data? = nil
|
||||
expectNoError {
|
||||
data = try archive { archiver in
|
||||
try archiver.encodeEncodable(s, forKey: "valid")
|
||||
try archiver.encodeEncodable(Date(), forKey: "non-codable")
|
||||
}
|
||||
}
|
||||
|
||||
unarchive(data!) { unarchiver in
|
||||
var caught = false
|
||||
do {
|
||||
_ = try unarchiver.decodeTopLevelDecodable(Simple.self, forKey: "non-codable")
|
||||
} catch {
|
||||
caught = true
|
||||
}
|
||||
expectTrue(caught)
|
||||
}
|
||||
|
||||
expectNoError {
|
||||
_ = try unarchive(data!) { unarchiver in
|
||||
let roundtrip = try unarchiver.decodeTopLevelDecodable(Simple.self, forKey: "valid")
|
||||
expectNotNil(roundtrip)
|
||||
if let rt = roundtrip {
|
||||
expectEqual(rt, s)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Run Tests
|
||||
|
||||
#if !FOUNDATION_XCTEST
|
||||
if #available(OSX 10.11, iOS 9.0, *) {
|
||||
let NSKeyedArchiverTest = TestSuite("TestNSKeyedArchiver")
|
||||
let tests = [
|
||||
"NSKeyedArchival.simpleCodableSupportInNSKeyedArchival": test_simpleCodableSupport,
|
||||
"NSKeyedArchival.encodableErrorHandling": test_encodableErrorHandling,
|
||||
"NSKeyedArchival.readingNonCodableFromDecodeDecodable": test_readingNonCodableFromDecodeDecodable,
|
||||
"NSKeyedArchival.toplevelAPIVariants": test_toplevelAPIVariants
|
||||
]
|
||||
|
||||
for (name, test) in tests {
|
||||
NSKeyedArchiverTest.test(name) { test() }
|
||||
}
|
||||
runAllTests()
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user