Skip to content

Commit 0796f7e

Browse files
authored
Merge pull request #27 from sketch204/fix/dateDecodingBug
Fix decoding bugs
2 parents ec6198d + 756330f commit 0796f7e

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

Sources/TOMLKit/Decoder/UnkeyedDecodingContainer.swift

+8-3
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,9 @@ extension InternalTOMLDecoder.UDC {
168168
strictDecoding: self.strictDecoding,
169169
notDecodedKeys: self.notDecodedKeys
170170
)
171+
let decodable = try T(from: decoder)
171172
self.currentIndex += 1
172-
return try T(from: decoder)
173+
return decodable
173174
}
174175
}
175176

@@ -187,7 +188,7 @@ extension InternalTOMLDecoder.UDC {
187188
)
188189
}
189190

190-
return KeyedDecodingContainer<NestedKey>(
191+
let container = KeyedDecodingContainer<NestedKey>(
191192
InternalTOMLDecoder.KDC(
192193
table: table,
193194
codingPath: self.codingPath + TOMLCodingKey(index: self.currentIndex),
@@ -197,6 +198,8 @@ extension InternalTOMLDecoder.UDC {
197198
notDecodedKeys: self.notDecodedKeys
198199
)
199200
)
201+
self.currentIndex += 1
202+
return container
200203
}
201204

202205
func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer {
@@ -211,14 +214,16 @@ extension InternalTOMLDecoder.UDC {
211214
)
212215
}
213216

214-
return InternalTOMLDecoder.UDC(
217+
let container = InternalTOMLDecoder.UDC(
215218
nestedArray,
216219
codingPath: self.codingPath + TOMLCodingKey(index: self.currentIndex),
217220
userInfo: self.userInfo,
218221
dataDecoder: self.dataDecoder,
219222
strictDecoding: self.strictDecoding,
220223
notDecodedKeys: self.notDecodedKeys
221224
)
225+
self.currentIndex += 1
226+
return container
222227
}
223228

224229
func superDecoder() throws -> Decoder {

Tests/TOMLKitTests/TOMLKitTests.swift

+54
Original file line numberDiff line numberDiff line change
@@ -447,4 +447,58 @@ final class TOMLKitTests: XCTestCase {
447447
XCTFail("DecodingError did not occur.")
448448
}
449449
}
450+
451+
func testFailingToDecodingDateFromUDCShouldNotIncreaseIndex() throws {
452+
let udc = InternalTOMLDecoder.UDC(
453+
["Not a date"],
454+
codingPath: [],
455+
userInfo: [:],
456+
dataDecoder: { $0.string != nil ? Data(base64Encoded: $0.string!) : nil },
457+
strictDecoding: false,
458+
notDecodedKeys: InternalTOMLDecoder.NotDecodedKeys()
459+
)
460+
461+
XCTAssertThrowsError(try udc.decode(Date.self))
462+
XCTAssertEqual(udc.currentIndex, 0)
463+
}
464+
465+
func testDecodingObjectFromUDCShouldIncreaseIndex() throws {
466+
struct StringCodingKey: CodingKey, Equatable {
467+
var stringValue: String
468+
469+
init(stringValue: String) {
470+
self.stringValue = stringValue
471+
}
472+
473+
var intValue: Int? { nil }
474+
init?(intValue: Int) { nil }
475+
}
476+
477+
478+
let udc = InternalTOMLDecoder.UDC(
479+
[TOMLTable(["key": "value"], inline: false)],
480+
codingPath: [],
481+
userInfo: [:],
482+
dataDecoder: { $0.string != nil ? Data(base64Encoded: $0.string!) : nil },
483+
strictDecoding: false,
484+
notDecodedKeys: InternalTOMLDecoder.NotDecodedKeys()
485+
)
486+
487+
let _ = try udc.nestedContainer(keyedBy: StringCodingKey.self)
488+
XCTAssertEqual(udc.currentIndex, 1)
489+
}
490+
491+
func testDecodingArrayFromUDCShouldIncreaseIndex() throws {
492+
let udc = InternalTOMLDecoder.UDC(
493+
[["value"]],
494+
codingPath: [],
495+
userInfo: [:],
496+
dataDecoder: { $0.string != nil ? Data(base64Encoded: $0.string!) : nil },
497+
strictDecoding: false,
498+
notDecodedKeys: InternalTOMLDecoder.NotDecodedKeys()
499+
)
500+
501+
let _ = try udc.nestedUnkeyedContainer()
502+
XCTAssertEqual(udc.currentIndex, 1)
503+
}
450504
}

0 commit comments

Comments
 (0)