MessagePacker icon indicating copy to clipboard operation
MessagePacker copied to clipboard

Unexpected behavior of implementation of UnkeyedDecodingContainer.decodeIfPresent(_:) method

Open sdpopov-keyvariable opened this issue 3 years ago • 0 comments

Let's take array of optional integers:

let values = [ 1, nil, 2 ]

Then encode it:

var container = encoder.unkeyedContainer()

try values.forEach {
    try container.encode($0)
}

The result matches expectation. Then decode it:

var values: [Int?] = .init()
var container = try decoder.unkeyedContainer()
for _ in 0..<3 {
    values.append(try container.decodeIfPresent(Int.self))
}

The resulting array is [ 1, nil, nil ], but must be [ 1, nil, 2 ]. This behavior is incorrect (see documentation).

As I see in debugger container doesn't increment .contentIndex. It remains 1.

MessagePacker 0.4.7, MacOS 13.2.1, Xcode 14.2.

sdpopov-keyvariable avatar Feb 19 '23 21:02 sdpopov-keyvariable