ruby_protobuf 0.3.3 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- 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
@@ -1,85 +1,83 @@
|
|
1
1
|
require 'protobuf/common/wire_type'
|
2
|
+
require 'protobuf/common/exceptions'
|
2
3
|
|
3
4
|
module Protobuf
|
4
|
-
class InvalidWireType < StandardError; end
|
5
5
|
|
6
|
-
|
7
|
-
class <<self
|
8
|
-
def decode(stream, message)
|
9
|
-
self.new(stream, message).decode
|
10
|
-
end
|
11
|
-
end
|
6
|
+
module Decoder
|
12
7
|
|
13
|
-
|
14
|
-
@stream, @message = stream, message
|
15
|
-
end
|
8
|
+
module_function
|
16
9
|
|
17
|
-
|
10
|
+
# Read bytes from +stream+ and pass to +message+ object.
|
11
|
+
def decode(stream, message)
|
18
12
|
until stream.eof?
|
19
|
-
tag, wire_type = read_key
|
13
|
+
tag, wire_type = read_key(stream)
|
20
14
|
bytes =
|
21
15
|
case wire_type
|
22
16
|
when WireType::VARINT
|
23
|
-
read_varint
|
17
|
+
read_varint(stream)
|
24
18
|
when WireType::FIXED64
|
25
|
-
read_fixed64
|
19
|
+
read_fixed64(stream)
|
26
20
|
when WireType::LENGTH_DELIMITED
|
27
|
-
read_length_delimited
|
21
|
+
read_length_delimited(stream)
|
28
22
|
when WireType::START_GROUP
|
29
|
-
read_start_group
|
23
|
+
read_start_group(stream)
|
30
24
|
when WireType::END_GROUP
|
31
|
-
read_end_group
|
25
|
+
read_end_group(stream)
|
32
26
|
when WireType::FIXED32
|
33
|
-
read_fixed32
|
27
|
+
read_fixed32(stream)
|
34
28
|
else
|
35
|
-
raise InvalidWireType
|
29
|
+
raise InvalidWireType, wire_type
|
36
30
|
end
|
37
|
-
message.set_field
|
31
|
+
message.set_field(tag, bytes)
|
38
32
|
end
|
39
33
|
message
|
40
34
|
end
|
41
35
|
|
42
|
-
|
43
|
-
|
36
|
+
# Read key pair (tag and wire-type) from +stream+.
|
44
37
|
def read_key(stream)
|
45
|
-
|
46
|
-
bits = Protobuf::Field::VarintField.decode bytes
|
38
|
+
bits = read_varint(stream)
|
47
39
|
wire_type = bits & 0x07
|
48
40
|
tag = bits >> 3
|
49
41
|
[tag, wire_type]
|
50
42
|
end
|
51
43
|
|
44
|
+
# Read varint integer value from +stream+.
|
52
45
|
def read_varint(stream)
|
53
46
|
read_method = stream.respond_to?(:readbyte) ? :readbyte : :readchar
|
54
|
-
|
47
|
+
value = index = 0
|
55
48
|
begin
|
56
|
-
byte = stream.
|
57
|
-
|
49
|
+
byte = stream.__send__(read_method)
|
50
|
+
value |= (byte & 0x7f) << (7 * index)
|
51
|
+
index += 1
|
58
52
|
end while (byte & 0x80).nonzero?
|
59
|
-
|
53
|
+
value
|
60
54
|
end
|
61
55
|
|
56
|
+
# Read 32-bit string value from +stream+.
|
62
57
|
def read_fixed32(stream)
|
63
58
|
stream.read(4)
|
64
59
|
end
|
65
60
|
|
61
|
+
# Read 64-bit string value from +stream+.
|
66
62
|
def read_fixed64(stream)
|
67
63
|
stream.read(8)
|
68
64
|
end
|
69
65
|
|
66
|
+
# Read length-delimited string value from +stream+.
|
70
67
|
def read_length_delimited(stream)
|
71
|
-
|
72
|
-
value_length
|
73
|
-
value = stream.read value_length
|
74
|
-
value.unpack('C*')
|
68
|
+
value_length = read_varint(stream)
|
69
|
+
stream.read(value_length)
|
75
70
|
end
|
76
71
|
|
72
|
+
# Not implemented.
|
77
73
|
def read_start_group(stream)
|
78
|
-
raise NotImplementedError
|
74
|
+
raise NotImplementedError, 'Group is deprecated.'
|
79
75
|
end
|
80
|
-
|
76
|
+
|
77
|
+
# Not implemented.
|
81
78
|
def read_end_group(stream)
|
82
|
-
raise NotImplementedError
|
79
|
+
raise NotImplementedError, 'Group is deprecated.'
|
83
80
|
end
|
81
|
+
|
84
82
|
end
|
85
83
|
end
|
@@ -1,40 +1,44 @@
|
|
1
1
|
require 'protobuf/common/wire_type'
|
2
|
+
require 'protobuf/common/exceptions'
|
2
3
|
|
3
4
|
module Protobuf
|
4
|
-
class NotInitializedError < StandardError; end
|
5
5
|
|
6
|
-
|
7
|
-
class <<self
|
8
|
-
def encode(stream, message)
|
9
|
-
self.new(stream, message).encode
|
10
|
-
end
|
11
|
-
end
|
6
|
+
module Encoder
|
12
7
|
|
13
|
-
|
14
|
-
@stream, @message = stream, message
|
15
|
-
end
|
8
|
+
module_function
|
16
9
|
|
17
|
-
|
10
|
+
# Encode +message+ and write to +stream+.
|
11
|
+
def encode(stream, message)
|
18
12
|
raise NotInitializedError unless message.initialized?
|
19
13
|
message.each_field do |field, value|
|
20
14
|
next unless message.has_field?(field.name)
|
21
15
|
|
22
16
|
if field.repeated?
|
23
|
-
|
24
|
-
|
17
|
+
if field.packed?
|
18
|
+
key = (field.tag << 3) | WireType::LENGTH_DELIMITED
|
19
|
+
packed_value = value.map {|val| field.encode(val) }.join
|
20
|
+
stream.write(Field::VarintField.encode(key))
|
21
|
+
stream.write(Field::VarintField.encode(packed_value.size))
|
22
|
+
stream.write(packed_value)
|
23
|
+
else
|
24
|
+
value.each do |val|
|
25
|
+
write_pair(stream, field, val)
|
26
|
+
end
|
25
27
|
end
|
26
28
|
else
|
27
|
-
write_pair field, value
|
29
|
+
write_pair(stream, field, value)
|
28
30
|
end
|
29
31
|
end
|
30
32
|
end
|
31
33
|
|
32
|
-
|
34
|
+
# Encode key and value, and write to +stream+.
|
35
|
+
def write_pair(stream, field, value)
|
33
36
|
key = (field.tag << 3) | field.wire_type
|
34
|
-
key_bytes =
|
35
|
-
stream.write
|
36
|
-
bytes = field.
|
37
|
-
stream.write
|
37
|
+
key_bytes = Field::VarintField.encode(key)
|
38
|
+
stream.write(key_bytes)
|
39
|
+
bytes = field.encode(value)
|
40
|
+
stream.write(bytes)
|
38
41
|
end
|
42
|
+
|
39
43
|
end
|
40
44
|
end
|
@@ -1,28 +1,62 @@
|
|
1
|
+
require 'delegate'
|
1
2
|
require 'protobuf/descriptor/enum_descriptor'
|
2
3
|
require 'protobuf/message/protoable'
|
3
4
|
|
4
5
|
module Protobuf
|
5
6
|
class Enum
|
6
7
|
class <<self
|
7
|
-
include
|
8
|
+
include Protoable
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
10
|
+
attr_reader :values
|
11
|
+
|
12
|
+
def name_by_value(value)
|
13
|
+
if not defined?(@values)
|
14
|
+
constants.find {|c| const_get(c) == value} # for compatibility
|
15
|
+
else
|
16
|
+
@values_index ||= @values.inject({}) {|hash, (n, v)| hash[v.value] = n; hash }
|
17
|
+
@values_index[value]
|
12
18
|
end
|
13
19
|
end
|
14
20
|
|
21
|
+
alias get_name_by_tag name_by_value
|
22
|
+
|
15
23
|
def valid_tag?(tag)
|
16
|
-
|
24
|
+
!! name_by_value(tag)
|
17
25
|
end
|
18
26
|
|
19
|
-
def
|
20
|
-
|
27
|
+
def descriptor
|
28
|
+
@descriptor ||= Descriptor::EnumDescriptor.new(self)
|
21
29
|
end
|
22
30
|
|
23
|
-
|
24
|
-
|
31
|
+
private
|
32
|
+
|
33
|
+
def define(name, value)
|
34
|
+
enum_value = EnumValue.new(self, name, value)
|
35
|
+
const_set(name, enum_value)
|
36
|
+
@values ||= {}
|
37
|
+
@values[name] = enum_value
|
25
38
|
end
|
26
39
|
end
|
27
40
|
end
|
41
|
+
|
42
|
+
class EnumValue < SimpleDelegator
|
43
|
+
|
44
|
+
attr_reader :parent_class, :name, :value
|
45
|
+
|
46
|
+
def initialize(parent_class, name, value)
|
47
|
+
@parent_class = parent_class
|
48
|
+
@name = name
|
49
|
+
@value = value
|
50
|
+
super(@value)
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_s
|
54
|
+
@name.to_s
|
55
|
+
end
|
56
|
+
|
57
|
+
def inspect
|
58
|
+
"\#<#{self.class} #{@parent_class}::#{@name}=#{@value}>"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
28
62
|
end
|