google-protobuf 3.0.0.alpha.3 → 3.0.0.alpha.3.1.pre
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of google-protobuf might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/ext/google/protobuf_c/defs.c +5 -3
- data/ext/google/protobuf_c/encode_decode.c +85 -75
- data/ext/google/protobuf_c/extconf.rb +1 -3
- data/ext/google/protobuf_c/message.c +9 -44
- data/ext/google/protobuf_c/protobuf.c +7 -10
- data/ext/google/protobuf_c/protobuf.h +9 -9
- data/ext/google/protobuf_c/repeated_field.c +55 -95
- data/ext/google/protobuf_c/storage.c +2 -5
- data/ext/google/protobuf_c/upb.c +248 -822
- data/ext/google/protobuf_c/upb.h +467 -511
- data/lib/google/protobuf.rb +0 -27
- data/tests/basic.rb +9 -119
- metadata +17 -27
- data/lib/google/protobuf/message_exts.rb +0 -53
- data/lib/google/protobuf/repeated_field.rb +0 -188
data/lib/google/protobuf.rb
CHANGED
@@ -28,36 +28,9 @@
|
|
28
28
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
29
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
30
|
|
31
|
-
# require mixins before we hook them into the java & c code
|
32
|
-
require 'google/protobuf/message_exts'
|
33
|
-
|
34
31
|
if RUBY_PLATFORM == "java"
|
35
32
|
require 'json'
|
36
33
|
require 'google/protobuf_java'
|
37
34
|
else
|
38
35
|
require 'google/protobuf_c'
|
39
36
|
end
|
40
|
-
|
41
|
-
require 'google/protobuf/repeated_field'
|
42
|
-
|
43
|
-
module Google
|
44
|
-
module Protobuf
|
45
|
-
|
46
|
-
def self.encode(msg)
|
47
|
-
msg.to_proto
|
48
|
-
end
|
49
|
-
|
50
|
-
def self.encode_json(msg)
|
51
|
-
msg.to_json
|
52
|
-
end
|
53
|
-
|
54
|
-
def self.decode(klass, proto)
|
55
|
-
klass.decode(proto)
|
56
|
-
end
|
57
|
-
|
58
|
-
def self.decode_json(klass, json)
|
59
|
-
klass.decode_json(json)
|
60
|
-
end
|
61
|
-
|
62
|
-
end
|
63
|
-
end
|
data/tests/basic.rb
CHANGED
@@ -154,8 +154,6 @@ module BasicTest
|
|
154
154
|
assert m.optional_bytes == "world"
|
155
155
|
m.optional_msg = TestMessage2.new(:foo => 42)
|
156
156
|
assert m.optional_msg == TestMessage2.new(:foo => 42)
|
157
|
-
m.optional_msg = nil
|
158
|
-
assert m.optional_msg == nil
|
159
157
|
end
|
160
158
|
|
161
159
|
def test_ctor_args
|
@@ -178,7 +176,7 @@ module BasicTest
|
|
178
176
|
:optional_msg => TestMessage2.new,
|
179
177
|
:repeated_string => ["hello", "there", "world"])
|
180
178
|
expected = '<BasicTest::TestMessage: optional_int32: -42, optional_int64: 0, optional_uint32: 0, optional_uint64: 0, optional_bool: false, optional_float: 0.0, optional_double: 0.0, optional_string: "", optional_bytes: "", optional_msg: <BasicTest::TestMessage2: foo: 0>, optional_enum: :A, repeated_int32: [], repeated_int64: [], repeated_uint32: [], repeated_uint64: [], repeated_bool: [], repeated_float: [], repeated_double: [], repeated_string: ["hello", "there", "world"], repeated_bytes: [], repeated_msg: [], repeated_enum: []>'
|
181
|
-
|
179
|
+
assert m.inspect == expected
|
182
180
|
end
|
183
181
|
|
184
182
|
def test_hash
|
@@ -276,7 +274,7 @@ module BasicTest
|
|
276
274
|
|
277
275
|
assert l.inspect == '[5, 2, 3, 4]'
|
278
276
|
|
279
|
-
l.
|
277
|
+
l.insert(7, 8, 9)
|
280
278
|
assert l == [5, 2, 3, 4, 7, 8, 9]
|
281
279
|
assert l.pop == 9
|
282
280
|
assert l == [5, 2, 3, 4, 7, 8]
|
@@ -316,17 +314,6 @@ module BasicTest
|
|
316
314
|
assert l4 == [0, 0, 0, 0, 0, 42, 100, 101, 102]
|
317
315
|
end
|
318
316
|
|
319
|
-
def test_parent_rptfield
|
320
|
-
#make sure we set the RepeatedField and can add to it
|
321
|
-
m = TestMessage.new
|
322
|
-
assert m.repeated_string == []
|
323
|
-
m.repeated_string << 'ok'
|
324
|
-
m.repeated_string.push('ok2')
|
325
|
-
assert m.repeated_string == ['ok', 'ok2']
|
326
|
-
m.repeated_string += ['ok3']
|
327
|
-
assert m.repeated_string == ['ok', 'ok2', 'ok3']
|
328
|
-
end
|
329
|
-
|
330
317
|
def test_rptfield_msg
|
331
318
|
l = Google::Protobuf::RepeatedField.new(:message, TestMessage)
|
332
319
|
l.push TestMessage.new
|
@@ -390,39 +377,6 @@ module BasicTest
|
|
390
377
|
end
|
391
378
|
end
|
392
379
|
|
393
|
-
def test_rptfield_array_ducktyping
|
394
|
-
l = Google::Protobuf::RepeatedField.new(:int32)
|
395
|
-
length_methods = %w(count length size)
|
396
|
-
length_methods.each do |lm|
|
397
|
-
assert l.send(lm) == 0
|
398
|
-
end
|
399
|
-
# out of bounds returns a nil
|
400
|
-
assert l[0] == nil
|
401
|
-
assert l[1] == nil
|
402
|
-
assert l[-1] == nil
|
403
|
-
l.push 4
|
404
|
-
length_methods.each do |lm|
|
405
|
-
assert l.send(lm) == 1
|
406
|
-
end
|
407
|
-
assert l[0] == 4
|
408
|
-
assert l[1] == nil
|
409
|
-
assert l[-1] == 4
|
410
|
-
assert l[-2] == nil
|
411
|
-
|
412
|
-
l.push 2
|
413
|
-
length_methods.each do |lm|
|
414
|
-
assert l.send(lm) == 2
|
415
|
-
end
|
416
|
-
assert l[0] == 4
|
417
|
-
assert l[1] == 2
|
418
|
-
assert l[2] == nil
|
419
|
-
assert l[-1] == 2
|
420
|
-
assert l[-2] == 4
|
421
|
-
assert l[-3] == nil
|
422
|
-
|
423
|
-
#adding out of scope will backfill with empty objects
|
424
|
-
end
|
425
|
-
|
426
380
|
def test_map_basic
|
427
381
|
# allowed key types:
|
428
382
|
# :int32, :int64, :uint32, :uint64, :bool, :string, :bytes.
|
@@ -758,12 +712,9 @@ module BasicTest
|
|
758
712
|
m = TestMessage.new
|
759
713
|
m.optional_string = "hello"
|
760
714
|
m.optional_int32 = 42
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
assert m.repeated_msg[-1] == tm1
|
765
|
-
m.repeated_msg.push tm2
|
766
|
-
assert m.repeated_msg[-1] == tm2
|
715
|
+
m.repeated_msg.push TestMessage2.new(:foo => 100)
|
716
|
+
m.repeated_msg.push TestMessage2.new(:foo => 200)
|
717
|
+
|
767
718
|
m2 = m.dup
|
768
719
|
assert m == m2
|
769
720
|
m.optional_int32 += 1
|
@@ -822,67 +773,6 @@ module BasicTest
|
|
822
773
|
assert m == m2
|
823
774
|
end
|
824
775
|
|
825
|
-
def test_encode_decode_helpers
|
826
|
-
m = TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2'])
|
827
|
-
json = m.to_json
|
828
|
-
m2 = TestMessage.decode_json(json)
|
829
|
-
assert m2.optional_string == 'foo'
|
830
|
-
assert m2.repeated_string == ['bar1', 'bar2']
|
831
|
-
|
832
|
-
proto = m.to_proto
|
833
|
-
m2 = TestMessage.decode(proto)
|
834
|
-
assert m2.optional_string == 'foo'
|
835
|
-
assert m2.repeated_string == ['bar1', 'bar2']
|
836
|
-
end
|
837
|
-
|
838
|
-
def test_protobuf_encode_decode_helpers
|
839
|
-
m = TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2'])
|
840
|
-
encoded_msg = Google::Protobuf.encode(m)
|
841
|
-
assert_equal m.to_proto, encoded_msg
|
842
|
-
|
843
|
-
decoded_msg = Google::Protobuf.decode(TestMessage, encoded_msg)
|
844
|
-
assert_equal TestMessage.decode(m.to_proto), decoded_msg
|
845
|
-
end
|
846
|
-
|
847
|
-
def test_protobuf_encode_decode_json_helpers
|
848
|
-
m = TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2'])
|
849
|
-
encoded_msg = Google::Protobuf.encode_json(m)
|
850
|
-
assert_equal m.to_json, encoded_msg
|
851
|
-
|
852
|
-
decoded_msg = Google::Protobuf.decode_json(TestMessage, encoded_msg)
|
853
|
-
assert_equal TestMessage.decode_json(m.to_json), decoded_msg
|
854
|
-
end
|
855
|
-
|
856
|
-
def test_to_h
|
857
|
-
m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2'])
|
858
|
-
expected_result = {
|
859
|
-
:optional_bool=>true,
|
860
|
-
:optional_bytes=>"",
|
861
|
-
:optional_double=>-10.100001,
|
862
|
-
:optional_enum=>:Default,
|
863
|
-
:optional_float=>0.0,
|
864
|
-
:optional_int32=>0,
|
865
|
-
:optional_int64=>0,
|
866
|
-
:optional_msg=>nil,
|
867
|
-
:optional_string=>"foo",
|
868
|
-
:optional_uint32=>0,
|
869
|
-
:optional_uint64=>0,
|
870
|
-
:repeated_bool=>[],
|
871
|
-
:repeated_bytes=>[],
|
872
|
-
:repeated_double=>[],
|
873
|
-
:repeated_enum=>[],
|
874
|
-
:repeated_float=>[],
|
875
|
-
:repeated_int32=>[],
|
876
|
-
:repeated_int64=>[],
|
877
|
-
:repeated_msg=>[],
|
878
|
-
:repeated_string=>["bar1", "bar2"],
|
879
|
-
:repeated_uint32=>[],
|
880
|
-
:repeated_uint64=>[]
|
881
|
-
}
|
882
|
-
assert_equal expected_result, m.to_h
|
883
|
-
end
|
884
|
-
|
885
|
-
|
886
776
|
def test_def_errors
|
887
777
|
s = Google::Protobuf::DescriptorPool.new
|
888
778
|
assert_raise TypeError do
|
@@ -937,6 +827,7 @@ module BasicTest
|
|
937
827
|
assert m['a.b'] == 4
|
938
828
|
end
|
939
829
|
|
830
|
+
|
940
831
|
def test_int_ranges
|
941
832
|
m = TestMessage.new
|
942
833
|
|
@@ -1042,6 +933,7 @@ module BasicTest
|
|
1042
933
|
assert_raise RangeError do
|
1043
934
|
m.optional_uint64 = 1.5
|
1044
935
|
end
|
936
|
+
|
1045
937
|
end
|
1046
938
|
|
1047
939
|
def test_stress_test
|
@@ -1096,8 +988,7 @@ module BasicTest
|
|
1096
988
|
end
|
1097
989
|
|
1098
990
|
def test_json
|
1099
|
-
|
1100
|
-
return if RUBY_PLATFORM == "java"
|
991
|
+
skip("Unimplemented") if RUBY_PLATFORM == "java"
|
1101
992
|
m = TestMessage.new(:optional_int32 => 1234,
|
1102
993
|
:optional_int64 => -0x1_0000_0000,
|
1103
994
|
:optional_uint32 => 0x8000_0000,
|
@@ -1128,8 +1019,7 @@ module BasicTest
|
|
1128
1019
|
end
|
1129
1020
|
|
1130
1021
|
def test_json_maps
|
1131
|
-
|
1132
|
-
return if RUBY_PLATFORM == "java"
|
1022
|
+
skip("Unimplemented") if RUBY_PLATFORM == "java"
|
1133
1023
|
m = MapMessage.new(:map_string_int32 => {"a" => 1})
|
1134
1024
|
expected = '{"map_string_int32":{"a":1},"map_string_msg":{}}'
|
1135
1025
|
assert MapMessage.encode_json(m) == expected
|
metadata
CHANGED
@@ -1,62 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google-protobuf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.0.alpha.3
|
5
|
-
prerelease: 6
|
4
|
+
version: 3.0.0.alpha.3.1.pre
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Protobuf Authors
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2015-
|
11
|
+
date: 2015-04-13 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rake-compiler
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: test-unit
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - ">="
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - ">="
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rubygems-tasks
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - ">="
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: '0'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - ">="
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '0'
|
62
55
|
description: Protocol Buffers are Google's data interchange format.
|
@@ -66,11 +59,9 @@ extensions:
|
|
66
59
|
- ext/google/protobuf_c/extconf.rb
|
67
60
|
extra_rdoc_files: []
|
68
61
|
files:
|
69
|
-
- lib/google/protobuf.rb
|
70
|
-
- lib/google/protobuf/message_exts.rb
|
71
|
-
- lib/google/protobuf/repeated_field.rb
|
72
62
|
- ext/google/protobuf_c/defs.c
|
73
63
|
- ext/google/protobuf_c/encode_decode.c
|
64
|
+
- ext/google/protobuf_c/extconf.rb
|
74
65
|
- ext/google/protobuf_c/map.c
|
75
66
|
- ext/google/protobuf_c/message.c
|
76
67
|
- ext/google/protobuf_c/protobuf.c
|
@@ -79,34 +70,33 @@ files:
|
|
79
70
|
- ext/google/protobuf_c/storage.c
|
80
71
|
- ext/google/protobuf_c/upb.c
|
81
72
|
- ext/google/protobuf_c/upb.h
|
73
|
+
- lib/google/protobuf.rb
|
82
74
|
- tests/basic.rb
|
83
|
-
- tests/stress.rb
|
84
75
|
- tests/generated_code_test.rb
|
85
|
-
-
|
86
|
-
homepage:
|
76
|
+
- tests/stress.rb
|
77
|
+
homepage:
|
87
78
|
licenses:
|
88
79
|
- BSD
|
80
|
+
metadata: {}
|
89
81
|
post_install_message:
|
90
82
|
rdoc_options: []
|
91
83
|
require_paths:
|
92
84
|
- lib
|
93
85
|
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
-
none: false
|
95
86
|
requirements:
|
96
|
-
- -
|
87
|
+
- - ">="
|
97
88
|
- !ruby/object:Gem::Version
|
98
89
|
version: '0'
|
99
90
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
-
none: false
|
101
91
|
requirements:
|
102
|
-
- -
|
92
|
+
- - ">"
|
103
93
|
- !ruby/object:Gem::Version
|
104
94
|
version: 1.3.1
|
105
95
|
requirements: []
|
106
96
|
rubyforge_project:
|
107
|
-
rubygems_version:
|
97
|
+
rubygems_version: 2.4.6
|
108
98
|
signing_key:
|
109
|
-
specification_version:
|
99
|
+
specification_version: 4
|
110
100
|
summary: Protocol Buffers
|
111
101
|
test_files:
|
112
102
|
- tests/basic.rb
|
@@ -1,53 +0,0 @@
|
|
1
|
-
# Protocol Buffers - Google's data interchange format
|
2
|
-
# Copyright 2008 Google Inc. All rights reserved.
|
3
|
-
# https://developers.google.com/protocol-buffers/
|
4
|
-
#
|
5
|
-
# Redistribution and use in source and binary forms, with or without
|
6
|
-
# modification, are permitted provided that the following conditions are
|
7
|
-
# met:
|
8
|
-
#
|
9
|
-
# * Redistributions of source code must retain the above copyright
|
10
|
-
# notice, this list of conditions and the following disclaimer.
|
11
|
-
# * Redistributions in binary form must reproduce the above
|
12
|
-
# copyright notice, this list of conditions and the following disclaimer
|
13
|
-
# in the documentation and/or other materials provided with the
|
14
|
-
# distribution.
|
15
|
-
# * Neither the name of Google Inc. nor the names of its
|
16
|
-
# contributors may be used to endorse or promote products derived from
|
17
|
-
# this software without specific prior written permission.
|
18
|
-
#
|
19
|
-
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
20
|
-
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
21
|
-
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
22
|
-
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
23
|
-
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
24
|
-
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
25
|
-
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
26
|
-
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
27
|
-
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
28
|
-
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
|
-
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
-
|
31
|
-
module Google
|
32
|
-
module Protobuf
|
33
|
-
module MessageExts
|
34
|
-
|
35
|
-
#this is only called in jruby; mri loades the ClassMethods differently
|
36
|
-
def self.included(klass)
|
37
|
-
klass.extend(ClassMethods)
|
38
|
-
end
|
39
|
-
|
40
|
-
module ClassMethods
|
41
|
-
end
|
42
|
-
|
43
|
-
def to_json
|
44
|
-
self.class.encode_json(self)
|
45
|
-
end
|
46
|
-
|
47
|
-
def to_proto
|
48
|
-
self.class.encode(self)
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
@@ -1,188 +0,0 @@
|
|
1
|
-
# Protocol Buffers - Google's data interchange format
|
2
|
-
# Copyright 2008 Google Inc. All rights reserved.
|
3
|
-
# https://developers.google.com/protocol-buffers/
|
4
|
-
#
|
5
|
-
# Redistribution and use in source and binary forms, with or without
|
6
|
-
# modification, are permitted provided that the following conditions are
|
7
|
-
# met:
|
8
|
-
#
|
9
|
-
# * Redistributions of source code must retain the above copyright
|
10
|
-
# notice, this list of conditions and the following disclaimer.
|
11
|
-
# * Redistributions in binary form must reproduce the above
|
12
|
-
# copyright notice, this list of conditions and the following disclaimer
|
13
|
-
# in the documentation and/or other materials provided with the
|
14
|
-
# distribution.
|
15
|
-
# * Neither the name of Google Inc. nor the names of its
|
16
|
-
# contributors may be used to endorse or promote products derived from
|
17
|
-
# this software without specific prior written permission.
|
18
|
-
#
|
19
|
-
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
20
|
-
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
21
|
-
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
22
|
-
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
23
|
-
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
24
|
-
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
25
|
-
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
26
|
-
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
27
|
-
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
28
|
-
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
|
-
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
-
|
31
|
-
require 'forwardable'
|
32
|
-
|
33
|
-
#
|
34
|
-
# This class makes RepeatedField act (almost-) like a Ruby Array.
|
35
|
-
# It has convenience methods that extend the core C or Java based
|
36
|
-
# methods.
|
37
|
-
#
|
38
|
-
# This is a best-effort to mirror Array behavior. Two comments:
|
39
|
-
# 1) patches always welcome :)
|
40
|
-
# 2) if performance is an issue, feel free to rewrite the method
|
41
|
-
# in jruby and C. The source code has plenty of examples
|
42
|
-
#
|
43
|
-
# KNOWN ISSUES
|
44
|
-
# - #[]= doesn't allow less used approaches such as `arr[1, 2] = 'fizz'`
|
45
|
-
# - #concat should return the orig array
|
46
|
-
# - #push should accept multiple arguments and push them all at the same time
|
47
|
-
#
|
48
|
-
module Google
|
49
|
-
module Protobuf
|
50
|
-
class RepeatedField
|
51
|
-
extend Forwardable
|
52
|
-
|
53
|
-
# methods defined in C or Java:
|
54
|
-
# +
|
55
|
-
# [], at
|
56
|
-
# []=
|
57
|
-
# concat
|
58
|
-
# clear
|
59
|
-
# dup, clone
|
60
|
-
# each
|
61
|
-
# push, <<
|
62
|
-
# replace
|
63
|
-
# length, size
|
64
|
-
# ==
|
65
|
-
# to_ary, to_a
|
66
|
-
# also all enumerable
|
67
|
-
#
|
68
|
-
# NOTE: using delegators rather than method_missing to make the
|
69
|
-
# relationship explicit instead of implicit
|
70
|
-
def_delegators :to_ary,
|
71
|
-
:&, :*, :-, :'<=>',
|
72
|
-
:assoc, :bsearch, :combination, :compact, :count, :cycle,
|
73
|
-
:drop, :drop_while, :eql?, :fetch, :find_index, :flatten,
|
74
|
-
:include?, :index, :inspect, :join,
|
75
|
-
:pack, :permutation, :product, :pretty_print, :pretty_print_cycle,
|
76
|
-
:rassoc, :repeated_combination, :repeated_permutation, :reverse,
|
77
|
-
:rindex, :rotate, :sample, :shuffle, :shelljoin, :slice,
|
78
|
-
:to_s, :transpose, :uniq, :|
|
79
|
-
|
80
|
-
|
81
|
-
def first(n=nil)
|
82
|
-
n ? self[0..n] : self[0]
|
83
|
-
end
|
84
|
-
|
85
|
-
|
86
|
-
def last(n=nil)
|
87
|
-
n ? self[(self.size-n-1)..-1] : self[-1]
|
88
|
-
end
|
89
|
-
|
90
|
-
|
91
|
-
def pop(n=nil)
|
92
|
-
if n
|
93
|
-
results = []
|
94
|
-
n.times{ results << pop_one }
|
95
|
-
return results
|
96
|
-
else
|
97
|
-
return pop_one
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
|
102
|
-
def empty?
|
103
|
-
self.size == 0
|
104
|
-
end
|
105
|
-
|
106
|
-
# array aliases into enumerable
|
107
|
-
alias_method :each_index, :each_with_index
|
108
|
-
alias_method :slice, :[]
|
109
|
-
alias_method :values_at, :select
|
110
|
-
alias_method :map, :collect
|
111
|
-
|
112
|
-
|
113
|
-
class << self
|
114
|
-
def define_array_wrapper_method(method_name)
|
115
|
-
define_method(method_name) do |*args, &block|
|
116
|
-
arr = self.to_a
|
117
|
-
result = arr.send(method_name, *args)
|
118
|
-
self.replace(arr)
|
119
|
-
return result if result
|
120
|
-
return block ? block.call : result
|
121
|
-
end
|
122
|
-
end
|
123
|
-
private :define_array_wrapper_method
|
124
|
-
|
125
|
-
|
126
|
-
def define_array_wrapper_with_result_method(method_name)
|
127
|
-
define_method(method_name) do |*args, &block|
|
128
|
-
# result can be an Enumerator, Array, or nil
|
129
|
-
# Enumerator can sometimes be returned if a block is an optional argument and it is not passed in
|
130
|
-
# nil usually specifies that no change was made
|
131
|
-
result = self.to_a.send(method_name, *args, &block)
|
132
|
-
if result
|
133
|
-
new_arr = result.to_a
|
134
|
-
self.replace(new_arr)
|
135
|
-
if result.is_a?(Enumerator)
|
136
|
-
# generate a fresh enum; rewinding the exiting one, in Ruby 2.2, will
|
137
|
-
# reset the enum with the same length, but all the #next calls will
|
138
|
-
# return nil
|
139
|
-
result = new_arr.to_enum
|
140
|
-
# generate a wrapper enum so any changes which occur by a chained
|
141
|
-
# enum can be captured
|
142
|
-
ie = ProxyingEnumerator.new(self, result)
|
143
|
-
result = ie.to_enum
|
144
|
-
end
|
145
|
-
end
|
146
|
-
result
|
147
|
-
end
|
148
|
-
end
|
149
|
-
private :define_array_wrapper_with_result_method
|
150
|
-
end
|
151
|
-
|
152
|
-
|
153
|
-
%w(delete delete_at delete_if shift slice! unshift).each do |method_name|
|
154
|
-
define_array_wrapper_method(method_name)
|
155
|
-
end
|
156
|
-
|
157
|
-
|
158
|
-
%w(collect! compact! fill flatten! insert reverse!
|
159
|
-
rotate! select! shuffle! sort! sort_by! uniq!).each do |method_name|
|
160
|
-
define_array_wrapper_with_result_method(method_name)
|
161
|
-
end
|
162
|
-
alias_method :keep_if, :select!
|
163
|
-
alias_method :map!, :collect!
|
164
|
-
alias_method :reject!, :delete_if
|
165
|
-
|
166
|
-
|
167
|
-
# propagates changes made by user of enumerator back to the original repeated field.
|
168
|
-
# This only applies in cases where the calling function which created the enumerator,
|
169
|
-
# such as #sort!, modifies itself rather than a new array, such as #sort
|
170
|
-
class ProxyingEnumerator < Struct.new(:repeated_field, :external_enumerator)
|
171
|
-
def each(*args, &block)
|
172
|
-
results = []
|
173
|
-
external_enumerator.each_with_index do |val, i|
|
174
|
-
result = yield(val)
|
175
|
-
results << result
|
176
|
-
#nil means no change occured from yield; usually occurs when #to_a is called
|
177
|
-
if result
|
178
|
-
repeated_field[i] = result if result != val
|
179
|
-
end
|
180
|
-
end
|
181
|
-
results
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
|
186
|
-
end
|
187
|
-
end
|
188
|
-
end
|