msgpack / msgpack-ruby Public
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
Support serializing bigint as extension types #244
Conversation
The motivation about BigInt is acceptable for me. In other words, |
Of course. The rationale is that using the native type when the integer fits in it is:
IMO extension types only make sense if the data doesn't fit a native type, and I see no benefit into being able to replace native types. We could however have it has an option, but again, I'd rather not do it unless someone see a use case for it (I don't). |
7cdfe71
to
79e4fdc
Compare
@casperisfine Could you rebase this change too? |
Rebased.
No need to apologize, thanks for all the reviews! |
@tagomoris I updated this PR with your comments from #243
if (hasBigintExtType && tryAppendWithExtTypeLookup(object)) { | ||
return; | ||
} | ||
throw runtime.newRangeError(String.format("Cannot pack big integer: %s", value)); |
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.
The MRI extension raises RangeError
on bigint, but the JRuby one ArgumentError
. I assume it is a bug.
ext/msgpack/factory_class.c
Outdated
@@ -209,6 +211,10 @@ static VALUE Factory_register_type(int argc, VALUE* argv, VALUE self) | |||
} | |||
} | |||
|
|||
if (ext_module == rb_cInteger && RB_TEST(rb_hash_aref(options, ID2SYM(rb_intern("bigint"))))) { |
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.
No idea what's the best key to pass, so I went with bigint: true
, but I'm open to suggestions.
# frozen_string_literal: true | ||
|
||
module MessagePack | ||
module Bigint |
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 it makes sense to provide a decent BigInt packer
Option renamed as |
Could you change to ... ?:
- raise ArgumentError if
oversized_integer_extension: true
is specified with non-Integer
class - not to require
msgpack/bigint
in default because its serialization format is not any agreed format - it should be required by users explicitly
if(required_size > 8 && pk->has_bigint_ext_type) { | ||
if(msgpack_packer_try_write_with_ext_type_lookup(pk, v)) { | ||
return; | ||
} |
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.
Does this code let msgpack_packer_write_u64
raise RangeError unless pk->has_bigint_ext_type
, right?
If so, I want a comment to explain it here.
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.
Yes, I wrote a spec for it, that's how I noticed JRuby was inconsistent.
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.
Oh, sorry, I want a code comment at just after L482
@tagomoris it should be good now. |
It cause two methods to be defined instead of one. Not the end of the world, but seems unnecessary. But if you really don't like |
Using |
I'd rather not, |
Is this only a stylistic issue? Because this pattern is fairly common. |
You can search for the |
I want to decrease points to confuse myself in the future. |
Then maybe some extra commenting could help? |
Could you write the implementation for the current Ruby versions at first (with appropriate code comments)? |
Of course. |
Fix: msgpack#243 ```ruby factory = MessagePack::Factory.new factory.register_type( 0x42, Integer, packer: ->(int) { int.to_s }, unpacker: -> (payload) { Integer(payload) } ) factory.dump(42) # Use the native integer type factory.dump(2**400) # Use the registered packer ```
Done. |
Thanks for the merge! |
Fix: #243