Skip to content

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

Merged
merged 4 commits into from
Jul 20, 2017

Conversation

inamiy
Copy link
Contributor

@inamiy inamiy commented Jun 17, 2017

struct MyStruct: Codable {
    let test: String
}

let json = "{}".data(using: .utf8)!

do {
    let decoder = JSONDecoder()
    let decoded = try decoder.decode(MyStruct.self, from: json)
    print(decoded)
}
catch {
    print(error)
}

Result

keyNotFound(CodableDemo.MyStruct.(CodingKeys in _CCD964A2735AA22BDC9F8350D8585760).test, Swift.DecodingError.Context(codingPath: [Optional(CodableDemo.MyStruct.(CodingKeys in _CCD964A2735AA22BDC9F8350D8585760).test)], debugDescription: "Key not found when expecting non-optional type String for coding key \"test\""))

Current DecodingError and EncodingError is hard to read when printing to the console, so I think protocol CodingKey should inherit CustomStringConvertible for better debugging.

@CodaFi CodaFi requested a review from itaiferber July 14, 2017 19:57
@itaiferber itaiferber self-assigned this Jul 14, 2017
Copy link
Contributor

@itaiferber itaiferber left a 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).

@@ -67,6 +67,12 @@ public protocol CodingKey {
init?(intValue: Int)
}

extension CodingKey {
public var description: String {
return stringValue
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improved description in 33582e6 and 98ed5f9, e.g. MyCodingKey(stringValue: "someKey", intValue: nil)

@@ -67,6 +67,12 @@ public protocol CodingKey {
init?(intValue: Int)
}

extension CodingKey {
public var description: String {
Copy link
Contributor

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).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 33582e6.

@inamiy inamiy force-pushed the CodingKey-CustomStringConvertible branch from df29667 to 33582e6 Compare July 16, 2017 03:47
Copy link
Contributor

@itaiferber itaiferber left a 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.

/// 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))"
Copy link
Contributor

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)))

Copy link
Contributor

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)

Copy link
Contributor Author

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 :)

@itaiferber
Copy link
Contributor

@swift-ci Please test and merge

@milseman
Copy link
Member

@itaiferber it doesn't seem the merge took. Do you want to merge this?

@itaiferber itaiferber merged commit 4094737 into swiftlang:master Jul 20, 2017
@itaiferber
Copy link
Contributor

@milseman Thanks for letting me know!

@inamiy
Copy link
Contributor Author

inamiy commented Jul 21, 2017

Thanks for merge 😄

@inamiy inamiy deleted the CodingKey-CustomStringConvertible branch July 21, 2017 01:43
@jrose-apple
Copy link
Contributor

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 stringValue, probably.

The fact that there's not an obvious right answer might mean this should be taken to swift-evolution.

@itaiferber
Copy link
Contributor

@jrose-apple This hasn't made its way to swift-4.0-branch yet, so we can tweak this and turn that off for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants