Allow timestamp-tagged scalars to unmarshal into strings#379
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a gap in the YAML constructor logic where scalars implicitly resolved as !!timestamp would not populate Go string targets, leaving them at the zero value. It aligns !!timestamp behavior with other scalar tags by allowing a string fallback that assigns the scalar text directly.
Changes:
- Extend
constructTimestampto supportreflect.Stringtargets by setting the scalar value (Node.Value) into the destination string. - Add a regression test ensuring an implicitly timestamp-tagged scalar unmarshals into
map[string]stringas the original timestamp text.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| yaml_test.go | Adds a regression test covering implicit !!timestamp resolution into a string field. |
| internal/libyaml/constructor.go | Adds reflect.String handling to constructTimestamp so timestamp scalars can populate string targets. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Thanks @oguzkilcan I feel like the fix is good. But I'm doubtful about the tests. Note: I edited your PR description to mention the issue you opened |
021e6cf to
661285b
Compare
|
Thank you for the review. I incorporated your suggestions into the test suite. It run successfully. |
|
I probably need to consult with the core team. |
661285b to
cb1c831
Compare
|
The 1.1 and 1.2 specs allow it to load as a string. It's really up to us what to do.
Honestly I'd like all of tag resolution and construction to be handled at the RuleSet plugin level. Then a RuleSet would be made for the current behavior inherited from go-yaml v3 and that would be the default, and there would become myriad other RuleSets to use or extend. That said I just noticed this is a fix for a v4 regression, so I'll take a closer look now. |
|
Looks like the regression was here: go-yaml/internal/libyaml/constructor.go Lines 653 to 666 in 082f4f4 and also regressed |
There was a problem hiding this comment.
@oguzkilcan Can you add a test for an implicit timestamp, as that is what was reported by #376.
cb1c831 to
3d241b5
Compare
constructTimestamp only handled time.Time and interface{} targets. A plain scalar resolving to the timestamp tag left a string field at its zero value instead of the raw text. This adds a string case that writes the scalar directly, the same fallback other scalar tags already have.
3d241b5 to
1ae3a62
Compare
constructTimestamp only handled time.Time and interface{} targets. A plain scalar resolving to the timestamp tag left a string field at its zero value instead of the raw text. This adds a string case that writes the scalar directly, the same fallback other scalar tags already have.
Fixes #376