ruby_protobuf 0.1.0 → 0.2.0

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.
@@ -1,6 +1,8 @@
1
- === 0.1.0 / 2008-08-01
1
+ === 0.2.0 / 2008-08-05
2
2
 
3
- * 2 fixed a small bug
4
- * 1 major enhancement
3
+ * 0.2.0 introduce descriptor (rev.75)
4
+ * Message structures can be restored from encoded data by means a descriptor object
5
+ * 0.1.0 fixed a small bug
6
+ * 0.0.1 major enhancement
5
7
  * Birthday!
6
8
 
@@ -3,26 +3,35 @@ Manifest.txt
3
3
  README.txt
4
4
  Rakefile
5
5
  bin/rprotoc
6
- bin/ruby_protobuf
7
- lib/protobuf/compiler.rb
8
- lib/protobuf/decoder.rb
9
- lib/protobuf/encoder.rb
10
- lib/protobuf/enum.rb
11
- lib/protobuf/extend.rb
12
- lib/protobuf/field.rb
13
- lib/protobuf/message.rb
14
- lib/protobuf/parser.y
15
- lib/protobuf/service.rb
16
- lib/protobuf/wire_type.rb
6
+ lib/protobuf/common/wire_type.rb
7
+ lib/protobuf/compiler/compiler.rb
8
+ lib/protobuf/compiler/parser.y
9
+ lib/protobuf/descriptor/descriptor.proto
10
+ lib/protobuf/descriptor/descriptor.rb
11
+ lib/protobuf/descriptor/descriptor_builder.rb
12
+ lib/protobuf/descriptor/descriptor_proto.rb
13
+ lib/protobuf/descriptor/enum_descriptor.rb
14
+ lib/protobuf/descriptor/field_descriptor.rb
15
+ lib/protobuf/descriptor/file_descriptor.rb
16
+ lib/protobuf/message/decoder.rb
17
+ lib/protobuf/message/encoder.rb
18
+ lib/protobuf/message/enum.rb
19
+ lib/protobuf/message/extend.rb
20
+ lib/protobuf/message/field.rb
21
+ lib/protobuf/message/message.rb
22
+ lib/protobuf/message/service.rb
17
23
  lib/ruby_protobuf.rb
18
24
  test/addressbook.proto
19
25
  test/addressbook.rb
26
+ test/check_unbuild.rb
20
27
  test/data/data.bin
28
+ test/data/data2.bin
21
29
  test/data/data_source.py
22
30
  test/data/types.bin
23
31
  test/data/types_source.py
24
32
  test/test_addressbook.rb
25
33
  test/test_compiler.rb
34
+ test/test_descriptor.rb
26
35
  test/test_message.rb
27
36
  test/test_parse.rb
28
37
  test/test_ruby_protobuf.rb
data/README.txt CHANGED
@@ -14,7 +14,7 @@ Protocol Buffers for Ruby.
14
14
 
15
15
  == SYNOPSIS:
16
16
 
17
- bin/rprotoc -I lib test/addressbook.proto
17
+ rprotoc test/addressbook.proto
18
18
 
19
19
  == REQUIREMENTS:
20
20
 
@@ -12,10 +12,10 @@ module Protobuf
12
12
  def initialize
13
13
  @indent_level = 0
14
14
  @ret = <<-eos
15
- require 'protobuf/message'
16
- require 'protobuf/enum'
17
- require 'protobuf/service'
18
- require 'protobuf/extend'
15
+ require 'protobuf/message/message'
16
+ require 'protobuf/message/enum'
17
+ require 'protobuf/message/service'
18
+ require 'protobuf/message/extend'
19
19
 
20
20
  eos
21
21
  end
@@ -33,7 +33,7 @@ require 'protobuf/extend'
33
33
  def compile(filename)
34
34
  File.open filename, 'r' do |file|
35
35
  file.each_line do |line|
36
- line.sub! /^(.*)\/\/.*/, '\1'
36
+ line.sub!(/^(.*)\/\/.*/, '\1')
37
37
  line.strip!
38
38
  case line
39
39
  when /^package\s+(\w+(\.\w+)?)\s*;$/
@@ -42,7 +42,7 @@ require 'protobuf/extend'
42
42
  @indent_level += 1
43
43
  end
44
44
  when /^message\s+(\w+)\s*\{$/
45
- putswi "class #{$1} < Protobuf::Message"
45
+ putswi "class #{$1} < ::Protobuf::Message"
46
46
  @indent_level += 1
47
47
  when /^(required|optional|repeated)\s+(\w+(\.\w+)?)\s+(\w+)\s*=\s*(\d+)\s*(\[\s*default\s*=\s*(\w+)\s*\])?\s*;$/
48
48
  rule, type, name, tag, default = $1, $2, $4, $5, $7
@@ -53,26 +53,26 @@ require 'protobuf/extend'
53
53
  end
54
54
  putswi "#{rule} :#{type}, :#{name}, #{tag}#{default}"
55
55
  when /^enum\s+(\w+)\s*\{$/
56
- putswi "class #{$1} < Protobuf::Enum"
56
+ putswi "class #{$1} < ::Protobuf::Enum"
57
57
  @indent_level += 1
58
58
  when /^(\w+)\s*=\s*(\w+)\s*;$/
59
59
  putswi "#{$1} = #{$2}"
60
60
  when /^extensions\s+(\w+)\s+to\s+(\w+)\s*;/
61
61
  low, high = $1, $2
62
- low = 'Protobuf::Extend.MIN' if low == 'min'
63
- high = 'Protobuf::Extend.MAX' if high == 'max'
62
+ low = '::Protobuf::Extend.MIN' if low == 'min'
63
+ high = '::Protobuf::Extend.MAX' if high == 'max'
64
64
  putswi "extensions #{min}..#{max}"
65
65
  when /^extend\s+(\w+)\s*\{/
66
- putswi "class #{$1} < Protobuf::Extend"
66
+ putswi "class #{$1} < ::Protobuf::Extend"
67
67
  @indent_level += 1
68
68
  when /^service\s+(\w+)\s*\{/
69
- putswi "class #{$1} < Protobuf::Service"
69
+ putswi "class #{$1} < ::Protobuf::Service"
70
70
  @indent_level += 1
71
71
  when /^rpc\s+(\w+)\s+\(\s*(\w+)\s*\)\s+returns\s+\(\s*(\w+)\s*\)\s*;/
72
72
  putswi "rpc :#{$1} => :#{$2}, :#{$3} => :#{$4}"
73
73
  when /^option\s+(\w+)\s*=\s*(.+)\s*;/
74
- putswi "Protobuf::OPTIONS[:#{$1}] = :#{$2}"
75
- when /^\}$/
74
+ putswi "::Protobuf::OPTIONS[:#{$1}] = :#{$2}"
75
+ when /^\}\s*;?$/
76
76
  @indent_level -= 1
77
77
  putswi "end"
78
78
  when ''
@@ -0,0 +1,286 @@
1
+ // Protocol Buffers - Google's data interchange format
2
+ // Copyright 2008 Google Inc.
3
+ // http://code.google.com/p/protobuf/
4
+ //
5
+ // Licensed under the Apache License, Version 2.0 (the "License");
6
+ // you may not use this file except in compliance with the License.
7
+ // You may obtain a copy of the License at
8
+ //
9
+ // http://www.apache.org/licenses/LICENSE-2.0
10
+ //
11
+ // Unless required by applicable law or agreed to in writing, software
12
+ // distributed under the License is distributed on an "AS IS" BASIS,
13
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ // See the License for the specific language governing permissions and
15
+ // limitations under the License.
16
+
17
+ // Author: kenton@google.com (Kenton Varda)
18
+ // Based on original Protocol Buffers design by
19
+ // Sanjay Ghemawat, Jeff Dean, and others.
20
+ //
21
+ // The messages in this file describe the definitions found in .proto files.
22
+ // A valid .proto file can be translated directly to a FileDescriptorProto
23
+ // without any other information (e.g. without reading its imports).
24
+
25
+
26
+
27
+ package google.protobuf;
28
+ option java_package = "com.google.protobuf";
29
+ option java_outer_classname = "DescriptorProtos";
30
+
31
+ // descriptor.proto must be optimized for speed because reflection-based
32
+ // algorithms don't work during bootstrapping.
33
+ option optimize_for = SPEED;
34
+
35
+ // Describes a complete .proto file.
36
+ message FileDescriptorProto {
37
+ optional string name = 1; // file name, relative to root of source tree
38
+ optional string package = 2; // e.g. "foo", "foo.bar", etc.
39
+
40
+ // Names of files imported by this file.
41
+ repeated string dependency = 3;
42
+
43
+ // All top-level definitions in this file.
44
+ repeated DescriptorProto message_type = 4;
45
+ repeated EnumDescriptorProto enum_type = 5;
46
+ repeated ServiceDescriptorProto service = 6;
47
+ repeated FieldDescriptorProto extension = 7;
48
+
49
+ optional FileOptions options = 8;
50
+ }
51
+
52
+ // Describes a message type.
53
+ message DescriptorProto {
54
+ optional string name = 1;
55
+
56
+ repeated FieldDescriptorProto field = 2;
57
+ repeated FieldDescriptorProto extension = 6;
58
+
59
+ repeated DescriptorProto nested_type = 3;
60
+ repeated EnumDescriptorProto enum_type = 4;
61
+
62
+ message ExtensionRange {
63
+ optional int32 start = 1;
64
+ optional int32 end = 2;
65
+ }
66
+ repeated ExtensionRange extension_range = 5;
67
+
68
+ optional MessageOptions options = 7;
69
+ }
70
+
71
+ // Describes a field within a message.
72
+ message FieldDescriptorProto {
73
+ enum Type {
74
+ // 0 is reserved for errors.
75
+ // Order is weird for historical reasons.
76
+ TYPE_DOUBLE = 1;
77
+ TYPE_FLOAT = 2;
78
+ TYPE_INT64 = 3; // Not ZigZag encoded. Negative numbers
79
+ // take 10 bytes. Use TYPE_SINT64 if negative
80
+ // values are likely.
81
+ TYPE_UINT64 = 4;
82
+ TYPE_INT32 = 5; // Not ZigZag encoded. Negative numbers
83
+ // take 10 bytes. Use TYPE_SINT32 if negative
84
+ // values are likely.
85
+ TYPE_FIXED64 = 6;
86
+ TYPE_FIXED32 = 7;
87
+ TYPE_BOOL = 8;
88
+ TYPE_STRING = 9;
89
+ TYPE_GROUP = 10; // Tag-delimited aggregate.
90
+ TYPE_MESSAGE = 11; // Length-delimited aggregate.
91
+
92
+ // New in version 2.
93
+ TYPE_BYTES = 12;
94
+ TYPE_UINT32 = 13;
95
+ TYPE_ENUM = 14;
96
+ TYPE_SFIXED32 = 15;
97
+ TYPE_SFIXED64 = 16;
98
+ TYPE_SINT32 = 17; // Uses ZigZag encoding.
99
+ TYPE_SINT64 = 18; // Uses ZigZag encoding.
100
+ };
101
+
102
+ enum Label {
103
+ // 0 is reserved for errors
104
+ LABEL_OPTIONAL = 1;
105
+ LABEL_REQUIRED = 2;
106
+ LABEL_REPEATED = 3;
107
+ // TODO(sanjay): Should we add LABEL_MAP?
108
+ };
109
+
110
+ optional string name = 1;
111
+ optional int32 number = 3;
112
+ optional Label label = 4;
113
+
114
+ // If type_name is set, this need not be set. If both this and type_name
115
+ // are set, this must be either TYPE_ENUM or TYPE_MESSAGE.
116
+ optional Type type = 5;
117
+
118
+ // For message and enum types, this is the name of the type. If the name
119
+ // starts with a '.', it is fully-qualified. Otherwise, C++-like scoping
120
+ // rules are used to find the type (i.e. first the nested types within this
121
+ // message are searched, then within the parent, on up to the root
122
+ // namespace).
123
+ optional string type_name = 6;
124
+
125
+ // For extensions, this is the name of the type being extended. It is
126
+ // resolved in the same manner as type_name.
127
+ optional string extendee = 2;
128
+
129
+ // For numeric types, contains the original text representation of the value.
130
+ // For booleans, "true" or "false".
131
+ // For strings, contains the default text contents (not escaped in any way).
132
+ // For bytes, contains the C escaped value. All bytes >= 128 are escaped.
133
+ // TODO(kenton): Base-64 encode?
134
+ optional string default_value = 7;
135
+
136
+ optional FieldOptions options = 8;
137
+ }
138
+
139
+ // Describes an enum type.
140
+ message EnumDescriptorProto {
141
+ optional string name = 1;
142
+
143
+ repeated EnumValueDescriptorProto value = 2;
144
+
145
+ optional EnumOptions options = 3;
146
+ }
147
+
148
+ // Describes a value within an enum.
149
+ message EnumValueDescriptorProto {
150
+ optional string name = 1;
151
+ optional int32 number = 2;
152
+
153
+ optional EnumValueOptions options = 3;
154
+ }
155
+
156
+ // Describes a service.
157
+ message ServiceDescriptorProto {
158
+ optional string name = 1;
159
+ repeated MethodDescriptorProto method = 2;
160
+
161
+ optional ServiceOptions options = 3;
162
+ }
163
+
164
+ // Describes a method of a service.
165
+ message MethodDescriptorProto {
166
+ optional string name = 1;
167
+
168
+ // Input and output type names. These are resolved in the same way as
169
+ // FieldDescriptorProto.type_name, but must refer to a message type.
170
+ optional string input_type = 2;
171
+ optional string output_type = 3;
172
+
173
+ optional MethodOptions options = 4;
174
+ }
175
+
176
+ // ===================================================================
177
+ // Options
178
+
179
+ // Each of the definitions above may have "options" attached. These are
180
+ // just annotations which may cause code to be generated slightly differently
181
+ // or may contain hints for code that manipulates protocol messages.
182
+
183
+ // TODO(kenton): Allow extensions to options.
184
+
185
+ message FileOptions {
186
+
187
+ // Sets the Java package where classes generated from this .proto will be
188
+ // placed. By default, the proto package is used, but this is often
189
+ // inappropriate because proto packages do not normally start with backwards
190
+ // domain names.
191
+ optional string java_package = 1;
192
+
193
+
194
+ // If set, all the classes from the .proto file are wrapped in a single
195
+ // outer class with the given name. This applies to both Proto1
196
+ // (equivalent to the old "--one_java_file" option) and Proto2 (where
197
+ // a .proto always translates to a single class, but you may want to
198
+ // explicitly choose the class name).
199
+ optional string java_outer_classname = 8;
200
+
201
+ // If set true, then the Java code generator will generate a separate .java
202
+ // file for each top-level message, enum, and service defined in the .proto
203
+ // file. Thus, these types will *not* be nested inside the outer class
204
+ // named by java_outer_classname. However, the outer class will still be
205
+ // generated to contain the file's getDescriptor() method as well as any
206
+ // top-level extensions defined in the file.
207
+ optional bool java_multiple_files = 10 [default=false];
208
+
209
+ // Generated classes can be optimized for speed or code size.
210
+ enum OptimizeMode {
211
+ SPEED = 1; // Generate complete code for parsing, serialization, etc.
212
+ CODE_SIZE = 2; // Use ReflectionOps to implement these methods.
213
+ }
214
+ optional OptimizeMode optimize_for = 9 [default=CODE_SIZE];
215
+ }
216
+
217
+ message MessageOptions {
218
+ // Set true to use the old proto1 MessageSet wire format for extensions.
219
+ // This is provided for backwards-compatibility with the MessageSet wire
220
+ // format. You should not use this for any other reason: It's less
221
+ // efficient, has fewer features, and is more complicated.
222
+ //
223
+ // The message must be defined exactly as follows:
224
+ // message Foo {
225
+ // option message_set_wire_format = true;
226
+ // extensions 4 to max;
227
+ // }
228
+ // Note that the message cannot have any defined fields; MessageSets only
229
+ // have extensions.
230
+ //
231
+ // All extensions of your type must be singular messages; e.g. they cannot
232
+ // be int32s, enums, or repeated messages.
233
+ //
234
+ // Because this is an option, the above two restrictions are not enforced by
235
+ // the protocol compiler.
236
+ optional bool message_set_wire_format = 1 [default=false];
237
+ }
238
+
239
+ message FieldOptions {
240
+ // The ctype option instructs the C++ code generator to use a different
241
+ // representation of the field than it normally would. See the specific
242
+ // options below. This option is not yet implemented in the open source
243
+ // release -- sorry, we'll try to include it in a future version!
244
+ optional CType ctype = 1;
245
+ enum CType {
246
+ CORD = 1;
247
+
248
+ STRING_PIECE = 2;
249
+ }
250
+
251
+ // EXPERIMENTAL. DO NOT USE.
252
+ // For "map" fields, the name of the field in the enclosed type that
253
+ // is the key for this map. For example, suppose we have:
254
+ // message Item {
255
+ // required string name = 1;
256
+ // required string value = 2;
257
+ // }
258
+ // message Config {
259
+ // repeated Item items = 1 [experimental_map_key="name"];
260
+ // }
261
+ // In this situation, the map key for Item will be set to "name".
262
+ // TODO: Fully-implement this, then remove the "experimental_" prefix.
263
+ optional string experimental_map_key = 9;
264
+ }
265
+
266
+ message EnumOptions {
267
+ }
268
+
269
+ message EnumValueOptions {
270
+ }
271
+
272
+ message ServiceOptions {
273
+
274
+ // Note: Field numbers 1 through 32 are reserved for Google's internal RPC
275
+ // framework. We apologize for hoarding these numbers to ourselves, but
276
+ // we were already using them long before we decided to release Protocol
277
+ // Buffers.
278
+ }
279
+
280
+ message MethodOptions {
281
+
282
+ // Note: Field numbers 1 through 32 are reserved for Google's internal RPC
283
+ // framework. We apologize for hoarding these numbers to ourselves, but
284
+ // we were already using them long before we decided to release Protocol
285
+ // Buffers.
286
+ }
@@ -0,0 +1,54 @@
1
+ module Protobuf
2
+ module Descriptor
3
+ class Descriptor
4
+ def initialize(message_class)
5
+ @message_class = message_class
6
+ end
7
+
8
+ def proto_type
9
+ 'Google::Protobuf::DescriptorProto'
10
+ end
11
+
12
+ def build(proto, opt={})
13
+ mod = opt[:module]
14
+ cls = mod.const_set proto.name, Class.new(@message_class)
15
+ proto.nested_type.each do |message_proto|
16
+ Protobuf::Message.descriptor.build message_proto, :module => cls
17
+ end
18
+ proto.enum_type.each do |enum_proto|
19
+ Protobuf::Enum.descriptor.build enum_proto, :module => cls
20
+ end
21
+ proto.field.each do |field_proto|
22
+ Protobuf::Field::BaseField.descriptor.build field_proto, :class => cls
23
+ end
24
+ end
25
+
26
+ def unbuild(parent_proto)
27
+ message_proto = Google::Protobuf::DescriptorProto.new
28
+ message_proto.name = @message_class.to_s.split('::').last
29
+ @message_class.fields.each do |tag, field|
30
+ field.descriptor.unbuild message_proto
31
+ end
32
+ ObjectSpace.each_object(Class) do |cls|
33
+ if innerclass? @message_class, cls
34
+ cls.descriptor.unbuild message_proto
35
+ end
36
+ end
37
+
38
+ case parent_proto
39
+ when Google::Protobuf::FileDescriptorProto
40
+ parent_proto.message_type << message_proto
41
+ when Google::Protobuf::DescriptorProto
42
+ parent_proto.nested_type << message_proto
43
+ else
44
+ raise TypeError.new(parent_proto.class.name)
45
+ end
46
+ end
47
+
48
+ def innerclass?(parent, child)
49
+ child.name =~ /::/ and child.name.split('::')[0..-2].join('::') == parent.name
50
+ end
51
+ end
52
+ end
53
+ end
54
+