Skip to content

Commit 4094737

Browse files
authored
Merge pull request #10350 from inamiy/CodingKey-CustomStringConvertible
Let CodingKey inherit CustomStringConvertible for better debugging
2 parents 3f308b7 + e017508 commit 4094737

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

stdlib/public/core/Codable.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public typealias Codable = Encodable & Decodable
4747
//===----------------------------------------------------------------------===//
4848

4949
/// A type that can be used as a key for encoding and decoding.
50-
public protocol CodingKey {
50+
public protocol CodingKey: CustomStringConvertible, CustomDebugStringConvertible {
5151
/// The string to use in a named collection (e.g. a string-keyed dictionary).
5252
var stringValue: String { get }
5353

@@ -67,6 +67,19 @@ public protocol CodingKey {
6767
init?(intValue: Int)
6868
}
6969

70+
extension CodingKey {
71+
/// A textual representation of this key.
72+
public var description: String {
73+
let intValue = self.intValue?.description ?? "nil"
74+
return "\(type(of: self))(stringValue: \"\(stringValue)\", intValue: \(intValue))"
75+
}
76+
77+
/// A textual representation of this key, suitable for debugging.
78+
public var debugDescription: String {
79+
return description
80+
}
81+
}
82+
7083
//===----------------------------------------------------------------------===//
7184
// Encoder & Decoder
7285
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)