Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encodeMap() error handling and TestEncoder_UnmarshallableTypes() #674

Merged
merged 2 commits into from
Mar 7, 2025

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants