ruby_protobuf 0.3.3 → 0.4.1
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/History.txt +10 -2
- data/Manifest.txt +26 -12
- data/README.txt +6 -1
- data/Rakefile +51 -14
- data/TODO +15 -0
- data/VERSION +1 -0
- data/bin/rprotoc +11 -6
- data/lib/protobuf/common/exceptions.rb +11 -0
- data/lib/protobuf/common/util.rb +9 -0
- data/lib/protobuf/common/wire_type.rb +6 -6
- data/lib/protobuf/compiler/compiler.rb +16 -16
- data/lib/protobuf/compiler/nodes.rb +67 -65
- data/lib/protobuf/compiler/proto.y +31 -38
- data/lib/protobuf/compiler/proto_parser.rb +315 -294
- data/lib/protobuf/compiler/template/rpc_bin.erb +1 -1
- data/lib/protobuf/compiler/template/rpc_client.erb +1 -1
- data/lib/protobuf/compiler/template/rpc_service.erb +2 -2
- data/lib/protobuf/compiler/visitors.rb +45 -39
- data/lib/protobuf/descriptor/descriptor.proto +0 -0
- data/lib/protobuf/descriptor/descriptor.rb +11 -10
- data/lib/protobuf/descriptor/descriptor_builder.rb +6 -7
- data/lib/protobuf/descriptor/descriptor_proto.rb +51 -31
- data/lib/protobuf/descriptor/enum_descriptor.rb +5 -5
- data/lib/protobuf/descriptor/field_descriptor.rb +8 -9
- data/lib/protobuf/descriptor/file_descriptor.rb +0 -1
- data/lib/protobuf/message/decoder.rb +33 -35
- data/lib/protobuf/message/encoder.rb +23 -19
- data/lib/protobuf/message/enum.rb +43 -9
- data/lib/protobuf/message/field.rb +281 -193
- data/lib/protobuf/message/message.rb +166 -110
- data/lib/protobuf/message/protoable.rb +4 -3
- data/lib/protobuf/message/service.rb +1 -1
- data/lib/protobuf/rpc/client.rb +3 -3
- data/lib/protobuf/rpc/handler.rb +1 -1
- data/lib/protobuf/rpc/server.rb +8 -8
- data/lib/ruby_protobuf.rb +1 -1
- data/test/check_unbuild.rb +7 -7
- data/test/proto/addressbook.pb.rb +67 -0
- data/test/proto/addressbook.proto +2 -0
- data/test/proto/addressbook_base.pb.rb +59 -0
- data/test/proto/addressbook_base.proto +1 -1
- data/test/proto/addressbook_ext.pb.rb +21 -0
- data/test/proto/addressbook_ext.proto +2 -2
- data/test/{collision.rb → proto/collision.pb.rb} +0 -0
- data/test/{ext_collision.rb → proto/ext_collision.pb.rb} +1 -1
- data/test/{ext_range.rb → proto/ext_range.pb.rb} +4 -4
- data/test/proto/ext_range.proto +2 -2
- data/test/proto/float_default.proto +10 -0
- data/test/proto/lowercase.pb.rb +31 -0
- data/test/proto/lowercase.proto +9 -0
- data/test/{merge.rb → proto/merge.pb.rb} +2 -2
- data/test/proto/nested.pb.rb +31 -0
- data/test/proto/nested.proto +2 -0
- data/test/proto/optional_field.pb.rb +36 -0
- data/test/proto/optional_field.proto +12 -0
- data/test/proto/packed.pb.rb +23 -0
- data/test/proto/packed.proto +6 -0
- data/test/{types.rb → proto/types.pb.rb} +43 -1
- data/test/test_addressbook.rb +30 -17
- data/test/test_compiler.rb +79 -78
- data/test/test_descriptor.rb +12 -12
- data/test/test_enum_value.rb +41 -0
- data/test/test_extension.rb +10 -14
- data/test/test_lowercase.rb +11 -0
- data/test/test_message.rb +44 -41
- data/test/test_optional_field.rb +61 -38
- data/test/test_packed_field.rb +40 -0
- data/test/test_parse.rb +8 -8
- data/test/test_repeated_types.rb +29 -3
- data/test/test_serialize.rb +12 -12
- data/test/test_standard_message.rb +30 -30
- data/test/test_types.rb +95 -95
- metadata +69 -39
- data/test/addressbook.rb +0 -98
- data/test/addressbook_base.rb +0 -62
- data/test/addressbook_ext.rb +0 -12
- data/test/nested.rb +0 -25
- data/test/test_ruby_protobuf.rb +0 -1
@@ -5,7 +5,7 @@ require '<%= required_file %>'
|
|
5
5
|
# build request
|
6
6
|
request = <%= message_module %>::<%= request %>.new
|
7
7
|
# TODO: setup a request
|
8
|
-
raise StandardError
|
8
|
+
raise StandardError, 'setup a request'
|
9
9
|
|
10
10
|
# create blunk response
|
11
11
|
response = <%= message_module %>::<%= response %>.new
|
@@ -6,7 +6,7 @@ require '<%= required_file %>'
|
|
6
6
|
class <%= module_name %>::<%= name %>Handler < Protobuf::Rpc::Handler
|
7
7
|
request <%= module_name %>::<%= request %>
|
8
8
|
response <%= module_name %>::<%= response %>
|
9
|
-
|
9
|
+
|
10
10
|
def self.process_request(request, response)
|
11
11
|
# TODO: edit this method
|
12
12
|
end
|
@@ -19,7 +19,7 @@ class <%= module_name %>::<%= service_name %> < Protobuf::Rpc::Server
|
|
19
19
|
<%- rpcs.each do |name, | -%>
|
20
20
|
:<%= underscore name %> => <%= module_name %>::<%= name %>Handler,
|
21
21
|
<%- end -%>
|
22
|
-
}
|
22
|
+
}
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -8,23 +8,23 @@ module Protobuf
|
|
8
8
|
attr_reader :silent
|
9
9
|
|
10
10
|
def create_file_with_backup(filename, contents, executable=false)
|
11
|
-
if File.exist?
|
11
|
+
if File.exist?(filename)
|
12
12
|
if File.read(filename) == contents
|
13
13
|
# do nothing
|
14
14
|
return
|
15
15
|
else
|
16
|
-
backup_filename = "#{filename}.#{Time.now.to_i}"
|
17
|
-
log_writing
|
18
|
-
FileUtils.copy
|
16
|
+
backup_filename = "#{filename}.#{Time.now.to_i}"
|
17
|
+
log_writing("#{backup_filename}", "backingup...")
|
18
|
+
FileUtils.copy(filename, backup_filename)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
File.open(filename, 'w') do |file|
|
23
|
-
log_writing
|
24
|
-
FileUtils.mkpath
|
25
|
-
file.write
|
22
|
+
File.open(filename, 'w') do |file|
|
23
|
+
log_writing(filename)
|
24
|
+
FileUtils.mkpath(File.dirname(filename))
|
25
|
+
file.write(contents)
|
26
26
|
end
|
27
|
-
|
27
|
+
FileUtils.chmod(0755, filename) if executable
|
28
28
|
end
|
29
29
|
|
30
30
|
def log_writing(filename, message="writing...")
|
@@ -49,8 +49,10 @@ module Protobuf
|
|
49
49
|
|
50
50
|
def commented_proto_contents
|
51
51
|
if proto_file
|
52
|
-
proto_filepath = File.exist?(proto_file)
|
53
|
-
|
52
|
+
proto_filepath = if File.exist?(proto_file)
|
53
|
+
then proto_file
|
54
|
+
else "#{@proto_dir}/#{proto_file}"
|
55
|
+
end
|
54
56
|
File.read(proto_filepath).gsub(/^/, '# ')
|
55
57
|
end
|
56
58
|
end
|
@@ -70,7 +72,7 @@ module Protobuf
|
|
70
72
|
def close_ruby
|
71
73
|
while 0 < indent
|
72
74
|
decrement
|
73
|
-
write
|
75
|
+
write('end')
|
74
76
|
end
|
75
77
|
end
|
76
78
|
|
@@ -79,7 +81,7 @@ module Protobuf
|
|
79
81
|
end
|
80
82
|
|
81
83
|
def to_s
|
82
|
-
@ruby.join
|
84
|
+
@ruby.join("\n")
|
83
85
|
end
|
84
86
|
|
85
87
|
def in_context(klass, &block)
|
@@ -91,24 +93,28 @@ module Protobuf
|
|
91
93
|
end
|
92
94
|
|
93
95
|
def visit(node)
|
94
|
-
node.accept_message_visitor
|
96
|
+
node.accept_message_visitor(self)
|
95
97
|
self
|
96
98
|
end
|
97
99
|
|
98
100
|
def required_message_from_proto(proto_file)
|
99
|
-
rb_path = proto_file.sub(/\.proto
|
101
|
+
rb_path = proto_file.sub(/\.proto\z/, '.pb.rb')
|
100
102
|
unless File.exist?("#{@out_dir}/#{rb_path}")
|
101
|
-
Compiler.compile
|
103
|
+
Compiler.compile(proto_file, @proto_dir, @out_dir)
|
102
104
|
end
|
103
105
|
rb_path.sub(/\.rb$/, '')
|
104
106
|
end
|
105
107
|
|
106
108
|
def create_files(filename, out_dir, file_create)
|
107
|
-
|
109
|
+
begin
|
110
|
+
Class.new.class_eval(to_s) # check the message
|
111
|
+
rescue LoadError => e
|
112
|
+
puts "Warning, couldn't test load proto file because of imports"
|
113
|
+
end
|
108
114
|
if file_create
|
109
|
-
log_writing
|
110
|
-
FileUtils.mkpath
|
111
|
-
File.open(filename, 'w') {|file| file.write
|
115
|
+
log_writing(filename)
|
116
|
+
FileUtils.mkpath(File.dirname(filename))
|
117
|
+
File.open(filename, 'w') {|file| file.write(to_s) }
|
112
118
|
else
|
113
119
|
to_s
|
114
120
|
end
|
@@ -125,7 +131,7 @@ module Protobuf
|
|
125
131
|
end
|
126
132
|
|
127
133
|
def visit(node)
|
128
|
-
node.accept_rpc_visitor
|
134
|
+
node.accept_rpc_visitor(self)
|
129
135
|
self
|
130
136
|
end
|
131
137
|
|
@@ -141,12 +147,12 @@ module Protobuf
|
|
141
147
|
message_module = package.map{|p| p.to_s.capitalize}.join('::')
|
142
148
|
required_file = message_file.sub(/^\.\//, '').sub(/\.rb$/, '')
|
143
149
|
|
144
|
-
create_bin
|
145
|
-
create_service
|
146
|
-
service_name, default_port, rpcs, required_file
|
150
|
+
create_bin(out_dir, underscored_name, message_module, service_name, default_port)
|
151
|
+
create_service(message_file, out_dir, underscored_name, message_module,
|
152
|
+
service_name, default_port, rpcs, required_file)
|
147
153
|
rpcs.each do |name, request, response|
|
148
|
-
create_client
|
149
|
-
message_module, required_file
|
154
|
+
create_client(out_dir, underscored_name, default_port, name, request, response,
|
155
|
+
message_module, required_file)
|
150
156
|
end
|
151
157
|
end
|
152
158
|
@file_contents
|
@@ -154,24 +160,24 @@ module Protobuf
|
|
154
160
|
|
155
161
|
def create_bin(out_dir, underscored_name, module_name, service_name, default_port)
|
156
162
|
bin_filename = "#{out_dir}/start_#{underscored_name}"
|
157
|
-
bin_contents = template_erb('rpc_bin').result
|
158
|
-
create_file_with_backup
|
163
|
+
bin_contents = template_erb('rpc_bin').result(binding)
|
164
|
+
create_file_with_backup(bin_filename, bin_contents, true) if @create_file
|
159
165
|
@file_contents[bin_filename] = bin_contents
|
160
166
|
end
|
161
167
|
|
162
|
-
def create_service(message_file, out_dir, underscored_name, module_name, service_name,
|
168
|
+
def create_service(message_file, out_dir, underscored_name, module_name, service_name,
|
163
169
|
default_port, rpcs, required_file)
|
164
170
|
service_filename = "#{out_dir}/#{underscored_name}.rb"
|
165
|
-
service_contents = template_erb('rpc_service').result
|
166
|
-
create_file_with_backup
|
171
|
+
service_contents = template_erb('rpc_service').result(binding)
|
172
|
+
create_file_with_backup(service_filename, service_contents) if @create_file
|
167
173
|
@file_contents[service_filename] = service_contents
|
168
174
|
end
|
169
175
|
|
170
|
-
def create_client(out_dir, underscored_name, default_port, name, request, response,
|
176
|
+
def create_client(out_dir, underscored_name, default_port, name, request, response,
|
171
177
|
message_module, required_file)
|
172
178
|
client_filename = "#{out_dir}/client_#{underscore name}.rb"
|
173
179
|
client_contents = template_erb('rpc_client').result binding
|
174
|
-
create_file_with_backup
|
180
|
+
create_file_with_backup(client_filename, client_contents, true) if @create_file
|
175
181
|
@file_contents[client_filename] = client_contents
|
176
182
|
end
|
177
183
|
|
@@ -182,7 +188,7 @@ module Protobuf
|
|
182
188
|
end
|
183
189
|
|
184
190
|
def template_erb(template)
|
185
|
-
ERB.new
|
191
|
+
ERB.new(File.read("#{File.dirname(__FILE__)}/template/#{template}.erb"), nil, '-')
|
186
192
|
end
|
187
193
|
end
|
188
194
|
|
@@ -196,7 +202,7 @@ module Protobuf
|
|
196
202
|
end
|
197
203
|
|
198
204
|
def visit(node)
|
199
|
-
node.accept_descriptor_visitor
|
205
|
+
node.accept_descriptor_visitor(self)
|
200
206
|
self
|
201
207
|
end
|
202
208
|
|
@@ -216,7 +222,7 @@ module Protobuf
|
|
216
222
|
end
|
217
223
|
|
218
224
|
def add_option(name, value)
|
219
|
-
options =
|
225
|
+
options =
|
220
226
|
case current_descriptor
|
221
227
|
when Google::Protobuf::FileDescriptorProto
|
222
228
|
Google::Protobuf::FileOptions.new
|
@@ -233,7 +239,7 @@ module Protobuf
|
|
233
239
|
when Google::Protobuf::MethodDescriptorProto
|
234
240
|
Google::Protobuf::MethodOptions.new
|
235
241
|
else
|
236
|
-
raise ArgumentError
|
242
|
+
raise ArgumentError, 'Invalid context'
|
237
243
|
end
|
238
244
|
#TODO how should options be handled?
|
239
245
|
#current_descriptor.options << option
|
@@ -246,7 +252,7 @@ module Protobuf
|
|
246
252
|
when Google::Protobuf::DescriptorProto
|
247
253
|
current_descriptor.nested_type << descriptor
|
248
254
|
else
|
249
|
-
raise ArgumentError
|
255
|
+
raise ArgumentError, 'Invalid context'
|
250
256
|
end
|
251
257
|
end
|
252
258
|
alias message_descriptor= descriptor=
|
@@ -276,7 +282,7 @@ module Protobuf
|
|
276
282
|
#TODO: how should i distiguish between field and extension
|
277
283
|
#current_descriptor.extension << descriptor
|
278
284
|
else
|
279
|
-
raise ArgumentError
|
285
|
+
raise ArgumentError, 'Invalid context'
|
280
286
|
end
|
281
287
|
end
|
282
288
|
|
File without changes
|
@@ -11,15 +11,15 @@ module Protobuf
|
|
11
11
|
|
12
12
|
def build(proto, opt={})
|
13
13
|
mod = opt[:module]
|
14
|
-
cls = mod.const_set
|
14
|
+
cls = mod.const_set(proto.name, Class.new(@message_class))
|
15
15
|
proto.nested_type.each do |message_proto|
|
16
|
-
Protobuf::Message.descriptor.build
|
16
|
+
Protobuf::Message.descriptor.build(message_proto, :module => cls)
|
17
17
|
end
|
18
18
|
proto.enum_type.each do |enum_proto|
|
19
|
-
Protobuf::Enum.descriptor.build
|
19
|
+
Protobuf::Enum.descriptor.build(enum_proto, :module => cls)
|
20
20
|
end
|
21
21
|
proto.field.each do |field_proto|
|
22
|
-
Protobuf::Field::BaseField.descriptor.build
|
22
|
+
Protobuf::Field::BaseField.descriptor.build(field_proto, :class => cls)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -27,11 +27,11 @@ module Protobuf
|
|
27
27
|
message_proto = Google::Protobuf::DescriptorProto.new
|
28
28
|
message_proto.name = @message_class.to_s.split('::').last
|
29
29
|
@message_class.fields.each do |tag, field|
|
30
|
-
field.descriptor.unbuild
|
30
|
+
field.descriptor.unbuild(message_proto)
|
31
31
|
end
|
32
32
|
ObjectSpace.each_object(Class) do |cls|
|
33
|
-
if innerclass?
|
34
|
-
cls.descriptor.unbuild
|
33
|
+
if innerclass?(@message_class, cls)
|
34
|
+
cls.descriptor.unbuild(message_proto)
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -41,14 +41,15 @@ module Protobuf
|
|
41
41
|
when Google::Protobuf::DescriptorProto
|
42
42
|
parent_proto.nested_type << message_proto
|
43
43
|
else
|
44
|
-
raise TypeError
|
44
|
+
raise TypeError, parent_proto.class.name
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
+
private
|
49
|
+
|
48
50
|
def innerclass?(parent, child)
|
49
|
-
child.name =~ /::/
|
51
|
+
child.name =~ /::/ && child.name.split('::')[0..-2].join('::') == parent.name
|
50
52
|
end
|
51
53
|
end
|
52
54
|
end
|
53
55
|
end
|
54
|
-
|
@@ -42,7 +42,7 @@ module Protobuf
|
|
42
42
|
when Google::Protobuf::FieldDescriptorProto::Type::TYPE_SINT64
|
43
43
|
:sint64
|
44
44
|
else
|
45
|
-
raise ArgumentError
|
45
|
+
raise ArgumentError, "Invalid type: #{proto.type}"
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
@@ -100,7 +100,7 @@ module Protobuf
|
|
100
100
|
when Google::Protobuf::FieldDescriptorProto::Label::LABEL_REPEATED
|
101
101
|
:repeated
|
102
102
|
else
|
103
|
-
raise ArgumentError
|
103
|
+
raise ArgumentError, "Invalid label: #{proto.label}"
|
104
104
|
end
|
105
105
|
end
|
106
106
|
|
@@ -114,7 +114,7 @@ module Protobuf
|
|
114
114
|
when :repeated
|
115
115
|
Google::Protobuf::FieldDescriptorProto::Label::LABEL_REPEATED
|
116
116
|
else
|
117
|
-
raise ArgumentError
|
117
|
+
raise ArgumentError, "Invalid label: #{label}"
|
118
118
|
end
|
119
119
|
end
|
120
120
|
|
@@ -126,14 +126,13 @@ module Protobuf
|
|
126
126
|
end
|
127
127
|
|
128
128
|
def build(proto, opt={})
|
129
|
-
acceptable_descriptor(proto).build
|
129
|
+
acceptable_descriptor(proto).build(proto)
|
130
130
|
end
|
131
131
|
|
132
132
|
def acceptable_descriptor(proto)
|
133
133
|
Protobuf::Descriptor.constants.each do |class_name|
|
134
|
-
descriptor_class = Protobuf::Descriptor.const_get
|
135
|
-
if descriptor_class.respond_to?(:proto_type)
|
136
|
-
descriptor_class.proto_type == proto.class.name
|
134
|
+
descriptor_class = Protobuf::Descriptor.const_get(class_name)
|
135
|
+
if descriptor_class.respond_to?(:proto_type) && descriptor_class.proto_type == proto.class.name
|
137
136
|
return descriptor_class
|
138
137
|
end
|
139
138
|
end
|
@@ -2,12 +2,14 @@ require 'protobuf/message/message'
|
|
2
2
|
require 'protobuf/message/enum'
|
3
3
|
require 'protobuf/message/service'
|
4
4
|
require 'protobuf/message/extend'
|
5
|
+
|
5
6
|
module Google
|
6
7
|
module Protobuf
|
7
|
-
::Protobuf::OPTIONS[:java_package] =
|
8
|
-
::Protobuf::OPTIONS[:java_outer_classname] =
|
9
|
-
::Protobuf::OPTIONS[:optimize_for] = :SPEED
|
8
|
+
::Protobuf::OPTIONS[:"java_package"] = "com.google.protobuf"
|
9
|
+
::Protobuf::OPTIONS[:"java_outer_classname"] = "DescriptorProtos"
|
10
|
+
::Protobuf::OPTIONS[:"optimize_for"] = :SPEED
|
10
11
|
class FileDescriptorProto < ::Protobuf::Message
|
12
|
+
defined_in __FILE__
|
11
13
|
optional :string, :name, 1
|
12
14
|
optional :string, :package, 2
|
13
15
|
repeated :string, :dependency, 3
|
@@ -18,12 +20,14 @@ module Google
|
|
18
20
|
optional :FileOptions, :options, 8
|
19
21
|
end
|
20
22
|
class DescriptorProto < ::Protobuf::Message
|
23
|
+
defined_in __FILE__
|
21
24
|
optional :string, :name, 1
|
22
25
|
repeated :FieldDescriptorProto, :field, 2
|
23
26
|
repeated :FieldDescriptorProto, :extension, 6
|
24
27
|
repeated :DescriptorProto, :nested_type, 3
|
25
28
|
repeated :EnumDescriptorProto, :enum_type, 4
|
26
29
|
class ExtensionRange < ::Protobuf::Message
|
30
|
+
defined_in __FILE__
|
27
31
|
optional :int32, :start, 1
|
28
32
|
optional :int32, :end, 2
|
29
33
|
end
|
@@ -31,30 +35,33 @@ module Google
|
|
31
35
|
optional :MessageOptions, :options, 7
|
32
36
|
end
|
33
37
|
class FieldDescriptorProto < ::Protobuf::Message
|
38
|
+
defined_in __FILE__
|
34
39
|
class Type < ::Protobuf::Enum
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
40
|
+
defined_in __FILE__
|
41
|
+
define :TYPE_DOUBLE, 1
|
42
|
+
define :TYPE_FLOAT, 2
|
43
|
+
define :TYPE_INT64, 3
|
44
|
+
define :TYPE_UINT64, 4
|
45
|
+
define :TYPE_INT32, 5
|
46
|
+
define :TYPE_FIXED64, 6
|
47
|
+
define :TYPE_FIXED32, 7
|
48
|
+
define :TYPE_BOOL, 8
|
49
|
+
define :TYPE_STRING, 9
|
50
|
+
define :TYPE_GROUP, 10
|
51
|
+
define :TYPE_MESSAGE, 11
|
52
|
+
define :TYPE_BYTES, 12
|
53
|
+
define :TYPE_UINT32, 13
|
54
|
+
define :TYPE_ENUM, 14
|
55
|
+
define :TYPE_SFIXED32, 15
|
56
|
+
define :TYPE_SFIXED64, 16
|
57
|
+
define :TYPE_SINT32, 17
|
58
|
+
define :TYPE_SINT64, 18
|
53
59
|
end
|
54
60
|
class Label < ::Protobuf::Enum
|
55
|
-
|
56
|
-
|
57
|
-
|
61
|
+
defined_in __FILE__
|
62
|
+
define :LABEL_OPTIONAL, 1
|
63
|
+
define :LABEL_REQUIRED, 2
|
64
|
+
define :LABEL_REPEATED, 3
|
58
65
|
end
|
59
66
|
optional :string, :name, 1
|
60
67
|
optional :int32, :number, 3
|
@@ -66,54 +73,67 @@ module Google
|
|
66
73
|
optional :FieldOptions, :options, 8
|
67
74
|
end
|
68
75
|
class EnumDescriptorProto < ::Protobuf::Message
|
76
|
+
defined_in __FILE__
|
69
77
|
optional :string, :name, 1
|
70
78
|
repeated :EnumValueDescriptorProto, :value, 2
|
71
79
|
optional :EnumOptions, :options, 3
|
72
80
|
end
|
73
81
|
class EnumValueDescriptorProto < ::Protobuf::Message
|
82
|
+
defined_in __FILE__
|
74
83
|
optional :string, :name, 1
|
75
84
|
optional :int32, :number, 2
|
76
85
|
optional :EnumValueOptions, :options, 3
|
77
86
|
end
|
78
87
|
class ServiceDescriptorProto < ::Protobuf::Message
|
88
|
+
defined_in __FILE__
|
79
89
|
optional :string, :name, 1
|
80
90
|
repeated :MethodDescriptorProto, :method, 2
|
81
91
|
optional :ServiceOptions, :options, 3
|
82
92
|
end
|
83
93
|
class MethodDescriptorProto < ::Protobuf::Message
|
94
|
+
defined_in __FILE__
|
84
95
|
optional :string, :name, 1
|
85
96
|
optional :string, :input_type, 2
|
86
97
|
optional :string, :output_type, 3
|
87
98
|
optional :MethodOptions, :options, 4
|
88
99
|
end
|
89
100
|
class FileOptions < ::Protobuf::Message
|
101
|
+
defined_in __FILE__
|
90
102
|
optional :string, :java_package, 1
|
91
103
|
optional :string, :java_outer_classname, 8
|
92
|
-
optional :bool, :java_multiple_files, 10,
|
104
|
+
optional :bool, :java_multiple_files, 10, :default => false
|
93
105
|
class OptimizeMode < ::Protobuf::Enum
|
94
|
-
|
95
|
-
|
106
|
+
defined_in __FILE__
|
107
|
+
define :SPEED, 1
|
108
|
+
define :CODE_SIZE, 2
|
96
109
|
end
|
97
|
-
optional :OptimizeMode, :optimize_for, 9,
|
110
|
+
optional :OptimizeMode, :optimize_for, 9, :default => :CODE_SIZE
|
98
111
|
end
|
99
112
|
class MessageOptions < ::Protobuf::Message
|
100
|
-
|
113
|
+
defined_in __FILE__
|
114
|
+
optional :bool, :message_set_wire_format, 1, :default => false
|
101
115
|
end
|
102
116
|
class FieldOptions < ::Protobuf::Message
|
117
|
+
defined_in __FILE__
|
103
118
|
optional :CType, :ctype, 1
|
104
119
|
class CType < ::Protobuf::Enum
|
105
|
-
|
106
|
-
|
120
|
+
defined_in __FILE__
|
121
|
+
define :CORD, 1
|
122
|
+
define :STRING_PIECE, 2
|
107
123
|
end
|
108
124
|
optional :string, :experimental_map_key, 9
|
109
125
|
end
|
110
126
|
class EnumOptions < ::Protobuf::Message
|
127
|
+
defined_in __FILE__
|
111
128
|
end
|
112
129
|
class EnumValueOptions < ::Protobuf::Message
|
130
|
+
defined_in __FILE__
|
113
131
|
end
|
114
132
|
class ServiceOptions < ::Protobuf::Message
|
133
|
+
defined_in __FILE__
|
115
134
|
end
|
116
135
|
class MethodOptions < ::Protobuf::Message
|
136
|
+
defined_in __FILE__
|
117
137
|
end
|
118
138
|
end
|
119
139
|
end
|
@@ -11,19 +11,19 @@ module Protobuf
|
|
11
11
|
|
12
12
|
def build(proto, opt)
|
13
13
|
mod = opt[:module]
|
14
|
-
cls = mod.const_set
|
14
|
+
cls = mod.const_set(proto.name, Class.new(Protobuf::Enum))
|
15
15
|
proto.value.each do |value_proto|
|
16
|
-
cls.
|
16
|
+
cls.class_eval { define value_proto.name, value_proto.number }
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
20
|
def unbuild(parent_proto)
|
21
21
|
enum_proto = Google::Protobuf::EnumDescriptorProto.new
|
22
22
|
enum_proto.name = @enum_class.name.split('::').last
|
23
|
-
@enum_class.
|
23
|
+
@enum_class.values.each do |name, value|
|
24
24
|
enum_value_proto = Google::Protobuf::EnumValueDescriptorProto.new
|
25
|
-
enum_value_proto.name =
|
26
|
-
enum_value_proto.number =
|
25
|
+
enum_value_proto.name = name.to_s
|
26
|
+
enum_value_proto.number = value.value
|
27
27
|
enum_proto.value << enum_value_proto
|
28
28
|
end
|
29
29
|
parent_proto.enum_type << enum_proto
|
@@ -11,23 +11,23 @@ module Protobuf
|
|
11
11
|
|
12
12
|
def build(proto, opt={})
|
13
13
|
cls = opt[:class]
|
14
|
-
rule = Protobuf::Descriptor.id2label
|
15
|
-
type = Protobuf::Descriptor.id2type
|
16
|
-
type = proto.type_name.to_sym if [:message, :enum].include?
|
14
|
+
rule = Protobuf::Descriptor.id2label(proto.label)
|
15
|
+
type = Protobuf::Descriptor.id2type(proto.type)
|
16
|
+
type = proto.type_name.to_sym if [:message, :enum].include?(type)
|
17
17
|
opts = {}
|
18
18
|
opts[:default] = proto.default_value if proto.default_value
|
19
|
-
cls.define_field
|
19
|
+
cls.define_field(rule, type, proto.name, proto.number, opts)
|
20
20
|
end
|
21
21
|
|
22
22
|
def unbuild(parent_proto, extension=false)
|
23
23
|
field_proto = Google::Protobuf::FieldDescriptorProto.new
|
24
24
|
field_proto.name = @field_instance.name.to_s
|
25
25
|
field_proto.number = @field_instance.tag
|
26
|
-
field_proto.label = Protobuf::Descriptor.label2id
|
27
|
-
field_proto.type = Protobuf::Descriptor.type2id
|
26
|
+
field_proto.label = Protobuf::Descriptor.label2id(@field_instance.rule)
|
27
|
+
field_proto.type = Protobuf::Descriptor.type2id(@field_instance.type)
|
28
28
|
if [Google::Protobuf::FieldDescriptorProto::Type::TYPE_MESSAGE,
|
29
29
|
Google::Protobuf::FieldDescriptorProto::Type::TYPE_ENUM].include? field_proto.type
|
30
|
-
field_proto.type_name = @field_instance.type.to_s.split('::').last
|
30
|
+
field_proto.type_name = @field_instance.type.to_s.split('::').last
|
31
31
|
end
|
32
32
|
field_proto.default_value = @field_instance.default.to_s if @field_instance.default
|
33
33
|
|
@@ -41,10 +41,9 @@ module Protobuf
|
|
41
41
|
parent_proto.field << field_proto
|
42
42
|
end
|
43
43
|
else
|
44
|
-
raise TypeError
|
44
|
+
raise TypeError, parent_proto.class.name
|
45
45
|
end
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
49
49
|
end
|
50
|
-
|