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

LossyDictionary: added failing test and fix for key conversion issue #51

Merged
merged 1 commit into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions Sources/BetterCodable/LossyDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ extension LossyDictionary: Decodable where Key: Decodable, Value: Decodable {
var elements: [Key: Value] = [:]
if Key.self == String.self {
let container = try decoder.container(keyedBy: DictionaryCodingKey.self)
let keys = try Self.extractKeys(from: decoder, container: container)

for key in container.allKeys {
for (key, stringKey) in keys {
do {
let value = try container.decode(LossyDecodableValue<Value>.self, forKey: key).value
elements[key.stringValue as! Key] = value
elements[stringKey as! Key] = value
} catch {
_ = try? container.decode(AnyDecodableValue.self, forKey: key)
}
Expand Down Expand Up @@ -81,6 +82,21 @@ extension LossyDictionary: Decodable where Key: Decodable, Value: Decodable {

self.wrappedValue = elements
}

private static func extractKeys(
from decoder: Decoder,
container: KeyedDecodingContainer<DictionaryCodingKey>
) throws -> [(DictionaryCodingKey, String)] {
// Decode a dictionary ignoring the values to decode the original keys
// without using the `JSONDecoder.KeyDecodingStrategy`.
let keys = try decoder.singleValueContainer().decode([String: AnyDecodableValue].self).keys

return zip(
container.allKeys.sorted(by: { $0.stringValue < $1.stringValue }),
keys.sorted()
Copy link
Owner

Choose a reason for hiding this comment

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

I understand why this is necessary, but how robust is it? Can you think of a scenario where the sorting would be different when converting from snake case? I'd hate for there to be an edge case that's missed.

I think the answer is "no", but is it possible to route through this only when the key strategy isn't the default?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I couldn't think of a way, but I agree it's not ideal.
Unfortunately the Decoder protocol doesn't expose that. I went through the whole Swift implementation to understand how it uses private types in a way that we can't.

Copy link
Owner

Choose a reason for hiding this comment

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

Thanks for the due diligence. I'll play around with this tonight and try and button up some other in flight work.

)
.map { ($0, $1) }
}
}

extension LossyDictionary: Encodable where Key: Encodable, Value: Encodable {
Expand Down
23 changes: 23 additions & 0 deletions Tests/BetterCodableTests/LossyDictionaryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,27 @@ class LossyDictionaryTests: XCTestCase {
XCTAssertEqual(fixture.stringToInt, ["one": 1, "two": 2, "three": 3])
XCTAssertEqual(fixture.intToString, [1: "one", 2: "two", 3: "three"])
}

func testEncodingLosslessDictionaryRetainsKeys() throws {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase

let encoder = JSONEncoder()
encoder.keyEncodingStrategy = .convertToSnakeCase

let fixture = Fixture(
stringToInt: [
"snake_case.with.dots_99.and_numbers": 1,
"dots.and_2.00.1_numbers": 2,
"key.1": 3,
"normal key": 4,
"another_key": 5
],
intToString: [:]
)

let reencodedFixture = try decoder.decode(Fixture.self, from: encoder.encode(fixture))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Encode and decode to ensure that fixture == reencodedFixture.


XCTAssertEqual(reencodedFixture, fixture)
}
}