google-protobuf 3.3.0-java → 3.5.0.pre-java
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 +4 -4
- data/lib/google/protobuf.rb +76 -0
- data/lib/google/protobuf/message_exts.rb +53 -0
- data/lib/google/protobuf/repeated_field.rb +188 -0
- data/lib/google/protobuf/well_known_types.rb +212 -0
- data/lib/google/protobuf_java.jar +0 -0
- data/tests/basic.rb +196 -7
- metadata +8 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 68a5655914bf8d448c5fded811b0616ffe6a5831
|
4
|
+
data.tar.gz: caa6f9c4e3a3976ab51a793a02d1132374d1db01
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b27301ac13090f9ceb087096fdca7d51d41718bbb384a488cb89d270d9319352ff2c61a0d0d7187c212018262f44f6f2cdd819b29136134e5bc368aa32621295
|
7
|
+
data.tar.gz: fac16a5c831071595513e381c0ec138099e745dcada0001b24865afb1cfa48d77b8304030241b1c6d8cb5a2f242d11912bcadc421696c1ccd7f131570a52c6d8
|
@@ -0,0 +1,76 @@
|
|
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 mixins before we hook them into the java & c code
|
32
|
+
require 'google/protobuf/message_exts'
|
33
|
+
|
34
|
+
# We define these before requiring the platform-specific modules.
|
35
|
+
# That way the module init can grab references to these.
|
36
|
+
module Google
|
37
|
+
module Protobuf
|
38
|
+
class Error < StandardError; end
|
39
|
+
class ParseError < Error; end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
if RUBY_PLATFORM == "java"
|
44
|
+
require 'json'
|
45
|
+
require 'google/protobuf_java'
|
46
|
+
else
|
47
|
+
begin
|
48
|
+
require "google/#{RUBY_VERSION.sub(/\.\d+$/, '')}/protobuf_c"
|
49
|
+
rescue LoadError
|
50
|
+
require 'google/protobuf_c'
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
require 'google/protobuf/repeated_field'
|
55
|
+
|
56
|
+
module Google
|
57
|
+
module Protobuf
|
58
|
+
|
59
|
+
def self.encode(msg)
|
60
|
+
msg.to_proto
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.encode_json(msg)
|
64
|
+
msg.to_json
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.decode(klass, proto)
|
68
|
+
klass.decode(proto)
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.decode_json(klass, json)
|
72
|
+
klass.decode_json(json)
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,53 @@
|
|
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
|
@@ -0,0 +1,188 @@
|
|
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, :bsearch_index, :combination, :compact, :count,
|
73
|
+
:cycle, :dig, :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,
|
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 occurred 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
|
@@ -0,0 +1,212 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
# Protocol Buffers - Google's data interchange format
|
3
|
+
# Copyright 2008 Google Inc. All rights reserved.
|
4
|
+
# https://developers.google.com/protocol-buffers/
|
5
|
+
#
|
6
|
+
# Redistribution and use in source and binary forms, with or without
|
7
|
+
# modification, are permitted provided that the following conditions are
|
8
|
+
# met:
|
9
|
+
#
|
10
|
+
# * Redistributions of source code must retain the above copyright
|
11
|
+
# notice, this list of conditions and the following disclaimer.
|
12
|
+
# * Redistributions in binary form must reproduce the above
|
13
|
+
# copyright notice, this list of conditions and the following disclaimer
|
14
|
+
# in the documentation and/or other materials provided with the
|
15
|
+
# distribution.
|
16
|
+
# * Neither the name of Google Inc. nor the names of its
|
17
|
+
# contributors may be used to endorse or promote products derived from
|
18
|
+
# this software without specific prior written permission.
|
19
|
+
#
|
20
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
21
|
+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
22
|
+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
23
|
+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
24
|
+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
25
|
+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
26
|
+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
27
|
+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
28
|
+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
29
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
30
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
|
32
|
+
require 'google/protobuf/any_pb'
|
33
|
+
require 'google/protobuf/duration_pb'
|
34
|
+
require 'google/protobuf/field_mask_pb'
|
35
|
+
require 'google/protobuf/struct_pb'
|
36
|
+
require 'google/protobuf/timestamp_pb'
|
37
|
+
|
38
|
+
module Google
|
39
|
+
module Protobuf
|
40
|
+
|
41
|
+
Any.class_eval do
|
42
|
+
def pack(msg, type_url_prefix='type.googleapis.com/')
|
43
|
+
if type_url_prefix.empty? or type_url_prefix[-1] != '/' then
|
44
|
+
self.type_url = "#{type_url_prefix}/#{msg.class.descriptor.name}"
|
45
|
+
else
|
46
|
+
self.type_url = "#{type_url_prefix}#{msg.class.descriptor.name}"
|
47
|
+
end
|
48
|
+
self.value = msg.to_proto
|
49
|
+
end
|
50
|
+
|
51
|
+
def unpack(klass)
|
52
|
+
if self.is(klass) then
|
53
|
+
klass.decode(self.value)
|
54
|
+
else
|
55
|
+
nil
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def type_name
|
60
|
+
return self.type_url.split("/")[-1]
|
61
|
+
end
|
62
|
+
|
63
|
+
def is(klass)
|
64
|
+
return self.type_name == klass.descriptor.name
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
Timestamp.class_eval do
|
69
|
+
def to_time
|
70
|
+
Time.at(self.to_f)
|
71
|
+
end
|
72
|
+
|
73
|
+
def from_time(time)
|
74
|
+
self.seconds = time.to_i
|
75
|
+
self.nanos = time.nsec
|
76
|
+
end
|
77
|
+
|
78
|
+
def to_i
|
79
|
+
self.seconds
|
80
|
+
end
|
81
|
+
|
82
|
+
def to_f
|
83
|
+
self.seconds + (self.nanos.quo(1_000_000_000))
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
Duration.class_eval do
|
88
|
+
def to_f
|
89
|
+
self.seconds + (self.nanos.to_f / 1_000_000_000)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
class UnexpectedStructType < Google::Protobuf::Error; end
|
94
|
+
|
95
|
+
Value.class_eval do
|
96
|
+
def to_ruby(recursive = false)
|
97
|
+
case self.kind
|
98
|
+
when :struct_value
|
99
|
+
if recursive
|
100
|
+
self.struct_value.to_h
|
101
|
+
else
|
102
|
+
self.struct_value
|
103
|
+
end
|
104
|
+
when :list_value
|
105
|
+
if recursive
|
106
|
+
self.list_value.to_a
|
107
|
+
else
|
108
|
+
self.list_value
|
109
|
+
end
|
110
|
+
when :null_value
|
111
|
+
nil
|
112
|
+
when :number_value
|
113
|
+
self.number_value
|
114
|
+
when :string_value
|
115
|
+
self.string_value
|
116
|
+
when :bool_value
|
117
|
+
self.bool_value
|
118
|
+
else
|
119
|
+
raise UnexpectedStructType
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def from_ruby(value)
|
124
|
+
case value
|
125
|
+
when NilClass
|
126
|
+
self.null_value = 0
|
127
|
+
when Numeric
|
128
|
+
self.number_value = value
|
129
|
+
when String
|
130
|
+
self.string_value = value
|
131
|
+
when TrueClass
|
132
|
+
self.bool_value = true
|
133
|
+
when FalseClass
|
134
|
+
self.bool_value = false
|
135
|
+
when Struct
|
136
|
+
self.struct_value = value
|
137
|
+
when Hash
|
138
|
+
self.struct_value = Struct.from_hash(value)
|
139
|
+
when ListValue
|
140
|
+
self.list_value = value
|
141
|
+
when Array
|
142
|
+
self.list_value = ListValue.from_a(value)
|
143
|
+
else
|
144
|
+
raise UnexpectedStructType
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
Struct.class_eval do
|
150
|
+
def [](key)
|
151
|
+
self.fields[key].to_ruby
|
152
|
+
end
|
153
|
+
|
154
|
+
def []=(key, value)
|
155
|
+
unless key.is_a?(String)
|
156
|
+
raise UnexpectedStructType, "Struct keys must be strings."
|
157
|
+
end
|
158
|
+
self.fields[key] ||= Google::Protobuf::Value.new
|
159
|
+
self.fields[key].from_ruby(value)
|
160
|
+
end
|
161
|
+
|
162
|
+
def to_h
|
163
|
+
ret = {}
|
164
|
+
self.fields.each { |key, val| ret[key] = val.to_ruby(true) }
|
165
|
+
ret
|
166
|
+
end
|
167
|
+
|
168
|
+
def self.from_hash(hash)
|
169
|
+
ret = Struct.new
|
170
|
+
hash.each { |key, val| ret[key] = val }
|
171
|
+
ret
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
ListValue.class_eval do
|
176
|
+
include Enumerable
|
177
|
+
|
178
|
+
def length
|
179
|
+
self.values.length
|
180
|
+
end
|
181
|
+
|
182
|
+
def [](index)
|
183
|
+
self.values[index].to_ruby
|
184
|
+
end
|
185
|
+
|
186
|
+
def []=(index, value)
|
187
|
+
self.values[index].from_ruby(value)
|
188
|
+
end
|
189
|
+
|
190
|
+
def <<(value)
|
191
|
+
wrapper = Google::Protobuf::Value.new
|
192
|
+
wrapper.from_ruby(value)
|
193
|
+
self.values << wrapper
|
194
|
+
end
|
195
|
+
|
196
|
+
def each
|
197
|
+
self.values.each { |x| yield(x.to_ruby) }
|
198
|
+
end
|
199
|
+
|
200
|
+
def to_a
|
201
|
+
self.values.map { |x| x.to_ruby(true) }
|
202
|
+
end
|
203
|
+
|
204
|
+
def self.from_a(arr)
|
205
|
+
ret = ListValue.new
|
206
|
+
arr.each { |val| ret << val }
|
207
|
+
ret
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
end
|
212
|
+
end
|
Binary file
|
data/tests/basic.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
#!/usr/bin/ruby
|
2
2
|
|
3
3
|
require 'google/protobuf'
|
4
|
+
require 'json'
|
4
5
|
require 'test/unit'
|
5
6
|
|
6
7
|
# ------------- generated code --------------
|
@@ -50,6 +51,17 @@ module BasicTest
|
|
50
51
|
optional :foo, :int32, 1
|
51
52
|
end
|
52
53
|
|
54
|
+
add_message "TestEmbeddedMessageParent" do
|
55
|
+
optional :child_msg, :message, 1, "TestEmbeddedMessageChild"
|
56
|
+
optional :number, :int32, 2
|
57
|
+
|
58
|
+
repeated :repeated_msg, :message, 3, "TestEmbeddedMessageChild"
|
59
|
+
repeated :repeated_number, :int32, 4
|
60
|
+
end
|
61
|
+
add_message "TestEmbeddedMessageChild" do
|
62
|
+
optional :sub_child, :message, 1, "TestMessage"
|
63
|
+
end
|
64
|
+
|
53
65
|
add_message "Recursive1" do
|
54
66
|
optional :foo, :message, 1, "Recursive2"
|
55
67
|
end
|
@@ -95,13 +107,25 @@ module BasicTest
|
|
95
107
|
optional :d, :enum, 4, "TestEnum"
|
96
108
|
end
|
97
109
|
end
|
110
|
+
|
111
|
+
add_message "repro.Outer" do
|
112
|
+
map :items, :int32, :message, 1, "repro.Inner"
|
113
|
+
end
|
114
|
+
|
115
|
+
add_message "repro.Inner" do
|
116
|
+
end
|
98
117
|
end
|
99
118
|
|
119
|
+
|
120
|
+
Outer = pool.lookup("repro.Outer").msgclass
|
121
|
+
Inner = pool.lookup("repro.Inner").msgclass
|
100
122
|
Foo = pool.lookup("Foo").msgclass
|
101
123
|
Bar = pool.lookup("Bar").msgclass
|
102
124
|
Baz = pool.lookup("Baz").msgclass
|
103
125
|
TestMessage = pool.lookup("TestMessage").msgclass
|
104
126
|
TestMessage2 = pool.lookup("TestMessage2").msgclass
|
127
|
+
TestEmbeddedMessageParent = pool.lookup("TestEmbeddedMessageParent").msgclass
|
128
|
+
TestEmbeddedMessageChild = pool.lookup("TestEmbeddedMessageChild").msgclass
|
105
129
|
Recursive1 = pool.lookup("Recursive1").msgclass
|
106
130
|
Recursive2 = pool.lookup("Recursive2").msgclass
|
107
131
|
TestEnum = pool.lookup("TestEnum").enummodule
|
@@ -150,12 +174,18 @@ module BasicTest
|
|
150
174
|
m.optional_double = 0.5
|
151
175
|
m.optional_string = "hello"
|
152
176
|
assert m.optional_string == "hello"
|
177
|
+
m.optional_string = :hello
|
178
|
+
assert m.optional_string == "hello"
|
153
179
|
m.optional_bytes = "world".encode!('ASCII-8BIT')
|
154
180
|
assert m.optional_bytes == "world"
|
155
181
|
m.optional_msg = TestMessage2.new(:foo => 42)
|
156
182
|
assert m.optional_msg == TestMessage2.new(:foo => 42)
|
157
183
|
m.optional_msg = nil
|
158
184
|
assert m.optional_msg == nil
|
185
|
+
m.optional_enum = :C
|
186
|
+
assert m.optional_enum == :C
|
187
|
+
m.optional_enum = 'C'
|
188
|
+
assert m.optional_enum == :C
|
159
189
|
end
|
160
190
|
|
161
191
|
def test_ctor_args
|
@@ -172,6 +202,34 @@ module BasicTest
|
|
172
202
|
assert m.repeated_string[2] == "world"
|
173
203
|
end
|
174
204
|
|
205
|
+
def test_ctor_string_symbol_args
|
206
|
+
m = TestMessage.new(:optional_enum => 'C', :repeated_enum => ['A', 'B'])
|
207
|
+
assert_equal :C, m.optional_enum
|
208
|
+
assert_equal [:A, :B], m.repeated_enum
|
209
|
+
|
210
|
+
m = TestMessage.new(:optional_string => :foo, :repeated_string => [:foo, :bar])
|
211
|
+
assert_equal 'foo', m.optional_string
|
212
|
+
assert_equal ['foo', 'bar'], m.repeated_string
|
213
|
+
end
|
214
|
+
|
215
|
+
def test_embeddedmsg_hash_init
|
216
|
+
m = TestEmbeddedMessageParent.new(:child_msg => {sub_child: {optional_int32: 1}},
|
217
|
+
:number => 2,
|
218
|
+
:repeated_msg => [{sub_child: {optional_int32: 3}}],
|
219
|
+
:repeated_number => [10, 20, 30])
|
220
|
+
|
221
|
+
assert_equal 2, m.number
|
222
|
+
assert_equal [10, 20, 30], m.repeated_number
|
223
|
+
|
224
|
+
assert_not_nil m.child_msg
|
225
|
+
assert_not_nil m.child_msg.sub_child
|
226
|
+
assert_equal m.child_msg.sub_child.optional_int32, 1
|
227
|
+
|
228
|
+
assert_not_nil m.repeated_msg
|
229
|
+
assert_equal 1, m.repeated_msg.length
|
230
|
+
assert_equal 3, m.repeated_msg.first.sub_child.optional_int32
|
231
|
+
end
|
232
|
+
|
175
233
|
def test_inspect
|
176
234
|
m = TestMessage.new(:optional_int32 => -42,
|
177
235
|
:optional_enum => :A,
|
@@ -603,7 +661,7 @@ module BasicTest
|
|
603
661
|
assert_raise RangeError do
|
604
662
|
m["z"] = :Z
|
605
663
|
end
|
606
|
-
assert_raise
|
664
|
+
assert_raise RangeError do
|
607
665
|
m["z"] = "z"
|
608
666
|
end
|
609
667
|
end
|
@@ -674,6 +732,21 @@ module BasicTest
|
|
674
732
|
m.map_string_int32['aaa'] = 3
|
675
733
|
end
|
676
734
|
|
735
|
+
def test_concurrent_decoding
|
736
|
+
o = Outer.new
|
737
|
+
o.items[0] = Inner.new
|
738
|
+
raw = Outer.encode(o)
|
739
|
+
|
740
|
+
thds = 2.times.map do
|
741
|
+
Thread.new do
|
742
|
+
100000.times do
|
743
|
+
assert_equal o, Outer.decode(raw)
|
744
|
+
end
|
745
|
+
end
|
746
|
+
end
|
747
|
+
thds.map(&:join)
|
748
|
+
end
|
749
|
+
|
677
750
|
def test_map_encode_decode
|
678
751
|
m = MapMessage.new(
|
679
752
|
:map_string_int32 => {"a" => 1, "b" => 2},
|
@@ -901,7 +974,7 @@ module BasicTest
|
|
901
974
|
end
|
902
975
|
|
903
976
|
def test_to_h
|
904
|
-
m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2'])
|
977
|
+
m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2'], :repeated_msg => [TestMessage2.new(:foo => 100)])
|
905
978
|
expected_result = {
|
906
979
|
:optional_bool=>true,
|
907
980
|
:optional_bytes=>"",
|
@@ -921,7 +994,7 @@ module BasicTest
|
|
921
994
|
:repeated_float=>[],
|
922
995
|
:repeated_int32=>[],
|
923
996
|
:repeated_int64=>[],
|
924
|
-
:repeated_msg=>[],
|
997
|
+
:repeated_msg=>[{:foo => 100}],
|
925
998
|
:repeated_string=>["bar1", "bar2"],
|
926
999
|
:repeated_uint32=>[],
|
927
1000
|
:repeated_uint64=>[]
|
@@ -1173,6 +1246,8 @@ module BasicTest
|
|
1173
1246
|
|
1174
1247
|
json_text = TestMessage.encode_json(m)
|
1175
1248
|
m2 = TestMessage.decode_json(json_text)
|
1249
|
+
puts m.inspect
|
1250
|
+
puts m2.inspect
|
1176
1251
|
assert m == m2
|
1177
1252
|
|
1178
1253
|
# Crash case from GitHub issue 283.
|
@@ -1184,21 +1259,135 @@ module BasicTest
|
|
1184
1259
|
Foo.encode_json(Foo.new(bar: bar, baz: [baz1, baz2]))
|
1185
1260
|
end
|
1186
1261
|
|
1262
|
+
def test_json_emit_defaults
|
1263
|
+
# TODO: Fix JSON in JRuby version.
|
1264
|
+
return if RUBY_PLATFORM == "java"
|
1265
|
+
m = TestMessage.new
|
1266
|
+
|
1267
|
+
expected = {
|
1268
|
+
optionalInt32: 0,
|
1269
|
+
optionalInt64: 0,
|
1270
|
+
optionalUint32: 0,
|
1271
|
+
optionalUint64: 0,
|
1272
|
+
optionalBool: false,
|
1273
|
+
optionalFloat: 0,
|
1274
|
+
optionalDouble: 0,
|
1275
|
+
optionalString: "",
|
1276
|
+
optionalBytes: "",
|
1277
|
+
optionalEnum: "Default",
|
1278
|
+
repeatedInt32: [],
|
1279
|
+
repeatedInt64: [],
|
1280
|
+
repeatedUint32: [],
|
1281
|
+
repeatedUint64: [],
|
1282
|
+
repeatedBool: [],
|
1283
|
+
repeatedFloat: [],
|
1284
|
+
repeatedDouble: [],
|
1285
|
+
repeatedString: [],
|
1286
|
+
repeatedBytes: [],
|
1287
|
+
repeatedMsg: [],
|
1288
|
+
repeatedEnum: []
|
1289
|
+
}
|
1290
|
+
|
1291
|
+
actual = TestMessage.encode_json(m, :emit_defaults => true)
|
1292
|
+
|
1293
|
+
assert JSON.parse(actual, :symbolize_names => true) == expected
|
1294
|
+
end
|
1295
|
+
|
1296
|
+
def test_json_emit_defaults_submsg
|
1297
|
+
# TODO: Fix JSON in JRuby version.
|
1298
|
+
return if RUBY_PLATFORM == "java"
|
1299
|
+
m = TestMessage.new(optional_msg: TestMessage2.new)
|
1300
|
+
|
1301
|
+
expected = {
|
1302
|
+
optionalInt32: 0,
|
1303
|
+
optionalInt64: 0,
|
1304
|
+
optionalUint32: 0,
|
1305
|
+
optionalUint64: 0,
|
1306
|
+
optionalBool: false,
|
1307
|
+
optionalFloat: 0,
|
1308
|
+
optionalDouble: 0,
|
1309
|
+
optionalString: "",
|
1310
|
+
optionalBytes: "",
|
1311
|
+
optionalMsg: {foo: 0},
|
1312
|
+
optionalEnum: "Default",
|
1313
|
+
repeatedInt32: [],
|
1314
|
+
repeatedInt64: [],
|
1315
|
+
repeatedUint32: [],
|
1316
|
+
repeatedUint64: [],
|
1317
|
+
repeatedBool: [],
|
1318
|
+
repeatedFloat: [],
|
1319
|
+
repeatedDouble: [],
|
1320
|
+
repeatedString: [],
|
1321
|
+
repeatedBytes: [],
|
1322
|
+
repeatedMsg: [],
|
1323
|
+
repeatedEnum: []
|
1324
|
+
}
|
1325
|
+
|
1326
|
+
actual = TestMessage.encode_json(m, :emit_defaults => true)
|
1327
|
+
|
1328
|
+
assert JSON.parse(actual, :symbolize_names => true) == expected
|
1329
|
+
end
|
1330
|
+
|
1331
|
+
def test_json_emit_defaults_repeated_submsg
|
1332
|
+
# TODO: Fix JSON in JRuby version.
|
1333
|
+
return if RUBY_PLATFORM == "java"
|
1334
|
+
m = TestMessage.new(repeated_msg: [TestMessage2.new])
|
1335
|
+
|
1336
|
+
expected = {
|
1337
|
+
optionalInt32: 0,
|
1338
|
+
optionalInt64: 0,
|
1339
|
+
optionalUint32: 0,
|
1340
|
+
optionalUint64: 0,
|
1341
|
+
optionalBool: false,
|
1342
|
+
optionalFloat: 0,
|
1343
|
+
optionalDouble: 0,
|
1344
|
+
optionalString: "",
|
1345
|
+
optionalBytes: "",
|
1346
|
+
optionalEnum: "Default",
|
1347
|
+
repeatedInt32: [],
|
1348
|
+
repeatedInt64: [],
|
1349
|
+
repeatedUint32: [],
|
1350
|
+
repeatedUint64: [],
|
1351
|
+
repeatedBool: [],
|
1352
|
+
repeatedFloat: [],
|
1353
|
+
repeatedDouble: [],
|
1354
|
+
repeatedString: [],
|
1355
|
+
repeatedBytes: [],
|
1356
|
+
repeatedMsg: [{foo: 0}],
|
1357
|
+
repeatedEnum: []
|
1358
|
+
}
|
1359
|
+
|
1360
|
+
actual = TestMessage.encode_json(m, :emit_defaults => true)
|
1361
|
+
|
1362
|
+
assert JSON.parse(actual, :symbolize_names => true) == expected
|
1363
|
+
end
|
1364
|
+
|
1187
1365
|
def test_json_maps
|
1188
1366
|
# TODO: Fix JSON in JRuby version.
|
1189
1367
|
return if RUBY_PLATFORM == "java"
|
1190
1368
|
m = MapMessage.new(:map_string_int32 => {"a" => 1})
|
1191
|
-
expected =
|
1192
|
-
expected_preserve =
|
1193
|
-
assert MapMessage.encode_json(m) == expected
|
1369
|
+
expected = {mapStringInt32: {a: 1}, mapStringMsg: {}}
|
1370
|
+
expected_preserve = {map_string_int32: {a: 1}, map_string_msg: {}}
|
1371
|
+
assert JSON.parse(MapMessage.encode_json(m), :symbolize_names => true) == expected
|
1194
1372
|
|
1195
1373
|
json = MapMessage.encode_json(m, :preserve_proto_fieldnames => true)
|
1196
|
-
assert json == expected_preserve
|
1374
|
+
assert JSON.parse(json, :symbolize_names => true) == expected_preserve
|
1197
1375
|
|
1198
1376
|
m2 = MapMessage.decode_json(MapMessage.encode_json(m))
|
1199
1377
|
assert m == m2
|
1200
1378
|
end
|
1201
1379
|
|
1380
|
+
def test_json_maps_emit_defaults_submsg
|
1381
|
+
# TODO: Fix JSON in JRuby version.
|
1382
|
+
return if RUBY_PLATFORM == "java"
|
1383
|
+
m = MapMessage.new(:map_string_msg => {"a" => TestMessage2.new})
|
1384
|
+
expected = {mapStringInt32: {}, mapStringMsg: {a: {foo: 0}}}
|
1385
|
+
|
1386
|
+
actual = MapMessage.encode_json(m, :emit_defaults => true)
|
1387
|
+
|
1388
|
+
assert JSON.parse(actual, :symbolize_names => true) == expected
|
1389
|
+
end
|
1390
|
+
|
1202
1391
|
def test_comparison_with_arbitrary_object
|
1203
1392
|
assert MapMessage.new != nil
|
1204
1393
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google-protobuf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.5.0.pre
|
5
5
|
platform: java
|
6
6
|
authors:
|
7
7
|
- Protobuf Authors
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-11-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
requirement: !ruby/object:Gem::Requirement
|
@@ -64,15 +64,19 @@ executables: []
|
|
64
64
|
extensions: []
|
65
65
|
extra_rdoc_files: []
|
66
66
|
files:
|
67
|
+
- lib/google/protobuf.rb
|
67
68
|
- lib/google/protobuf/any_pb.rb
|
68
69
|
- lib/google/protobuf/api_pb.rb
|
69
70
|
- lib/google/protobuf/duration_pb.rb
|
70
71
|
- lib/google/protobuf/empty_pb.rb
|
71
72
|
- lib/google/protobuf/field_mask_pb.rb
|
73
|
+
- lib/google/protobuf/message_exts.rb
|
74
|
+
- lib/google/protobuf/repeated_field.rb
|
72
75
|
- lib/google/protobuf/source_context_pb.rb
|
73
76
|
- lib/google/protobuf/struct_pb.rb
|
74
77
|
- lib/google/protobuf/timestamp_pb.rb
|
75
78
|
- lib/google/protobuf/type_pb.rb
|
79
|
+
- lib/google/protobuf/well_known_types.rb
|
76
80
|
- lib/google/protobuf/wrappers_pb.rb
|
77
81
|
- lib/google/protobuf_java.jar
|
78
82
|
- tests/basic.rb
|
@@ -93,9 +97,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
93
97
|
version: '0'
|
94
98
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
99
|
requirements:
|
96
|
-
- - "
|
100
|
+
- - ">"
|
97
101
|
- !ruby/object:Gem::Version
|
98
|
-
version:
|
102
|
+
version: 1.3.1
|
99
103
|
requirements: []
|
100
104
|
rubyforge_project:
|
101
105
|
rubygems_version: 2.4.8
|