Skip to content

Allow encoding empty bare inner lists - #82

Open
deadcaf3 wants to merge 1 commit into
apple:mainfrom
deadcaf3:fix-empty-bare-inner-list
Open

deadcaf3 wants to merge 1 commit into
apple:mainfrom
deadcaf3:fix-empty-bare-inner-list

Conversation

@deadcaf3

Copy link
Copy Markdown

Motivation:

Encoding an empty array as a list member or a dictionary value throws invalidTypeForItem. For example, a list field of [[1, 2, 3], []], or a dictionary field whose [Int] value is empty.

These values start out as itemOrInnerList and only become an inner list once the first element is appended. With nothing to append, the node is never resolved, and inserting it into the parent fails. () is a perfectly valid inner list, so this should just work.

Modifications:

  • When an unkeyed container is requested on an itemOrInnerList node, resolve it to an empty inner list straight away. An unkeyed container can only mean an inner list at that point, so non-empty lists behave exactly as before.
  • Add an encoder test for empty inner lists in a list and in a dictionary.

I did this in unkeyedContainer() rather than in insert(_:atKey:) so that an empty keyed struct still throws instead of quietly turning into ().

Result:

Empty inner lists encode as ().

Tested locally on macOS with Swift 6.4.1, swift test passes.

Motivation:

Encoding an empty array as a list member or dictionary value threw
invalidTypeForItem. Those values start out as itemOrInnerList and only
become an inner list once something is appended, so an empty array left
the node undecided and inserting it into the parent failed.

Modifications:

Resolve itemOrInnerList to an empty inner list as soon as an unkeyed
container is requested, since only an inner list can be encoded that
way. Add an encoder test covering empty inner lists in lists and
dictionaries.

Result:

Empty bare inner lists encode as "()".
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

Successfully merging this pull request may close these issues.

1 participant