Skip to content

Commit

Permalink
Merge pull request swiftlang#1087 from itaiferber/codable-encode-deco…
Browse files Browse the repository at this point in the history
…de-nil-changes

Optionality updates to Codable API
  • Loading branch information
itaiferber authored Jun 29, 2017
2 parents 26e3b89 + 25888e4 commit 5b07ca5
Show file tree
Hide file tree
Showing 2 changed files with 574 additions and 526 deletions.
58 changes: 0 additions & 58 deletions Foundation/Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,6 @@
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//

//===----------------------------------------------------------------------===//
// Errors
//===----------------------------------------------------------------------===//

// Adding the following extensions to EncodingError and DecodingError allows them to bridge to NSErrors implicitly.

fileprivate let NSCodingPathErrorKey = "NSCodingPath"
fileprivate let NSDebugDescriptionErrorKey = "NSDebugDescription"

extension EncodingError : CustomNSError {
public static var errorDomain: String = NSCocoaErrorDomain

public var errorCode: Int {
switch self {
case .invalidValue(_, _): return CocoaError.coderInvalidValue.rawValue
}
}

public var errorUserInfo: [String : Any] {
let context: Context
switch self {
case .invalidValue(_, let c): context = c
}

return [NSCodingPathErrorKey: context.codingPath,
NSDebugDescriptionErrorKey: context.debugDescription]
}
}

extension DecodingError : CustomNSError {
public static var errorDomain: String = NSCocoaErrorDomain

public var errorCode: Int {
switch self {
case .valueNotFound(_, _): fallthrough
case .keyNotFound(_, _):
return CocoaError.coderValueNotFound.rawValue

case .typeMismatch(_, _): fallthrough
case .dataCorrupted(_):
return CocoaError.coderReadCorrupt.rawValue
}
}

public var errorUserInfo: [String : Any] {
let context: Context
switch self {
case .typeMismatch(_, let c): context = c
case .valueNotFound(_, let c): context = c
case .keyNotFound(_, let c): context = c
case .dataCorrupted(let c): context = c
}

return [NSCodingPathErrorKey: context.codingPath,
NSDebugDescriptionErrorKey: context.debugDescription]
}
}

//===----------------------------------------------------------------------===//
// Error Utilities
//===----------------------------------------------------------------------===//
Expand Down
Loading

0 comments on commit 5b07ca5

Please sign in to comment.