msgpack 1.2.10-java → 1.4.0-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 68e21b85c0e2e2db53641e8b8eabd536907b1b96f91ba6f98a8a45e51f6895cf
4
- data.tar.gz: 7a413a7514351ba8c4fa80068a4d5a597af824e0eae9ce8f9cf4fea771e1e280
3
+ metadata.gz: df3b24e93761eeee9892ce39650e1d78ef3b10690c17175ff1fdf89fb2430e79
4
+ data.tar.gz: 0aca3d762180d301e52bd89f303ff593487f3ceb468bf792e378ee4b5a56af11
5
5
  SHA512:
6
- metadata.gz: 5e03b708ad5d3e50ef830b5b6965bd22556dc1681690fba5cb0a21fe57ef54d27952fad7d17d88949177828047e6a1f99027ff42632cab567921be727bb18f57
7
- data.tar.gz: 80e15ddbf479f6939802d23a3ce886c0a667ae9d60d5e9e78f436274c456cb6d7903c9d6ce880a695aad1c72f4c2ee61c58faea66ae47bbc11fbbf35f60928ed
6
+ metadata.gz: 18a3f11686786575886117fdc5f0886c8c3bdd796a79f92bac63437771c885235f80e05f87abf65ad427ca9343931adb79f2a8ba3ba00c73ae3d078f86c12883
7
+ data.tar.gz: 8462fa5a43f395a56bb01223c0513b06a9761d2608436b1675b036e0d94a393f896f0779e68e416d8d0c2e779cce50debab7dcc03e6ed816b7739d1a667ddf3f
@@ -5,11 +5,7 @@ if defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby" # This is same with `/java/ =
5
5
  require "msgpack/msgpack.jar"
6
6
  org.msgpack.jruby.MessagePackLibrary.new.load(JRuby.runtime, false)
7
7
  else
8
- begin
9
- require "msgpack/#{RUBY_VERSION[/\d+.\d+/]}/msgpack"
10
- rescue LoadError
11
- require "msgpack/msgpack"
12
- end
8
+ require "msgpack/msgpack"
13
9
  end
14
10
 
15
11
  require "msgpack/packer"
@@ -17,6 +13,8 @@ require "msgpack/unpacker"
17
13
  require "msgpack/factory"
18
14
  require "msgpack/symbol"
19
15
  require "msgpack/core_ext"
16
+ require "msgpack/timestamp"
17
+ require "msgpack/time"
20
18
 
21
19
  module MessagePack
22
20
  DefaultFactory = MessagePack::Factory.new
Binary file
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ # MessagePack extention packer and unpacker for built-in Time class
4
+ module MessagePack
5
+ module Time
6
+ # 3-arg Time.at is available Ruby >= 2.5
7
+ TIME_AT_3_AVAILABLE = begin
8
+ !!::Time.at(0, 0, :nanosecond)
9
+ rescue ArgumentError
10
+ false
11
+ end
12
+
13
+ Unpacker = if TIME_AT_3_AVAILABLE
14
+ lambda do |payload|
15
+ tv = MessagePack::Timestamp.from_msgpack_ext(payload)
16
+ ::Time.at(tv.sec, tv.nsec, :nanosecond)
17
+ end
18
+ else
19
+ lambda do |payload|
20
+ tv = MessagePack::Timestamp.from_msgpack_ext(payload)
21
+ ::Time.at(tv.sec, tv.nsec / 1000.0)
22
+ end
23
+ end
24
+
25
+ Packer = lambda { |time|
26
+ MessagePack::Timestamp.to_msgpack_ext(time.tv_sec, time.tv_nsec)
27
+ }
28
+ end
29
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MessagePack
4
+ class Timestamp # a.k.a. "TimeSpec"
5
+ # Because the byte-order of MessagePack is big-endian in,
6
+ # pack() and unpack() specifies ">".
7
+ # See https://docs.ruby-lang.org/en/trunk/Array.html#method-i-pack for details.
8
+
9
+ # The timestamp extension type defined in the MessagePack spec.
10
+ # See https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type for details.
11
+ TYPE = -1
12
+
13
+ TIMESTAMP32_MAX_SEC = (1 << 32) - 1
14
+ TIMESTAMP64_MAX_SEC = (1 << 34) - 1
15
+
16
+ # @return [Integer]
17
+ attr_reader :sec
18
+
19
+ # @return [Integer]
20
+ attr_reader :nsec
21
+
22
+ # @param [Integer] sec
23
+ # @param [Integer] nsec
24
+ def initialize(sec, nsec)
25
+ @sec = sec
26
+ @nsec = nsec
27
+ end
28
+
29
+ def self.from_msgpack_ext(data)
30
+ case data.length
31
+ when 4
32
+ # timestamp32 (sec: uint32be)
33
+ sec, = data.unpack('L>')
34
+ new(sec, 0)
35
+ when 8
36
+ # timestamp64 (nsec: uint30be, sec: uint34be)
37
+ n, s = data.unpack('L>2')
38
+ sec = ((n & 0b11) << 32) | s
39
+ nsec = n >> 2
40
+ new(sec, nsec)
41
+ when 12
42
+ # timestam96 (nsec: uint32be, sec: int64be)
43
+ nsec, sec = data.unpack('L>q>')
44
+ new(sec, nsec)
45
+ else
46
+ raise MalformedFormatError, "Invalid timestamp data size: #{data.length}"
47
+ end
48
+ end
49
+
50
+ def self.to_msgpack_ext(sec, nsec)
51
+ if sec >= 0 && nsec >= 0 && sec <= TIMESTAMP64_MAX_SEC
52
+ if nsec === 0 && sec <= TIMESTAMP32_MAX_SEC
53
+ # timestamp32 = (sec: uint32be)
54
+ [sec].pack('L>')
55
+ else
56
+ # timestamp64 (nsec: uint30be, sec: uint34be)
57
+ nsec30 = nsec << 2
58
+ sec_high2 = sec >> 32 # high 2 bits (`x & 0b11` is redandunt)
59
+ sec_low32 = sec & 0xffffffff # low 32 bits
60
+ [nsec30 | sec_high2, sec_low32].pack('L>2')
61
+ end
62
+ else
63
+ # timestamp96 (nsec: uint32be, sec: int64be)
64
+ [nsec, sec].pack('L>q>')
65
+ end
66
+ end
67
+
68
+ def to_msgpack_ext
69
+ self.class.to_msgpack_ext(sec, nsec)
70
+ end
71
+
72
+ def ==(other)
73
+ other.class == self.class && sec == other.sec && nsec == other.nsec
74
+ end
75
+ end
76
+ end
@@ -1,9 +1,6 @@
1
1
  module MessagePack
2
- VERSION = "1.2.10"
3
-
4
- # NOTE for msgpack-ruby maintainer:
5
- # Check these things to release new binaryes for new Ruby versions (especially for Windows):
6
- # * versions/supports of rake-compiler & rake-compiler-dock
7
- # * update RUBY_CC_VERSION in Rakefile
8
- # * check Ruby dependency of released mswin gem details
2
+ VERSION = "1.4.0"
3
+ # Note for maintainers:
4
+ # Don't miss building/releasing the JRuby version (rake buld:java)
5
+ # See "How to build -java rubygems" in README for more details.
9
6
  end
@@ -19,6 +19,23 @@ def java?
19
19
  /java/ =~ RUBY_PLATFORM
20
20
  end
21
21
 
22
+ # checking if Hash#[]= (rb_hash_aset) dedupes string keys
23
+ def automatic_string_keys_deduplication?
24
+ h = {}
25
+ x = {}
26
+ r = rand.to_s
27
+ h[%W(#{r}).join('')] = :foo
28
+ x[%W(#{r}).join('')] = :foo
29
+
30
+ x.keys[0].equal?(h.keys[0])
31
+ end
32
+
33
+ def string_deduplication?
34
+ r1 = rand.to_s
35
+ r2 = r1.dup
36
+ (-r1).equal?(-r2)
37
+ end
38
+
22
39
  if java?
23
40
  RSpec.configure do |c|
24
41
  c.treat_symbols_as_metadata_keys_with_true_values = true
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe MessagePack::Timestamp do
6
+ describe 'malformed format' do
7
+ it do
8
+ expect do
9
+ MessagePack::Timestamp.from_msgpack_ext([0xd4, 0x00].pack("C*"))
10
+ end.to raise_error(MessagePack::MalformedFormatError)
11
+ end
12
+ end
13
+
14
+ describe 'register_type with Time' do
15
+ let(:factory) do
16
+ factory = MessagePack::Factory.new
17
+ factory.register_type(
18
+ MessagePack::Timestamp::TYPE,
19
+ Time,
20
+ packer: MessagePack::Time::Packer,
21
+ unpacker: MessagePack::Time::Unpacker
22
+ )
23
+ factory
24
+ end
25
+
26
+ let(:time) { Time.local(2019, 6, 17, 1, 2, 3, 123_456_789 / 1000.0) }
27
+ it 'serializes and deserializes Time' do
28
+ prefix_fixext8_with_type_id = [0xd7, -1].pack("c*")
29
+
30
+ packed = factory.pack(time)
31
+ expect(packed).to start_with(prefix_fixext8_with_type_id)
32
+ expect(packed.size).to eq(10)
33
+ unpacked = factory.unpack(packed)
34
+ expect(unpacked.to_i).to eq(time.to_i)
35
+ expect(unpacked.to_f).to eq(time.to_f)
36
+ # expect(unpacked).to eq(time) # we can't do it because of nsec (rational vs float?)
37
+ end
38
+
39
+ let(:time_without_nsec) { Time.local(2019, 6, 17, 1, 2, 3, 0) }
40
+ it 'serializes time without nanosec as fixext4' do
41
+ prefix_fixext4_with_type_id = [0xd6, -1].pack("c*")
42
+
43
+ packed = factory.pack(time_without_nsec)
44
+ expect(packed).to start_with(prefix_fixext4_with_type_id)
45
+ expect(packed.size).to eq(6)
46
+ unpacked = factory.unpack(packed)
47
+ expect(unpacked).to eq(time_without_nsec)
48
+ end
49
+
50
+ let(:time_after_2514) { Time.at(1 << 34) } # the max num of 34bit int means 2514-05-30 01:53:04 UTC
51
+ it 'serializes time after 2038 as ext8' do
52
+ prefix_ext8_with_12bytes_payload_and_type_id = [0xc7, 12, -1].pack("c*")
53
+
54
+ expect(time_after_2514.to_i).to be > 0xffffffff
55
+ packed = factory.pack(time_after_2514)
56
+ expect(packed).to start_with(prefix_ext8_with_12bytes_payload_and_type_id)
57
+ expect(packed.size).to eq(15)
58
+ end
59
+
60
+ it 'runs correctly (regression)' do
61
+ expect(factory.unpack(factory.pack(Time.utc(2200)))).to eq(Time.utc(2200))
62
+ end
63
+ end
64
+
65
+ describe 'register_type with MessagePack::Timestamp' do
66
+ let(:factory) do
67
+ factory = MessagePack::Factory.new
68
+ factory.register_type(MessagePack::Timestamp::TYPE, MessagePack::Timestamp)
69
+ factory
70
+ end
71
+
72
+ let(:timestamp) { MessagePack::Timestamp.new(Time.now.tv_sec, 123_456_789) }
73
+ it 'serializes and deserializes MessagePack::Timestamp' do
74
+ packed = factory.pack(timestamp)
75
+ unpacked = factory.unpack(packed)
76
+ expect(unpacked).to eq(timestamp)
77
+ end
78
+ end
79
+
80
+ describe 'timestamp32' do
81
+ it 'handles [1, 0]' do
82
+ t = MessagePack::Timestamp.new(1, 0)
83
+
84
+ payload = t.to_msgpack_ext
85
+ unpacked = MessagePack::Timestamp.from_msgpack_ext(payload)
86
+
87
+ expect(unpacked).to eq(t)
88
+ end
89
+ end
90
+
91
+ describe 'timestamp64' do
92
+ it 'handles [1, 1]' do
93
+ t = MessagePack::Timestamp.new(1, 1)
94
+
95
+ payload = t.to_msgpack_ext
96
+ unpacked = MessagePack::Timestamp.from_msgpack_ext(payload)
97
+
98
+ expect(unpacked).to eq(t)
99
+ end
100
+ end
101
+
102
+ describe 'timestamp96' do
103
+ it 'handles [-1, 0]' do
104
+ t = MessagePack::Timestamp.new(-1, 0)
105
+
106
+ payload = t.to_msgpack_ext
107
+ unpacked = MessagePack::Timestamp.from_msgpack_ext(payload)
108
+
109
+ expect(unpacked).to eq(t)
110
+ end
111
+
112
+ it 'handles [-1, 999_999_999]' do
113
+ t = MessagePack::Timestamp.new(-1, 999_999_999)
114
+
115
+ payload = t.to_msgpack_ext
116
+ unpacked = MessagePack::Timestamp.from_msgpack_ext(payload)
117
+
118
+ expect(unpacked).to eq(t)
119
+ end
120
+ end
121
+ end
@@ -18,13 +18,26 @@ describe MessagePack::Unpacker do
18
18
  it 'gets options to specify how to unpack values' do
19
19
  u1 = MessagePack::Unpacker.new
20
20
  u1.symbolize_keys?.should == false
21
+ u1.freeze?.should == false
21
22
  u1.allow_unknown_ext?.should == false
22
23
 
23
- u2 = MessagePack::Unpacker.new(symbolize_keys: true, allow_unknown_ext: true)
24
+ u2 = MessagePack::Unpacker.new(symbolize_keys: true, freeze: true, allow_unknown_ext: true)
24
25
  u2.symbolize_keys?.should == true
26
+ u2.freeze?.should == true
25
27
  u2.allow_unknown_ext?.should == true
26
28
  end
27
29
 
30
+ if automatic_string_keys_deduplication?
31
+ it 'ensure string hash keys are deduplicated' do
32
+ sample_data = [{"foo" => 1}, {"foo" => 2}]
33
+ sample_packed = MessagePack.pack(sample_data).force_encoding('ASCII-8BIT')
34
+ unpacker.feed(sample_packed)
35
+ hashes = nil
36
+ unpacker.each { |obj| hashes = obj }
37
+ expect(hashes[0].keys.first).to equal(hashes[1].keys.first)
38
+ end
39
+ end
40
+
28
41
  it 'gets IO or object which has #read to read data from it' do
29
42
  sample_data = {"message" => "morning!", "num" => 1}
30
43
  sample_packed = MessagePack.pack(sample_data).force_encoding('ASCII-8BIT')
@@ -651,6 +664,88 @@ describe MessagePack::Unpacker do
651
664
  end
652
665
  end
653
666
 
667
+ context 'freeze' do
668
+ let :struct do
669
+ {'hello' => 'world', 'nested' => ['object', {'structure' => true}]}
670
+ end
671
+
672
+ let :buffer do
673
+ MessagePack.pack(struct)
674
+ end
675
+
676
+ let :unpacker do
677
+ described_class.new(:freeze => true)
678
+ end
679
+
680
+ it 'can freeze objects when using .unpack' do
681
+ parsed_struct = MessagePack.unpack(buffer, freeze: true)
682
+ parsed_struct.should == struct
683
+
684
+ parsed_struct.should be_frozen
685
+ parsed_struct['hello'].should be_frozen
686
+ parsed_struct['nested'].should be_frozen
687
+ parsed_struct['nested'][0].should be_frozen
688
+ parsed_struct['nested'][1].should be_frozen
689
+
690
+ if string_deduplication?
691
+ parsed_struct.keys[0].should be_equal('hello'.freeze)
692
+ parsed_struct.keys[1].should be_equal('nested'.freeze)
693
+ parsed_struct.values[0].should be_equal('world'.freeze)
694
+ parsed_struct.values[1][0].should be_equal('object'.freeze)
695
+ parsed_struct.values[1][1].keys[0].should be_equal('structure'.freeze)
696
+ end
697
+ end
698
+
699
+ it 'can freeze objects when using #each' do
700
+ objs = []
701
+ unpacker.feed(buffer)
702
+ unpacker.each do |obj|
703
+ objs << obj
704
+ end
705
+
706
+ parsed_struct = objs.first
707
+ parsed_struct.should == struct
708
+
709
+ parsed_struct.should be_frozen
710
+ parsed_struct['hello'].should be_frozen
711
+ parsed_struct['nested'].should be_frozen
712
+ parsed_struct['nested'][0].should be_frozen
713
+ parsed_struct['nested'][1].should be_frozen
714
+
715
+ if string_deduplication?
716
+ parsed_struct.keys[0].should be_equal('hello'.freeze)
717
+ parsed_struct.keys[1].should be_equal('nested'.freeze)
718
+ parsed_struct.values[0].should be_equal('world'.freeze)
719
+ parsed_struct.values[1][0].should be_equal('object'.freeze)
720
+ parsed_struct.values[1][1].keys[0].should be_equal('structure'.freeze)
721
+ end
722
+ end
723
+
724
+ it 'can freeze objects when using #feed_each' do
725
+ objs = []
726
+ unpacker.feed_each(buffer) do |obj|
727
+ objs << obj
728
+ end
729
+
730
+ parsed_struct = objs.first
731
+ parsed_struct.should == struct
732
+
733
+ parsed_struct.should be_frozen
734
+ parsed_struct['hello'].should be_frozen
735
+ parsed_struct['nested'].should be_frozen
736
+ parsed_struct['nested'][0].should be_frozen
737
+ parsed_struct['nested'][1].should be_frozen
738
+
739
+ if string_deduplication?
740
+ parsed_struct.keys[0].should be_equal('hello'.freeze)
741
+ parsed_struct.keys[1].should be_equal('nested'.freeze)
742
+ parsed_struct.values[0].should be_equal('world'.freeze)
743
+ parsed_struct.values[1][0].should be_equal('object'.freeze)
744
+ parsed_struct.values[1][1].keys[0].should be_equal('structure'.freeze)
745
+ end
746
+ end
747
+ end
748
+
654
749
  context 'binary encoding', :encodings do
655
750
  let :buffer do
656
751
  MessagePack.pack({'hello' => 'world', 'nested' => ['object', {'structure' => true}]})
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: msgpack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.10
4
+ version: 1.4.0
5
5
  platform: java
6
6
  authors:
7
7
  - Sadayuki Furuhashi
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2019-04-19 00:00:00.000000000 Z
13
+ date: 2021-01-27 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  requirement: !ruby/object:Gem::Requirement
@@ -43,17 +43,17 @@ dependencies:
43
43
  - !ruby/object:Gem::Dependency
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - "~>"
46
+ - - ">="
47
47
  - !ruby/object:Gem::Version
48
- version: '1.0'
48
+ version: '0'
49
49
  name: rake-compiler
50
50
  prerelease: false
51
51
  type: :development
52
52
  version_requirements: !ruby/object:Gem::Requirement
53
53
  requirements:
54
- - - "~>"
54
+ - - ">="
55
55
  - !ruby/object:Gem::Version
56
- version: '1.0'
56
+ version: '0'
57
57
  - !ruby/object:Gem::Dependency
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
@@ -113,6 +113,8 @@ files:
113
113
  - lib/msgpack/msgpack.jar
114
114
  - lib/msgpack/packer.rb
115
115
  - lib/msgpack/symbol.rb
116
+ - lib/msgpack/time.rb
117
+ - lib/msgpack/timestamp.rb
116
118
  - lib/msgpack/unpacker.rb
117
119
  - lib/msgpack/version.rb
118
120
  - spec/cases.json
@@ -136,6 +138,7 @@ files:
136
138
  - spec/packer_spec.rb
137
139
  - spec/random_compat.rb
138
140
  - spec/spec_helper.rb
141
+ - spec/timestamp_spec.rb
139
142
  - spec/unpack_spec.rb
140
143
  - spec/unpacker_spec.rb
141
144
  homepage: http://msgpack.org/
@@ -157,8 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
160
  - !ruby/object:Gem::Version
158
161
  version: '0'
159
162
  requirements: []
160
- rubyforge_project: msgpack
161
- rubygems_version: 2.7.6
163
+ rubygems_version: 3.0.3
162
164
  signing_key:
163
165
  specification_version: 4
164
166
  summary: MessagePack, a binary-based efficient data interchange format.
@@ -184,5 +186,6 @@ test_files:
184
186
  - spec/packer_spec.rb
185
187
  - spec/random_compat.rb
186
188
  - spec/spec_helper.rb
189
+ - spec/timestamp_spec.rb
187
190
  - spec/unpack_spec.rb
188
191
  - spec/unpacker_spec.rb