Add support for RawMessage, similar to json.RawMessage#790
Add support for RawMessage, similar to json.RawMessage#790goccy merged 2 commits intogoccy:masterfrom
Conversation
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #790 +/- ##
==========================================
- Coverage 77.97% 77.91% -0.06%
==========================================
Files 22 22
Lines 8108 8123 +15
==========================================
+ Hits 6322 6329 +7
- Misses 1370 1376 +6
- Partials 416 418 +2 🚀 New features to boost your workflow:
|
This adds a type that users can use to refer to raw data in the yaml for deferred decoding. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
06efea9 to
4aa949a
Compare
There was a problem hiding this comment.
Pull request overview
This PR adds support for yaml.RawMessage, a type similar to json.RawMessage from the standard library. It enables delayed YAML decoding or precomputed YAML encoding, with JSON interoperability through additional marshal/unmarshal methods.
Key Changes
- Implemented
RawMessagetype with YAML and JSON marshaling/unmarshaling support - Added comprehensive test coverage including JSON compatibility scenarios
- Minor style improvements (var to := syntax) in existing tests
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| yaml.go | Adds RawMessage type with MarshalYAML, UnmarshalYAML, MarshalJSON, and UnmarshalJSON methods for bidirectional YAML/JSON compatibility |
| yaml_test.go | Adds comprehensive test coverage including TestRawMessage for basic functionality and TestRawMessageJSONCompatibility for JSON interoperability scenarios |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| return m.UnmarshalYAML(b) | ||
| } | ||
|
|
||
| func (m RawMessage) MarshalJSON() ([]byte, error) { |
There was a problem hiding this comment.
The MarshalJSON method doesn't handle the nil case explicitly, unlike MarshalYAML which returns []byte("null") for nil values. When m is nil, YAMLToJSON(nil) will be called which may not produce the expected "null" JSON value. Consider adding a nil check:
func (m RawMessage) MarshalJSON() ([]byte, error) {
if m == nil {
return []byte("null"), nil
}
return YAMLToJSON(m)
}This ensures consistency with MarshalYAML and matches the behavior of json.RawMessage in the standard library.
| func (m RawMessage) MarshalJSON() ([]byte, error) { | |
| func (m RawMessage) MarshalJSON() ([]byte, error) { | |
| if m == nil { | |
| return []byte("null"), nil | |
| } |
There was a problem hiding this comment.
It's ok, In my understand, YAMLToJSON handles nil correctly.
|
@thanethomson (cc: @cpuguy83 ) Thank you for your contribution !!! LGTM 👍 |
* Add support for RawMessage, similar to json.RawMessage This adds a type that users can use to refer to raw data in the yaml for deferred decoding. Signed-off-by: Brian Goff <cpuguy83@gmail.com> * test: add suggested cases from goccy#668 --------- Signed-off-by: Brian Goff <cpuguy83@gmail.com> Co-authored-by: Brian Goff <cpuguy83@gmail.com>
* Add support for RawMessage, similar to json.RawMessage This adds a type that users can use to refer to raw data in the yaml for deferred decoding. Signed-off-by: Brian Goff <cpuguy83@gmail.com> * test: add suggested cases from goccy#668 --------- Signed-off-by: Brian Goff <cpuguy83@gmail.com> Co-authored-by: Brian Goff <cpuguy83@gmail.com>
Builds on #668, since that PR seems stale and I'd like to make use of this feature.
@goccy I've attempted to add the test cases you requested in #668 (comment) - do these work for you?
Let me know if there's anything else I should add.
Closes #425.