Skip to content

Conversation

@dorencambia
Copy link
Contributor

@dorencambia dorencambia commented Mar 6, 2025

Problem

I encountered an issue where unmarshallable types in maps do not properly handle errors.

bytes, err := yaml.Marshal(map[string]any{"a": make(chan int)}) returns a nil error and string(bytes) is <nil>\n.

Similarly, calling Marshal on nested map[string]any{"a": map[string]any{"b": make(chan string)}} returns a nil error and string(bytes) is %!v(PANIC=String method: runtime error: invalid memory address or nil pointer dereference)\n

Solution

Add error to return value in func (e *Encoder) encodeMap(ctx context.Context, value reflect.Value, column int) (ast.Node, error) and return the error when value can't be encoded.

Testing

Add new func TestEncoder_UnmarshallableTypes(t *testing.T) with these test cases:

  • channel
  • function
  • complex number
  • unsafe pointer
  • uintptr
  • map with channel
  • nested map with func
  • slice with channel
  • nested slice with complex number
  • struct with unsafe pointer
@codecov-commenter
Copy link

codecov-commenter commented Mar 6, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 77.84%. Comparing base (944206a) to head (5a65c54).

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #674      +/-   ##
==========================================
+ Coverage   77.63%   77.84%   +0.21%     
==========================================
  Files          22       22              
  Lines        7959     7959              
==========================================
+ Hits         6179     6196      +17     
+ Misses       1362     1350      -12     
+ Partials      418      413       -5     
🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
Copy link
Owner

@goccy goccy left a comment

Choose a reason for hiding this comment

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

Thank you for your great PR !! LGTM 👍

@goccy goccy merged commit 9671363 into goccy:master Mar 7, 2025
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants