ruby-protocol-buffers 1.4.1 → 1.5.0.beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +2 -2
- data/lib/protocol_buffers.rb +2 -0
- data/lib/protocol_buffers/compiler/file_descriptor_to_ruby.rb +52 -9
- data/lib/protocol_buffers/runtime/enum.rb +33 -1
- data/lib/protocol_buffers/runtime/field.rb +7 -7
- data/lib/protocol_buffers/runtime/message.rb +28 -0
- data/lib/protocol_buffers/runtime/rpc.rb +23 -0
- data/lib/protocol_buffers/runtime/service.rb +27 -1
- data/lib/protocol_buffers/version.rb +1 -1
- data/ruby-protocol-buffers.gemspec +2 -0
- data/spec/proto_files/enums.pb.rb +61 -0
- data/spec/proto_files/enums.proto +26 -0
- data/spec/proto_files/no_package.pb.rb +31 -0
- data/spec/proto_files/services.pb.rb +50 -0
- data/spec/proto_files/services.proto +27 -0
- data/spec/proto_files/simple.pb.rb +7 -0
- data/spec/runtime_spec.rb +214 -2
- metadata +19 -10
data/README.md
CHANGED
@@ -143,6 +143,8 @@ end
|
|
143
143
|
* imports
|
144
144
|
* nested types
|
145
145
|
* passing on unknown fields when re-serializing a message
|
146
|
+
* groups
|
147
|
+
* RPC stubbing
|
146
148
|
|
147
149
|
### Unsupported Features
|
148
150
|
|
@@ -152,8 +154,6 @@ end
|
|
152
154
|
|
153
155
|
### Probably Never to be Supported
|
154
156
|
|
155
|
-
* RPC stubbing
|
156
|
-
* deprecated protocol features (e.g. groups)
|
157
157
|
* the unsupported options (java_*, optimize_for, message_set_wire_format, deprecated)
|
158
158
|
|
159
159
|
## Authors
|
data/lib/protocol_buffers.rb
CHANGED
@@ -36,10 +36,14 @@ HEADER
|
|
36
36
|
@io.write("\n") unless descriptor.dependency.empty?
|
37
37
|
|
38
38
|
in_namespace("module", @package_modules) do
|
39
|
-
declare(descriptor.message_type, descriptor.enum_type)
|
39
|
+
declare(descriptor.package, descriptor.message_type, descriptor.enum_type)
|
40
40
|
|
41
41
|
descriptor.message_type.each do |message|
|
42
|
-
dump_message(message)
|
42
|
+
dump_message(descriptor.package, message)
|
43
|
+
end
|
44
|
+
|
45
|
+
descriptor.service.each do |service|
|
46
|
+
dump_service(descriptor.package, service)
|
43
47
|
end
|
44
48
|
end
|
45
49
|
|
@@ -47,7 +51,7 @@ HEADER
|
|
47
51
|
|
48
52
|
protected
|
49
53
|
|
50
|
-
def declare(messages, enums)
|
54
|
+
def declare(package, messages, enums)
|
51
55
|
return if messages.empty? && enums.empty?
|
52
56
|
|
53
57
|
line %{# forward declarations}
|
@@ -61,7 +65,7 @@ HEADER
|
|
61
65
|
line
|
62
66
|
line %{# enums}
|
63
67
|
enums.each do |enum|
|
64
|
-
dump_enum(enum)
|
68
|
+
dump_enum(package, enum)
|
65
69
|
end
|
66
70
|
end
|
67
71
|
end
|
@@ -119,12 +123,16 @@ HEADER
|
|
119
123
|
TYPE_SINT64 => ":sint64",
|
120
124
|
}
|
121
125
|
|
122
|
-
def dump_message(message)
|
126
|
+
def dump_message(package, message)
|
123
127
|
in_namespace("class", message.name, " < ::ProtocolBuffers::Message") do
|
124
|
-
|
128
|
+
fully_qualified_name = fully_qualified_name(package, message.name)
|
129
|
+
declare(fully_qualified_name, message.nested_type, message.enum_type)
|
130
|
+
|
131
|
+
line %{set_fully_qualified_name "#{fully_qualified_name}"}
|
132
|
+
line
|
125
133
|
|
126
134
|
line %{# nested messages} unless message.nested_type.empty?
|
127
|
-
message.nested_type.each { |inner| dump_message(inner) }
|
135
|
+
message.nested_type.each { |inner| dump_message(fully_qualified_name, inner) }
|
128
136
|
|
129
137
|
message.field.each do |field|
|
130
138
|
typename = field_typename(field)
|
@@ -147,9 +155,14 @@ HEADER
|
|
147
155
|
line
|
148
156
|
end
|
149
157
|
|
150
|
-
def dump_enum(enum)
|
158
|
+
def dump_enum(package, enum)
|
151
159
|
in_namespace("module", enum.name) do
|
152
160
|
line %{include ::ProtocolBuffers::Enum}
|
161
|
+
line
|
162
|
+
|
163
|
+
line %{set_fully_qualified_name "#{fully_qualified_name(package, enum.name)}"}
|
164
|
+
line
|
165
|
+
|
153
166
|
enum.value.each do |value|
|
154
167
|
line %{#{capfirst(value.name)} = #{value.number}}
|
155
168
|
end
|
@@ -157,8 +170,23 @@ HEADER
|
|
157
170
|
line
|
158
171
|
end
|
159
172
|
|
173
|
+
def dump_service(package, service)
|
174
|
+
in_namespace("class", service.name, " < ::ProtocolBuffers::Service") do
|
175
|
+
fully_qualified_name = fully_qualified_name(package, service.name)
|
176
|
+
line %{set_fully_qualified_name "#{fully_qualified_name}"}
|
177
|
+
line
|
178
|
+
service.method.each do |method|
|
179
|
+
line %{rpc :#{underscore(method.name)}, "#{method.name}", #{service_typename(method.input_type)}, #{service_typename(method.output_type)}}
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
160
184
|
def field_typename(field)
|
161
|
-
TYPE_MAPPING[field.type] || field.type_name
|
185
|
+
TYPE_MAPPING[field.type] || service_typename(field.type_name)
|
186
|
+
end
|
187
|
+
|
188
|
+
def service_typename(type_name)
|
189
|
+
type_name.split(".").map { |t| camelize(t) }.join("::")
|
162
190
|
end
|
163
191
|
|
164
192
|
# TODO: this probably doesn't work for all default values, expand
|
@@ -176,6 +204,10 @@ HEADER
|
|
176
204
|
end
|
177
205
|
end
|
178
206
|
|
207
|
+
def fully_qualified_name(package, name)
|
208
|
+
package == nil || package.empty? ? name : "#{package}.#{name}"
|
209
|
+
end
|
210
|
+
|
179
211
|
def capfirst(s)
|
180
212
|
"#{s[0,1].capitalize}#{s[1..-1]}" if s
|
181
213
|
end
|
@@ -184,4 +216,15 @@ HEADER
|
|
184
216
|
lower_case_and_underscored_word.to_s.gsub(/(?:^|_)(.)/) { $1.upcase }
|
185
217
|
end
|
186
218
|
|
219
|
+
def underscore(camelized_word)
|
220
|
+
word = camelized_word.to_s.dup
|
221
|
+
word.gsub!(/::/, '/')
|
222
|
+
word.gsub!(/(?:([A-Za-z\d])|^)((?=\a)\b)(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" }
|
223
|
+
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
|
224
|
+
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
225
|
+
word.tr!("-", "_")
|
226
|
+
word.downcase!
|
227
|
+
word
|
228
|
+
end
|
229
|
+
|
187
230
|
end
|
@@ -1,4 +1,36 @@
|
|
1
1
|
module ProtocolBuffers
|
2
|
-
module Enum
|
2
|
+
module Enum
|
3
|
+
def self.included(clazz)
|
4
|
+
clazz.extend(EnumClassMethods)
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
module EnumClassMethods
|
9
|
+
def set_fully_qualified_name(fully_qualified_name)
|
10
|
+
@fully_qualified_name = fully_qualified_name.dup.freeze
|
11
|
+
end
|
12
|
+
|
13
|
+
def fully_qualified_name
|
14
|
+
@fully_qualified_name
|
15
|
+
end
|
16
|
+
|
17
|
+
def value_to_names_map
|
18
|
+
@value_to_names_map ||= self.constants.inject(Hash.new) do |hash, constant|
|
19
|
+
# values do not have to be unique
|
20
|
+
value = self.const_get(constant)
|
21
|
+
hash[value] ||= Array.new
|
22
|
+
hash[value] << constant.to_sym
|
23
|
+
hash
|
24
|
+
end
|
25
|
+
@value_to_names_map
|
26
|
+
end
|
27
|
+
|
28
|
+
def name_to_value_map
|
29
|
+
@name_to_value_map ||= self.constants.inject(Hash.new) do |hash, constant|
|
30
|
+
hash[constant.to_sym] = self.const_get(constant)
|
31
|
+
hash
|
32
|
+
end
|
33
|
+
@name_to_value_map
|
34
|
+
end
|
3
35
|
end
|
4
36
|
end
|
@@ -151,26 +151,26 @@ module ProtocolBuffers
|
|
151
151
|
def add_writer_to(klass)
|
152
152
|
if repeated?
|
153
153
|
klass.class_eval <<-EOF, __FILE__, __LINE__+1
|
154
|
-
def #{name}=(
|
155
|
-
if
|
154
|
+
def #{name}=(__value)
|
155
|
+
if __value.nil?
|
156
156
|
#{name}.clear
|
157
157
|
else
|
158
158
|
#{name}.clear
|
159
|
-
|
159
|
+
__value.each { |i| @#{name}.push i }
|
160
160
|
end
|
161
161
|
end
|
162
162
|
EOF
|
163
163
|
else
|
164
164
|
klass.class_eval <<-EOF, __FILE__, __LINE__+1
|
165
|
-
def #{name}=(
|
165
|
+
def #{name}=(__value)
|
166
166
|
field = fields[#{tag}]
|
167
|
-
if
|
167
|
+
if __value.nil?
|
168
168
|
@set_fields[#{tag}] = false
|
169
169
|
@#{name} = field.default_value
|
170
170
|
else
|
171
|
-
field.check_valid(
|
171
|
+
field.check_valid(__value)
|
172
172
|
@set_fields[#{tag}] = true
|
173
|
-
@#{name} =
|
173
|
+
@#{name} = __value
|
174
174
|
if @parent_for_notify
|
175
175
|
@parent_for_notify.default_changed(@tag_for_notify)
|
176
176
|
@parent_for_notify = @tag_for_notify = nil
|
@@ -257,6 +257,22 @@ module ProtocolBuffers
|
|
257
257
|
end
|
258
258
|
alias_method :to_s, :serialize_to_string
|
259
259
|
|
260
|
+
def to_hash
|
261
|
+
self.class.to_hash(self)
|
262
|
+
end
|
263
|
+
|
264
|
+
def self.to_hash(message)
|
265
|
+
return nil if message == nil
|
266
|
+
return message.is_a?(String) ? message.dup : message unless message.is_a?(::ProtocolBuffers::Message)
|
267
|
+
message.fields.select do |tag, field|
|
268
|
+
message.value_for_tag?(tag)
|
269
|
+
end.inject(Hash.new) do |hash, (tag, field)|
|
270
|
+
value = message.value_for_tag(tag)
|
271
|
+
hash[field.name] = value.is_a?(::ProtocolBuffers::RepeatedField) ? value.map { |elem| to_hash(elem) } : to_hash(value)
|
272
|
+
hash
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
260
276
|
# Parse a Message of this class from the given IO/String. Since Protocol
|
261
277
|
# Buffers are not length delimited, this will read until the end of the
|
262
278
|
# stream.
|
@@ -447,6 +463,18 @@ module ProtocolBuffers
|
|
447
463
|
end
|
448
464
|
end
|
449
465
|
|
466
|
+
def self.set_fully_qualified_name(name)
|
467
|
+
@fully_qualified_name = name.dup.freeze
|
468
|
+
end
|
469
|
+
|
470
|
+
def self.fully_qualified_name
|
471
|
+
@fully_qualified_name
|
472
|
+
end
|
473
|
+
|
474
|
+
def fully_qualified_name
|
475
|
+
self.class.fully_qualified_name
|
476
|
+
end
|
477
|
+
|
450
478
|
def valid?
|
451
479
|
self.class.valid?(self)
|
452
480
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ProtocolBuffers
|
2
|
+
class Rpc
|
3
|
+
attr_reader :name, :proto_name, :request_class, :response_class, :service_class
|
4
|
+
|
5
|
+
def initialize(name, proto_name, request_class, response_class, service_class)
|
6
|
+
@name = name
|
7
|
+
@proto_name = proto_name.dup.freeze
|
8
|
+
@request_class = request_class
|
9
|
+
@response_class = response_class
|
10
|
+
@service_class = service_class
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
{
|
15
|
+
:name => name,
|
16
|
+
:proto_name => proto_name,
|
17
|
+
:request_class_name => request_class.name,
|
18
|
+
:response_class_name => response_class.name,
|
19
|
+
:service_class_name => service_class.name
|
20
|
+
}.to_s
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -1 +1,27 @@
|
|
1
|
-
|
1
|
+
require 'protocol_buffers/runtime/rpc'
|
2
|
+
|
3
|
+
module ProtocolBuffers
|
4
|
+
class Service
|
5
|
+
|
6
|
+
private_class_method :new
|
7
|
+
|
8
|
+
def self.set_fully_qualified_name(name)
|
9
|
+
@fully_qualified_name = name.dup.freeze
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.fully_qualified_name
|
13
|
+
@fully_qualified_name
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.rpcs
|
17
|
+
@rpcs
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.rpc(name, proto_name, request_type, response_type)
|
21
|
+
@rpcs ||= Array.new
|
22
|
+
@rpcs = @rpcs.dup
|
23
|
+
@rpcs << Rpc.new(name.to_sym, proto_name, request_type, response_type, self).freeze
|
24
|
+
@rpcs.freeze
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
3
|
+
|
4
|
+
require 'protocol_buffers'
|
5
|
+
|
6
|
+
module Enums
|
7
|
+
# forward declarations
|
8
|
+
class FooMessage < ::ProtocolBuffers::Message; end
|
9
|
+
|
10
|
+
# enums
|
11
|
+
module FooEnum
|
12
|
+
include ::ProtocolBuffers::Enum
|
13
|
+
|
14
|
+
set_fully_qualified_name "enums.FooEnum"
|
15
|
+
|
16
|
+
ONE = 1
|
17
|
+
TWO = 2
|
18
|
+
THREE = 3
|
19
|
+
end
|
20
|
+
|
21
|
+
module BarEnum
|
22
|
+
include ::ProtocolBuffers::Enum
|
23
|
+
|
24
|
+
# purposefully removing qualified name to make sure that nothing breaks
|
25
|
+
#set_fully_qualified_name "enums.BarEnum"
|
26
|
+
|
27
|
+
FOUR = 4
|
28
|
+
FIVE = 5
|
29
|
+
SIX = 6
|
30
|
+
end
|
31
|
+
|
32
|
+
class FooMessage < ::ProtocolBuffers::Message
|
33
|
+
# forward declarations
|
34
|
+
|
35
|
+
# enums
|
36
|
+
module NestedFooEnum
|
37
|
+
include ::ProtocolBuffers::Enum
|
38
|
+
|
39
|
+
set_fully_qualified_name "enums.FooMessage.NestedFooEnum"
|
40
|
+
|
41
|
+
SEVEN = 7
|
42
|
+
EIGHT = 8
|
43
|
+
end
|
44
|
+
|
45
|
+
module NestedBarEnum
|
46
|
+
include ::ProtocolBuffers::Enum
|
47
|
+
|
48
|
+
# purposefully removing qualified name to make sure that nothing breaks
|
49
|
+
#set_fully_qualified_name "enums.FooMessage.NestedBarEnum"
|
50
|
+
|
51
|
+
NINE = 9
|
52
|
+
TEN = 10
|
53
|
+
end
|
54
|
+
|
55
|
+
set_fully_qualified_name "enums.FooMessage"
|
56
|
+
|
57
|
+
optional ::Enums::FooMessage::NestedFooEnum, :nested_foo_enum, 1
|
58
|
+
optional ::Enums::FooMessage::NestedBarEnum, :nested_bar_enum, 2
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
package enums;
|
2
|
+
|
3
|
+
enum FooEnum {
|
4
|
+
ONE = 1;
|
5
|
+
TWO = 2;
|
6
|
+
THREE = 3;
|
7
|
+
}
|
8
|
+
|
9
|
+
enum BarEnum {
|
10
|
+
FOUR = 4;
|
11
|
+
FIVE = 5;
|
12
|
+
SIX = 6;
|
13
|
+
}
|
14
|
+
|
15
|
+
message FooMessage {
|
16
|
+
enum NestedFooEnum {
|
17
|
+
SEVEN = 7;
|
18
|
+
EIGHT = 8;
|
19
|
+
}
|
20
|
+
enum NestedBarEnum {
|
21
|
+
NINE = 9;
|
22
|
+
TEN = 10;
|
23
|
+
}
|
24
|
+
optional NestedFooEnum nested_foo_enum = 1;
|
25
|
+
optional NestedBarEnum nested_bar_enum = 2;
|
26
|
+
}
|
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
3
|
+
|
4
|
+
require 'protocol_buffers'
|
5
|
+
|
6
|
+
# forward declarations
|
7
|
+
class A < ::ProtocolBuffers::Message; end
|
8
|
+
class C < ::ProtocolBuffers::Message; end
|
9
|
+
|
10
|
+
class A < ::ProtocolBuffers::Message
|
11
|
+
# forward declarations
|
12
|
+
class B < ::ProtocolBuffers::Message; end
|
13
|
+
|
14
|
+
set_fully_qualified_name "A"
|
15
|
+
|
16
|
+
# nested messages
|
17
|
+
class B < ::ProtocolBuffers::Message
|
18
|
+
set_fully_qualified_name "A.B"
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
optional ::A::B, :b, 1
|
23
|
+
end
|
24
|
+
|
25
|
+
class C < ::ProtocolBuffers::Message
|
26
|
+
# purposefully removing qualified name to make sure that nothing breaks
|
27
|
+
#set_fully_qualified_name "C"
|
28
|
+
|
29
|
+
optional ::A::B, :b, 1
|
30
|
+
end
|
31
|
+
|
@@ -0,0 +1,50 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
3
|
+
|
4
|
+
require 'protocol_buffers'
|
5
|
+
|
6
|
+
module Services
|
7
|
+
# forward declarations
|
8
|
+
class FooRequest < ::ProtocolBuffers::Message; end
|
9
|
+
class FooResponse < ::ProtocolBuffers::Message; end
|
10
|
+
class BarRequest < ::ProtocolBuffers::Message; end
|
11
|
+
class BarResponse < ::ProtocolBuffers::Message; end
|
12
|
+
|
13
|
+
class FooRequest < ::ProtocolBuffers::Message
|
14
|
+
set_fully_qualified_name "services.FooRequest"
|
15
|
+
|
16
|
+
optional :sint64, :one, 1
|
17
|
+
end
|
18
|
+
|
19
|
+
class FooResponse < ::ProtocolBuffers::Message
|
20
|
+
set_fully_qualified_name "services.FooResponse"
|
21
|
+
|
22
|
+
optional :uint64, :two, 1
|
23
|
+
end
|
24
|
+
|
25
|
+
class BarRequest < ::ProtocolBuffers::Message
|
26
|
+
set_fully_qualified_name "services.BarRequest"
|
27
|
+
|
28
|
+
optional :string, :three, 1
|
29
|
+
end
|
30
|
+
|
31
|
+
class BarResponse < ::ProtocolBuffers::Message
|
32
|
+
set_fully_qualified_name "services.BarResponse"
|
33
|
+
|
34
|
+
optional :bytes, :four, 1
|
35
|
+
end
|
36
|
+
|
37
|
+
class FooBarService < ::ProtocolBuffers::Service
|
38
|
+
set_fully_qualified_name "services.FooBarService"
|
39
|
+
|
40
|
+
rpc :get_foo, "GetFoo", ::Services::FooRequest, ::Services::FooResponse
|
41
|
+
rpc :get_bar, "GetBar", ::Services::BarRequest, ::Services::BarResponse
|
42
|
+
end
|
43
|
+
class NoNameFooBarService < ::ProtocolBuffers::Service
|
44
|
+
# purposefully removing qualified name to make sure that nothing breaks
|
45
|
+
#set_fully_qualified_name "services.NoNameFooBarService"
|
46
|
+
|
47
|
+
rpc :get_foo, "GetFoo", ::Services::FooRequest, ::Services::FooResponse
|
48
|
+
rpc :get_bar, "GetBar", ::Services::BarRequest, ::Services::BarResponse
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
package services;
|
2
|
+
|
3
|
+
message FooRequest {
|
4
|
+
optional sint64 one = 1;
|
5
|
+
}
|
6
|
+
|
7
|
+
message FooResponse {
|
8
|
+
optional uint64 two = 1;
|
9
|
+
}
|
10
|
+
|
11
|
+
message BarRequest {
|
12
|
+
optional string three = 1;
|
13
|
+
}
|
14
|
+
|
15
|
+
message BarResponse {
|
16
|
+
optional bytes four = 1;
|
17
|
+
}
|
18
|
+
|
19
|
+
service FooBarService {
|
20
|
+
rpc GetFoo(FooRequest) returns (FooResponse);
|
21
|
+
rpc GetBar(BarRequest) returns (BarResponse);
|
22
|
+
}
|
23
|
+
|
24
|
+
service NoNameFooBarService {
|
25
|
+
rpc GetFoo(FooRequest) returns (FooResponse);
|
26
|
+
rpc GetBar(BarRequest) returns (BarResponse);
|
27
|
+
}
|
@@ -10,13 +10,20 @@ module Simple
|
|
10
10
|
class Bar < ::ProtocolBuffers::Message; end
|
11
11
|
|
12
12
|
class Test1 < ::ProtocolBuffers::Message
|
13
|
+
set_fully_qualified_name "simple.Test1"
|
14
|
+
|
13
15
|
optional :string, :test_field, 1
|
14
16
|
end
|
15
17
|
|
16
18
|
class Foo < ::ProtocolBuffers::Message
|
19
|
+
set_fully_qualified_name "simple.Foo"
|
20
|
+
|
17
21
|
end
|
18
22
|
|
19
23
|
class Bar < ::ProtocolBuffers::Message
|
24
|
+
# purposefully removing qualified name to make sure that nothing breaks
|
25
|
+
#set_fully_qualified_name "simple.Bar"
|
26
|
+
|
20
27
|
optional ::Simple::Foo, :foo, 1
|
21
28
|
end
|
22
29
|
|
data/spec/runtime_spec.rb
CHANGED
@@ -11,12 +11,12 @@ require 'protocol_buffers/compiler'
|
|
11
11
|
describe ProtocolBuffers, "runtime" do
|
12
12
|
before(:each) do
|
13
13
|
# clear our namespaces
|
14
|
-
%w( Simple Featureful Foo Packed TehUnknown TehUnknown2 TehUnknown3 ).each do |klass|
|
14
|
+
%w( Simple Featureful Foo Packed TehUnknown TehUnknown2 TehUnknown3 Enums A C Services).each do |klass|
|
15
15
|
Object.send(:remove_const, klass.to_sym) if Object.const_defined?(klass.to_sym)
|
16
16
|
end
|
17
17
|
|
18
18
|
# load test protos
|
19
|
-
%w( simple featureful packed ).each do |proto|
|
19
|
+
%w( simple featureful packed enums no_package services).each do |proto|
|
20
20
|
load File.join(File.dirname(__FILE__), "proto_files", "#{proto}.pb.rb")
|
21
21
|
end
|
22
22
|
end
|
@@ -105,13 +105,48 @@ describe ProtocolBuffers, "runtime" do
|
|
105
105
|
msg2.should == msg1
|
106
106
|
end
|
107
107
|
|
108
|
+
it "correctly unsets fields" do
|
109
|
+
msg1 = Simple::Test1.new
|
110
|
+
msg1.has_test_field?.should == false
|
111
|
+
msg1.test_field.should == ""
|
112
|
+
msg1.to_s.should == ""
|
113
|
+
|
114
|
+
msg1.test_field = "zomgkittenz"
|
115
|
+
msg1.has_test_field?.should == true
|
116
|
+
msg1.test_field.should == "zomgkittenz"
|
117
|
+
msg1.to_s.should_not == ""
|
118
|
+
|
119
|
+
msg1.test_field = nil
|
120
|
+
msg1.has_test_field?.should == false
|
121
|
+
msg1.test_field.should == ""
|
122
|
+
msg1.to_s.should == ""
|
123
|
+
end
|
124
|
+
|
108
125
|
it "doesn't serialize unset fields" do
|
109
126
|
msg1 = Simple::Test1.new
|
127
|
+
msg1.has_test_field?.should == false
|
128
|
+
msg1.test_field.should == ""
|
129
|
+
msg1.to_s.should == ""
|
130
|
+
|
131
|
+
msg2 = Simple::Test1.parse(ProtocolBuffers.bin_sio(msg1.to_s))
|
132
|
+
msg2.has_test_field?.should == false
|
133
|
+
msg2.test_field.should == ""
|
134
|
+
msg2.to_s.should == ""
|
135
|
+
|
136
|
+
msg1 = Simple::Test1.new
|
137
|
+
msg1.has_test_field?.should == false
|
110
138
|
msg1.test_field.should == ""
|
111
139
|
msg1.to_s.should == ""
|
112
140
|
|
113
141
|
msg1.test_field = "zomgkittenz"
|
114
142
|
msg1.to_s.should_not == ""
|
143
|
+
|
144
|
+
msg1.test_field = nil
|
145
|
+
|
146
|
+
msg2 = Simple::Test1.parse(ProtocolBuffers.bin_sio(msg1.to_s))
|
147
|
+
msg2.has_test_field?.should == false
|
148
|
+
msg2.test_field.should == ""
|
149
|
+
msg2.to_s.should == ""
|
115
150
|
end
|
116
151
|
|
117
152
|
it "flags values that have been set" do
|
@@ -646,4 +681,181 @@ describe ProtocolBuffers, "runtime" do
|
|
646
681
|
msg.i.should == 805059
|
647
682
|
end
|
648
683
|
|
684
|
+
it "correctly converts to a hash" do
|
685
|
+
f = Featureful::A.new
|
686
|
+
f.i1 = [1, 2, 3]
|
687
|
+
f.i3 = 4
|
688
|
+
sub11 = Featureful::A::Sub.new
|
689
|
+
sub11.payload = "sub11payload"
|
690
|
+
sub11.payload_type = Featureful::A::Sub::Payloads::P1
|
691
|
+
sub11.subsub1.subsub_payload = "sub11subsubpayload"
|
692
|
+
sub12 = Featureful::A::Sub.new
|
693
|
+
sub12.payload = "sub12payload"
|
694
|
+
sub12.payload_type = Featureful::A::Sub::Payloads::P2
|
695
|
+
sub12.subsub1.subsub_payload = "sub12subsubpayload"
|
696
|
+
f.sub1 = [sub11, sub12]
|
697
|
+
f.sub3.payload = "sub3payload"
|
698
|
+
f.sub3.payload_type = Featureful::A::Sub::Payloads::P1
|
699
|
+
f.sub3.subsub1.subsub_payload = "sub3subsubpayload"
|
700
|
+
f.group3.i1 = 1
|
701
|
+
|
702
|
+
f.valid?.should == true
|
703
|
+
f.to_hash.should == {
|
704
|
+
:i1 => [1, 2, 3],
|
705
|
+
:i3 => 4,
|
706
|
+
:sub1 => [
|
707
|
+
{
|
708
|
+
:payload => "sub11payload",
|
709
|
+
:payload_type => 0,
|
710
|
+
:subsub1 => {
|
711
|
+
:subsub_payload => "sub11subsubpayload"
|
712
|
+
}
|
713
|
+
},
|
714
|
+
{
|
715
|
+
:payload => "sub12payload",
|
716
|
+
:payload_type => 1,
|
717
|
+
:subsub1 => {
|
718
|
+
:subsub_payload => "sub12subsubpayload"
|
719
|
+
}
|
720
|
+
}
|
721
|
+
],
|
722
|
+
:sub3 => {
|
723
|
+
:payload => "sub3payload",
|
724
|
+
:payload_type => 0,
|
725
|
+
:subsub1 => {
|
726
|
+
:subsub_payload => "sub3subsubpayload"
|
727
|
+
}
|
728
|
+
},
|
729
|
+
:group1 => [],
|
730
|
+
:group3 => {
|
731
|
+
:i1 => 1,
|
732
|
+
:subgroup => []
|
733
|
+
}
|
734
|
+
}
|
735
|
+
|
736
|
+
end
|
737
|
+
|
738
|
+
it "correctly handles fully qualified names on Messages" do
|
739
|
+
Simple::Test1.fully_qualified_name.should == "simple.Test1"
|
740
|
+
Simple::Foo.fully_qualified_name.should == "simple.Foo"
|
741
|
+
Simple::Bar.fully_qualified_name.should == nil
|
742
|
+
end
|
743
|
+
|
744
|
+
it "correctly handles fully qualified names on Messages with no package" do
|
745
|
+
A.fully_qualified_name.should == "A"
|
746
|
+
A::B.fully_qualified_name.should == "A.B"
|
747
|
+
C.fully_qualified_name.should == nil
|
748
|
+
end
|
749
|
+
|
750
|
+
it "has only Enum values as constants" do
|
751
|
+
Enums::FooEnum.constants.map(&:to_sym).should =~ [:ONE, :TWO, :THREE]
|
752
|
+
Enums::BarEnum.constants.map(&:to_sym).should =~ [:FOUR, :FIVE, :SIX]
|
753
|
+
Enums::FooMessage::NestedFooEnum.constants.map(&:to_sym).should =~ [:SEVEN, :EIGHT]
|
754
|
+
Enums::FooMessage::NestedBarEnum.constants.map(&:to_sym).should =~ [:NINE, :TEN]
|
755
|
+
end
|
756
|
+
|
757
|
+
it "correctly populates the maps between name and values for Enums" do
|
758
|
+
Enums::FooEnum.value_to_names_map.should == {
|
759
|
+
1 => [:ONE],
|
760
|
+
2 => [:TWO],
|
761
|
+
3 => [:THREE]
|
762
|
+
}
|
763
|
+
Enums::BarEnum.value_to_names_map.should == {
|
764
|
+
4 => [:FOUR],
|
765
|
+
5 => [:FIVE],
|
766
|
+
6 => [:SIX]
|
767
|
+
}
|
768
|
+
Enums::FooEnum.name_to_value_map.should == {
|
769
|
+
:ONE => 1,
|
770
|
+
:TWO => 2,
|
771
|
+
:THREE => 3
|
772
|
+
}
|
773
|
+
Enums::BarEnum.name_to_value_map.should == {
|
774
|
+
:FOUR => 4,
|
775
|
+
:FIVE => 5,
|
776
|
+
:SIX => 6
|
777
|
+
}
|
778
|
+
Enums::FooMessage::NestedFooEnum.value_to_names_map.should == {
|
779
|
+
7 => [:SEVEN],
|
780
|
+
8 => [:EIGHT],
|
781
|
+
}
|
782
|
+
Enums::FooMessage::NestedBarEnum.value_to_names_map.should == {
|
783
|
+
9 => [:NINE],
|
784
|
+
10 => [:TEN],
|
785
|
+
}
|
786
|
+
Enums::FooMessage::NestedFooEnum.name_to_value_map.should == {
|
787
|
+
:SEVEN => 7,
|
788
|
+
:EIGHT => 8,
|
789
|
+
}
|
790
|
+
Enums::FooMessage::NestedBarEnum.name_to_value_map.should == {
|
791
|
+
:NINE => 9,
|
792
|
+
:TEN => 10,
|
793
|
+
}
|
794
|
+
end
|
795
|
+
|
796
|
+
it "correctly handles fully qualified names on Enums" do
|
797
|
+
Enums::FooEnum.fully_qualified_name.should == "enums.FooEnum"
|
798
|
+
Enums::BarEnum.fully_qualified_name.should == nil
|
799
|
+
Enums::FooMessage::NestedFooEnum.fully_qualified_name.should == "enums.FooMessage.NestedFooEnum"
|
800
|
+
Enums::FooMessage::NestedBarEnum.fully_qualified_name.should == nil
|
801
|
+
end
|
802
|
+
|
803
|
+
it "correctly handles service definitions" do
|
804
|
+
get_foo_rpc, get_bar_rpc = get_rpcs
|
805
|
+
|
806
|
+
get_foo_rpc.name.should == :get_foo
|
807
|
+
get_foo_rpc.proto_name.should == "GetFoo"
|
808
|
+
get_foo_rpc.request_class.should == Services::FooRequest
|
809
|
+
get_foo_rpc.response_class.should == Services::FooResponse
|
810
|
+
get_foo_rpc.service_class.should == Services::FooBarService
|
811
|
+
|
812
|
+
get_bar_rpc.name.should == :get_bar
|
813
|
+
get_bar_rpc.proto_name.should == "GetBar"
|
814
|
+
get_bar_rpc.request_class.should == Services::BarRequest
|
815
|
+
get_bar_rpc.response_class.should == Services::BarResponse
|
816
|
+
get_bar_rpc.service_class.should == Services::FooBarService
|
817
|
+
end
|
818
|
+
|
819
|
+
it "correctly handles == for Rpcs" do
|
820
|
+
get_foo_rpc, get_bar_rpc = get_rpcs
|
821
|
+
|
822
|
+
get_foo_rpc.should == get_foo_rpc
|
823
|
+
get_bar_rpc.should == get_bar_rpc
|
824
|
+
get_foo_rpc.should_not == get_bar_rpc
|
825
|
+
end
|
826
|
+
|
827
|
+
it "correctly freezes rpcs" do
|
828
|
+
get_foo_rpc, get_bar_rpc = get_rpcs
|
829
|
+
|
830
|
+
get_foo_rpc.frozen?.should == true
|
831
|
+
get_bar_rpc.frozen?.should == true
|
832
|
+
get_foo_rpc.proto_name.frozen?.should == true
|
833
|
+
get_bar_rpc.proto_name.frozen?.should == true
|
834
|
+
|
835
|
+
# make sure to_s is still possible when frozen
|
836
|
+
get_foo_rpc.to_s
|
837
|
+
get_bar_rpc.to_s
|
838
|
+
|
839
|
+
Services::FooBarService.rpcs.frozen?.should == true
|
840
|
+
end
|
841
|
+
|
842
|
+
it "correctly handles fully qualified names on Services" do
|
843
|
+
Services::FooBarService.fully_qualified_name.should == "services.FooBarService"
|
844
|
+
Services::NoNameFooBarService.fully_qualified_name.should == nil
|
845
|
+
end
|
846
|
+
|
847
|
+
def get_rpcs
|
848
|
+
Services::FooBarService.rpcs.size.should == 2
|
849
|
+
first_rpc = Services::FooBarService.rpcs[0]
|
850
|
+
second_rpc = Services::FooBarService.rpcs[1]
|
851
|
+
case first_rpc.name
|
852
|
+
when :get_foo
|
853
|
+
second_rpc.name.should == :get_bar
|
854
|
+
return first_rpc, second_rpc
|
855
|
+
when :get_bar
|
856
|
+
first_rpc.name.should == :get_bar
|
857
|
+
return second_rpc, first_rpc
|
858
|
+
else raise ArgumentError.new(first_rpc.name)
|
859
|
+
end
|
860
|
+
end
|
649
861
|
end
|
metadata
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-protocol-buffers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.5.0.beta1
|
5
|
+
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Brian Palmer
|
@@ -12,7 +12,7 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date: 2013-
|
15
|
+
date: 2013-08-28 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: autotest-standalone
|
@@ -170,6 +170,7 @@ files:
|
|
170
170
|
- lib/protocol_buffers/runtime/extend.rb
|
171
171
|
- lib/protocol_buffers/runtime/field.rb
|
172
172
|
- lib/protocol_buffers/runtime/message.rb
|
173
|
+
- lib/protocol_buffers/runtime/rpc.rb
|
173
174
|
- lib/protocol_buffers/runtime/service.rb
|
174
175
|
- lib/protocol_buffers/runtime/varint.rb
|
175
176
|
- lib/protocol_buffers/version.rb
|
@@ -180,12 +181,17 @@ files:
|
|
180
181
|
- spec/nil_bugs_spec.rb
|
181
182
|
- spec/proto_files/depends.proto
|
182
183
|
- spec/proto_files/dotted_package.proto
|
184
|
+
- spec/proto_files/enums.pb.rb
|
185
|
+
- spec/proto_files/enums.proto
|
183
186
|
- spec/proto_files/featureful.pb.rb
|
184
187
|
- spec/proto_files/featureful.proto
|
185
188
|
- spec/proto_files/nested/child.proto
|
189
|
+
- spec/proto_files/no_package.pb.rb
|
186
190
|
- spec/proto_files/no_package.proto
|
187
191
|
- spec/proto_files/packed.pb.rb
|
188
192
|
- spec/proto_files/packed.proto
|
193
|
+
- spec/proto_files/services.pb.rb
|
194
|
+
- spec/proto_files/services.proto
|
189
195
|
- spec/proto_files/simple.pb.rb
|
190
196
|
- spec/proto_files/simple.proto
|
191
197
|
- spec/proto_files/top_level_enum.proto
|
@@ -197,7 +203,8 @@ files:
|
|
197
203
|
- tasks/rspec.rake
|
198
204
|
- tasks/yard.rake
|
199
205
|
homepage: https://github.com/codekitchen/ruby-protocol-buffers
|
200
|
-
licenses:
|
206
|
+
licenses:
|
207
|
+
- BSD
|
201
208
|
post_install_message:
|
202
209
|
rdoc_options: []
|
203
210
|
require_paths:
|
@@ -210,16 +217,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
210
217
|
version: '0'
|
211
218
|
segments:
|
212
219
|
- 0
|
213
|
-
hash: -
|
220
|
+
hash: -2340940649634800505
|
214
221
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
215
222
|
none: false
|
216
223
|
requirements:
|
217
|
-
- - ! '
|
224
|
+
- - ! '>'
|
218
225
|
- !ruby/object:Gem::Version
|
219
|
-
version:
|
220
|
-
segments:
|
221
|
-
- 0
|
222
|
-
hash: -833684518850791827
|
226
|
+
version: 1.3.1
|
223
227
|
requirements: []
|
224
228
|
rubyforge_project:
|
225
229
|
rubygems_version: 1.8.23
|
@@ -233,12 +237,17 @@ test_files:
|
|
233
237
|
- spec/nil_bugs_spec.rb
|
234
238
|
- spec/proto_files/depends.proto
|
235
239
|
- spec/proto_files/dotted_package.proto
|
240
|
+
- spec/proto_files/enums.pb.rb
|
241
|
+
- spec/proto_files/enums.proto
|
236
242
|
- spec/proto_files/featureful.pb.rb
|
237
243
|
- spec/proto_files/featureful.proto
|
238
244
|
- spec/proto_files/nested/child.proto
|
245
|
+
- spec/proto_files/no_package.pb.rb
|
239
246
|
- spec/proto_files/no_package.proto
|
240
247
|
- spec/proto_files/packed.pb.rb
|
241
248
|
- spec/proto_files/packed.proto
|
249
|
+
- spec/proto_files/services.pb.rb
|
250
|
+
- spec/proto_files/services.proto
|
242
251
|
- spec/proto_files/simple.pb.rb
|
243
252
|
- spec/proto_files/simple.proto
|
244
253
|
- spec/proto_files/top_level_enum.proto
|