Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@vpapp
Copy link
Contributor

@vpapp vpapp commented Mar 10, 2025

Goal of this PR

Fix handling of empty and nil slices. There are scenarios where empty slices get turned into slices of 1 element, and where nil slices cause a panic.

This PR fixes these scenarios.

How did I test it?

Added unit tests that demonstrate various scenarios, some of which fail before the fix and pass with the fix.
Notably:

  • TestDecoder_UnionInterfaceArrayEmpty
    • empty array of ints in union decoded as a slice of int with 1 element instead of empty slice
  • TestDecoder_UnionArrayRecordEmpty
    • empty array of records in union decoded to non-nil pointer to nil slice as nil slice instead of empty slice
  • TestDecoder_UnionInterfaceArrayRecordEmpty
    • empty array of records in union decoded as a slice of records with 1 element instead of empty slice
  • TestEncoder_UnionInterfaceUnregisteredArrayNull
    • encoding a nil array in map[string]any panicked instead of encoding an empty slice

@vpapp vpapp force-pushed the fix-empty-nil-slices branch from 1d734d3 to fa178bf Compare March 10, 2025 18:29
Copy link
Member

@nrwiersma nrwiersma left a comment

Choose a reason for hiding this comment

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

LGTM, thanks

@nrwiersma nrwiersma merged commit 52e732b into hamba:main Mar 10, 2025
13 checks passed
@vpapp vpapp deleted the fix-empty-nil-slices branch March 10, 2025 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants