Skip to content
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

Unexpected behavior #58

Closed
ericlewis opened this issue Feb 22, 2022 · 5 comments
Closed

Unexpected behavior #58

ericlewis opened this issue Feb 22, 2022 · 5 comments

Comments

@ericlewis
Copy link
Contributor

Hey all, I was just trying something out and ran into an odd issue. I am not entirely sure what the compiler is thinking.

The below code has a problem when it comes to decoding, with JSONDecoder throwing Expected to decode Array<Any> but found a dictionary instead. on files, what is interesting is if we change the type to: [String: File] it works perfectly fine. I didn't dig in to it much, but it seems like the ExpressibleByArrayLiteral conformance is being interpreted incorrectly.

Problem code:

struct Gist {
  let id: Self.ID
  let files: [File.Name: File]

  typealias ID = Tagged<Self, String>
}

extension Gist: Identifiable {}
extension Gist: Codable {}

struct File {
  let filename: Name
  let content: String

  typealias Name = Tagged<Self, String>
}

extension File: Codable {}
@stephencelis
Copy link
Member

Hi @ericlewis! There doesn’t appear to be any decoding code in your example above that would throw. Can you provide a full sample that reproduces the issue? There are definitely some gotchas around codability, some of which we can’t work around when it comes to coding containers, but if there is a bug we can fix we’d love to know!

@mbrandonw
Copy link
Member

This is currently a Swift limitation in which only string and integer keys properly encode/decode into dictionaries. A fix has been proposed and accepted into Swift: https://github.com/apple/swift-evolution/blob/main/proposals/0320-codingkeyrepresentable.md

@ericlewis
Copy link
Contributor Author

Ah! Seems like that is the case then @mbrandonw.

@ericlewis
Copy link
Contributor Author

@stephencelis it just using a JSONDecoder.decode(Gist.self, from: data). Nothing fancy. Brandon's reply seems to be the case.

@stephencelis
Copy link
Member

@ericlewis That was one of the issues around decoding containers I was alluding to, just wanted to confirm what data was beforehand :)

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

No branches or pull requests

3 participants