-
Couldn't load subscription status.
- Fork 209
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
Data object encoding #692
Conversation
|
FYI: I haven't put any guards in yet to make this work for older versions of ruby (before |
|
I'll update this to pass for earlier versions of ruby. But I'm also looking for feedback on the approach. The more I think about it, the more I'm thinking this should use |
3110d41 to
3a845fd
Compare
|
For what it's worth, I've updated the PR to:
|
2292f0b to
325b073
Compare
|
Sorry. I thought I had it passing before, but I must have made a mistake in my earlier tests with ruby 2.5 (perhaps I had somehow disabled warnings locally)? I've changed the guards from |
|
Any thoughts about this approach? (Is it too close to the 3.4.0 release to hope for getting support for |
|
It would be possible to call JRuby's internal API from Ruby, but since we already have an extension for this library we can just add it there. This feature and the tests provided are guarded to only happen on Ruby 3.2+, so JRuby 9.4 would not be affected (it is 3.1 compatible) and JRuby 10 (3.4 compatible) is unreleased so we have some time to add JRuby support. |
|
@tenderlove Thanks. I did briefly consider abusing @headius I think that I may have screwed up the ruby 3.2 guard, the last time I updated my branch. I need to rebase anyway, so I'll fix it and double check that the tests pass under older ruby versions. |
This sets the ivars _before_ calling initialize, which feels wrong. But Data doesn't give us any mechanism for setting the members other than 1) initialize, or 2) drop down into the C API. Since initialize freezes the object, we need to set the ivars before that. I think this is a reasonable compromise—if users need better handling, they can implement their own `encode_with` and `init_with`. But it will lead to unhappy surprises for some users. Alternatively, we could use the C API, similarly to Marshal. Psych _is_ already using the C API for path2class and build_exception. This would be the least surprising behavior for users, I think.
325b073 to
3573fb3
Compare
|
@tenderlove Okay, I rebased on the latest I also changed the tests to use |
This adds basic encoding/decoding for ruby 3.2
Dataobjects, using!ruby/data:ClassName(a mapping of member names to member values) and!ruby/data-with-ivars:ClassNametags (a mapping withmembersandivarsmappings). LikeMarshal, this usesrb_struct_initializeto assign member values to newDataobjects.Originally (in the first commit), I updated
Visitor::ToRubyto simply calldata.send(:initialize, **members). But#initializefreezes the object, so we can't set instance variables afterwards.The second commit sets instance variables before calling
#initialize, which I think will lead to unhappy surprises for some users. Unfortunately,Datadoesn't give us any other mechanism for setting the members... except for the C API.The third commit works around the above issue by copying how Marshal loads
Dataobjects, delegatingToRuby#init_struct(obj, members)torb_struct_initialize. I think this is the least surprising behavior for users. This also bypasses any user-defined#initializemethod (which may be seen as a feature or a bug).