-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Let CodingKey inherit CustomStringConvertible for better debugging #10350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Let CodingKey inherit CustomStringConvertible for better debugging #10350
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for putting this together! If we're going to do this for debugging purposes, I think it would be helpful to conform to CustomDebugStringConvertible
as well (which should definitely also display the intValue
, even if it's nil
).
stdlib/public/core/Codable.swift
Outdated
@@ -67,6 +67,12 @@ public protocol CodingKey { | |||
init?(intValue: Int) | |||
} | |||
|
|||
extension CodingKey { | |||
public var description: String { | |||
return stringValue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this has an intValue
too, we should be printing that out as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stdlib/public/core/Codable.swift
Outdated
@@ -67,6 +67,12 @@ public protocol CodingKey { | |||
init?(intValue: Int) | |||
} | |||
|
|||
extension CodingKey { | |||
public var description: String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a doc comment describing this (something like "A textual representation of this key." should suffice).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in 33582e6.
df29667
to
33582e6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! I'd just say that you can shorten the method for doing string interpolation in description
, but looks good otherwise.
stdlib/public/core/Codable.swift
Outdated
/// A textual representation of this key. | ||
public var description: String { | ||
let intValue = self.intValue?.description ?? "nil" | ||
return "\(String(describing: type(of: self)))(stringValue: \"\(stringValue)\", intValue: \(intValue))" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just use \(type(of: self))
instead of \(String(describing: type(of: self)))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(After you change this, BTW, I will test and merge)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's simpler! I fixed in e017508 :)
@swift-ci Please test and merge |
@itaiferber it doesn't seem the merge took. Do you want to merge this? |
@milseman Thanks for letting me know! |
Thanks for merge 😄 |
I'm not sure it makes sense for these to be CustomStringConvertible this way. CustomDebugStringConvertible makes sense, but CustomStringConvertible is the sort of thing you'd put into an interpolation. I'd expect just the The fact that there's not an obvious right answer might mean this should be taken to swift-evolution. |
@jrose-apple This hasn't made its way to |
Result
Current
DecodingError
andEncodingError
is hard to read when printing to the console, so I thinkprotocol CodingKey
should inheritCustomStringConvertible
for better debugging.