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

Fix decoding bugs #27

Merged
merged 4 commits into from
Dec 5, 2024
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
11 changes: 8 additions & 3 deletions Sources/TOMLKit/Decoder/UnkeyedDecodingContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,9 @@ extension InternalTOMLDecoder.UDC {
strictDecoding: self.strictDecoding,
notDecodedKeys: self.notDecodedKeys
)
let decodable = try T(from: decoder)
self.currentIndex += 1
return try T(from: decoder)
return decodable
}
}

Expand All @@ -187,7 +188,7 @@ extension InternalTOMLDecoder.UDC {
)
}

return KeyedDecodingContainer<NestedKey>(
let container = KeyedDecodingContainer<NestedKey>(
InternalTOMLDecoder.KDC(
table: table,
codingPath: self.codingPath + TOMLCodingKey(index: self.currentIndex),
Expand All @@ -197,6 +198,8 @@ extension InternalTOMLDecoder.UDC {
notDecodedKeys: self.notDecodedKeys
)
)
self.currentIndex += 1
return container
}

func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer {
Expand All @@ -211,14 +214,16 @@ extension InternalTOMLDecoder.UDC {
)
}

return InternalTOMLDecoder.UDC(
let container = InternalTOMLDecoder.UDC(
nestedArray,
codingPath: self.codingPath + TOMLCodingKey(index: self.currentIndex),
userInfo: self.userInfo,
dataDecoder: self.dataDecoder,
strictDecoding: self.strictDecoding,
notDecodedKeys: self.notDecodedKeys
)
self.currentIndex += 1
return container
}

func superDecoder() throws -> Decoder {
Expand Down
54 changes: 54 additions & 0 deletions Tests/TOMLKitTests/TOMLKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,58 @@ final class TOMLKitTests: XCTestCase {
XCTFail("DecodingError did not occur.")
}
}

func testFailingToDecodingDateFromUDCShouldNotIncreaseIndex() throws {
let udc = InternalTOMLDecoder.UDC(
["Not a date"],
codingPath: [],
userInfo: [:],
dataDecoder: { $0.string != nil ? Data(base64Encoded: $0.string!) : nil },
strictDecoding: false,
notDecodedKeys: InternalTOMLDecoder.NotDecodedKeys()
)

XCTAssertThrowsError(try udc.decode(Date.self))
XCTAssertEqual(udc.currentIndex, 0)
}

func testDecodingObjectFromUDCShouldIncreaseIndex() throws {
struct StringCodingKey: CodingKey, Equatable {
var stringValue: String

init(stringValue: String) {
self.stringValue = stringValue
}

var intValue: Int? { nil }
init?(intValue: Int) { nil }
}


let udc = InternalTOMLDecoder.UDC(
[TOMLTable(["key": "value"], inline: false)],
codingPath: [],
userInfo: [:],
dataDecoder: { $0.string != nil ? Data(base64Encoded: $0.string!) : nil },
strictDecoding: false,
notDecodedKeys: InternalTOMLDecoder.NotDecodedKeys()
)

let _ = try udc.nestedContainer(keyedBy: StringCodingKey.self)
XCTAssertEqual(udc.currentIndex, 1)
}

func testDecodingArrayFromUDCShouldIncreaseIndex() throws {
let udc = InternalTOMLDecoder.UDC(
[["value"]],
codingPath: [],
userInfo: [:],
dataDecoder: { $0.string != nil ? Data(base64Encoded: $0.string!) : nil },
strictDecoding: false,
notDecodedKeys: InternalTOMLDecoder.NotDecodedKeys()
)

let _ = try udc.nestedUnkeyedContainer()
XCTAssertEqual(udc.currentIndex, 1)
}
}
Loading