Treat output from TextMarshaler as string #698
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #689.
The original issue was that since
null.StringFromimplements theTextMarshalerinterface,goccy/go-yamltries to interpret its output as a YAML document usinge.encodeDocument. However, because the output contains a special character (in this case,#), the parser fails to handle it correctly and returnsnil.IIUC, the output from a
TextMarshalerimplementation should not be interpreted as a YAML document. It should be treated as a plain string, sinceTextMarshaleris intended to return a textual representation of a value. This behavior is consistent withgopkg.in/yaml.v3.One side effect I noticed after making this change is related to how
*time.Timeis encoded. Previously, since*time.Timealso implementsTextMarshaler, it was being encoded by the code block below. After treating the output fromMarshalText()as a string, the library started wrapping*time.Timevalues in double quotes. To prevent this, I updated theencodeByMarshalerfunction to handle*time.Timeexplicitly.