Skip to content

Treat output from TextMarshaler as string #698

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

Merged
merged 1 commit into from
May 5, 2025
Merged

Treat output from TextMarshaler as string #698

merged 1 commit into from
May 5, 2025

Conversation

shuheiktgw
Copy link
Collaborator

Closes #689.

The original issue was that since null.StringFrom implements the TextMarshaler interface, goccy/go-yaml tries to interpret its output as a YAML document using e.encodeDocument. However, because the output contains a special character (in this case, #), the parser fails to handle it correctly and returns nil.

IIUC, the output from a TextMarshaler implementation should not be interpreted as a YAML document. It should be treated as a plain string, since TextMarshaler is intended to return a textual representation of a value. This behavior is consistent with gopkg.in/yaml.v3.

One side effect I noticed after making this change is related to how *time.Time is encoded. Previously, since *time.Time also implements TextMarshaler, it was being encoded by the code block below. After treating the output from MarshalText() as a string, the library started wrapping *time.Time values in double quotes. To prevent this, I updated the encodeByMarshaler function to handle *time.Time explicitly.

if marshaler, ok := iface.(encoding.TextMarshaler); ok {
		doc, err := marshaler.MarshalText()
		if err != nil {
			return nil, err
		}
		node, err := e.encodeDocument(doc)
...
}
  • Describe the purpose for which you created this PR.
  • Create test code that corresponds to the modification

@shuheiktgw shuheiktgw requested review from Copilot and goccy April 17, 2025 07:36
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

@codecov-commenter
Copy link

⚠️ 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.92%. Comparing base (d1e3460) to head (a75057e).

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

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #698      +/-   ##
==========================================
+ Coverage   77.89%   77.92%   +0.03%     
==========================================
  Files          22       22              
  Lines        7965     7965              
==========================================
+ Hits         6204     6207       +3     
+ Misses       1348     1346       -2     
+ Partials      413      412       -1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@@ -400,20 +400,21 @@ func (e *Encoder) encodeByMarshaler(ctx context.Context, v reflect.Value, column
if t, ok := iface.(time.Time); ok {
return e.encodeTime(t, column), nil
}
// Handle *time.Time explicitly since it implements TextMarshaler and shouldn't be treated as plain text
if t, ok := iface.(*time.Time); ok && t != nil {
Copy link
Owner

Choose a reason for hiding this comment

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

This works correctly 👍

Since isInvalidValue becomes true at the beginning of encodeValue when *time.Time is nil, this process is always executed only when *time.Time is not nil.

@goccy
Copy link
Owner

goccy commented May 5, 2025

Thank you for your great PR ! LGTM 👍

@goccy goccy merged commit cd70dcf into master May 5, 2025
20 checks passed
@goccy goccy deleted the text_marshaler branch May 5, 2025 14:27
nieomylnieja added a commit to nobl9/nobl9-go that referenced this pull request May 12, 2025
…65] (#708)

## Summary

Pinned the `go-yaml` version until
goccy/go-yaml#698 is released.

## Release Notes

Fixes segmentation violation errors when `apiVersion` was set to an
empty string in any of the v1alpha objects.
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.

nil pointer dereference for null.String with some text values
3 participants