-
Notifications
You must be signed in to change notification settings - Fork 211
Data object encoding #692
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
Data object encoding #692
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # frozen_string_literal: true | ||
| require_relative 'helper' | ||
|
|
||
| class PsychDataWithIvar < Data.define(:foo) | ||
| attr_reader :bar | ||
| def initialize(**) | ||
| @bar = 'hello' | ||
| super | ||
| end | ||
| end unless RUBY_VERSION < "3.2" | ||
|
|
||
| module Psych | ||
| class TestData < TestCase | ||
| class SelfReferentialData < Data.define(:foo) | ||
| attr_accessor :ref | ||
| def initialize(foo:) | ||
| @ref = self | ||
| super | ||
| end | ||
| end unless RUBY_VERSION < "3.2" | ||
|
|
||
| def setup | ||
| omit "Data requires ruby >= 3.2" if RUBY_VERSION < "3.2" | ||
| end | ||
|
|
||
| # TODO: move to another test? | ||
| def test_dump_data | ||
| assert_equal <<~eoyml, Psych.dump(PsychDataWithIvar["bar"]) | ||
| --- !ruby/data-with-ivars:PsychDataWithIvar | ||
| members: | ||
| foo: bar | ||
| ivars: | ||
| "@bar": hello | ||
| eoyml | ||
| end | ||
|
|
||
| def test_self_referential_data | ||
| circular = SelfReferentialData.new("foo") | ||
|
|
||
| loaded = Psych.unsafe_load(Psych.dump(circular)) | ||
| assert_instance_of(SelfReferentialData, loaded.ref) | ||
|
|
||
| assert_equal(circular, loaded) | ||
| assert_same(loaded, loaded.ref) | ||
| end | ||
|
|
||
| def test_roundtrip | ||
| thing = PsychDataWithIvar.new("bar") | ||
| data = Psych.unsafe_load(Psych.dump(thing)) | ||
|
|
||
| assert_equal "hello", data.bar | ||
| assert_equal "bar", data.foo | ||
| end | ||
|
|
||
| def test_load | ||
| obj = Psych.unsafe_load(<<~eoyml) | ||
| --- !ruby/data-with-ivars:PsychDataWithIvar | ||
| members: | ||
| foo: bar | ||
| ivars: | ||
| "@bar": hello | ||
| eoyml | ||
|
|
||
| assert_equal "hello", obj.bar | ||
| assert_equal "bar", obj.foo | ||
| end | ||
| end | ||
| end | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is very brittle, and probably only works on CRuby due to this bug or similar: https://bugs.ruby-lang.org/issues/18632
i.e. this is passing
[{ ... }]torb_struct_initialize(), i.e. a positional Hash, not keyword arguments.Normal behavior would be to treat that Hash as the value of the first member, and don't assign other members. But it seems to accidentally work.
It would be a lot safer to pass the values as a simple Array instead, and query the members from the class by calling
struct_or_data_class.members.From truffleruby/truffleruby#4012 (comment)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm trying to repro this in new C API specs but I actually can't, they fail on CRuby too:
gives
From experimenting by tweaking those specs it only works when using
keyword_init: trueexplicitly (likeStruct.new(:a, :b, :c, keyword_init: true)) , and just never works for Data.Maybe it "works" due to init_struct getting kwargs and accidentally propagating that flag to other C calls or so (just a guess).
Looks like it in https://github.com/ruby/ruby/blob/48c7f349f68846e10d60ae77ad299a38ee014479/struct.c#L766
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In marshal.c https://github.com/ruby/ruby/blob/48c7f349f68846e10d60ae77ad299a38ee014479/marshal.c#L2150-L2176
CRuby uses a Hash only if
rb_struct_s_keyword_init(), in all other cases it uses an Array of values.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since for this use case in Psych we only care about Data, not Struct, we should pass an Array of values (same as what Marshal does for Data, as far as I can tell
rb_struct_s_keyword_init()is always false for Data classes).@nevans Would you have time to fix this?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last thought is it would really be good to ask for
rb_data_initialize()upstream at https://bugs.ruby-lang.org/, because reusingrb_struct_initialize()for Data feels like a pretty huge hack, given it doesn't run any function related to Data, onlyrb_struct_initialize_m()which is not meant to be used withData.Of course that can't be used now, but would enable a cleaner way in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix in #749