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.
Files changed (78) hide show
  1. data/History.txt +10 -2
  2. data/Manifest.txt +26 -12
  3. data/README.txt +6 -1
  4. data/Rakefile +51 -14
  5. data/TODO +15 -0
  6. data/VERSION +1 -0
  7. data/bin/rprotoc +11 -6
  8. data/lib/protobuf/common/exceptions.rb +11 -0
  9. data/lib/protobuf/common/util.rb +9 -0
  10. data/lib/protobuf/common/wire_type.rb +6 -6
  11. data/lib/protobuf/compiler/compiler.rb +16 -16
  12. data/lib/protobuf/compiler/nodes.rb +67 -65
  13. data/lib/protobuf/compiler/proto.y +31 -38
  14. data/lib/protobuf/compiler/proto_parser.rb +315 -294
  15. data/lib/protobuf/compiler/template/rpc_bin.erb +1 -1
  16. data/lib/protobuf/compiler/template/rpc_client.erb +1 -1
  17. data/lib/protobuf/compiler/template/rpc_service.erb +2 -2
  18. data/lib/protobuf/compiler/visitors.rb +45 -39
  19. data/lib/protobuf/descriptor/descriptor.proto +0 -0
  20. data/lib/protobuf/descriptor/descriptor.rb +11 -10
  21. data/lib/protobuf/descriptor/descriptor_builder.rb +6 -7
  22. data/lib/protobuf/descriptor/descriptor_proto.rb +51 -31
  23. data/lib/protobuf/descriptor/enum_descriptor.rb +5 -5
  24. data/lib/protobuf/descriptor/field_descriptor.rb +8 -9
  25. data/lib/protobuf/descriptor/file_descriptor.rb +0 -1
  26. data/lib/protobuf/message/decoder.rb +33 -35
  27. data/lib/protobuf/message/encoder.rb +23 -19
  28. data/lib/protobuf/message/enum.rb +43 -9
  29. data/lib/protobuf/message/field.rb +281 -193
  30. data/lib/protobuf/message/message.rb +166 -110
  31. data/lib/protobuf/message/protoable.rb +4 -3
  32. data/lib/protobuf/message/service.rb +1 -1
  33. data/lib/protobuf/rpc/client.rb +3 -3
  34. data/lib/protobuf/rpc/handler.rb +1 -1
  35. data/lib/protobuf/rpc/server.rb +8 -8
  36. data/lib/ruby_protobuf.rb +1 -1
  37. data/test/check_unbuild.rb +7 -7
  38. data/test/proto/addressbook.pb.rb +67 -0
  39. data/test/proto/addressbook.proto +2 -0
  40. data/test/proto/addressbook_base.pb.rb +59 -0
  41. data/test/proto/addressbook_base.proto +1 -1
  42. data/test/proto/addressbook_ext.pb.rb +21 -0
  43. data/test/proto/addressbook_ext.proto +2 -2
  44. data/test/{collision.rb → proto/collision.pb.rb} +0 -0
  45. data/test/{ext_collision.rb → proto/ext_collision.pb.rb} +1 -1
  46. data/test/{ext_range.rb → proto/ext_range.pb.rb} +4 -4
  47. data/test/proto/ext_range.proto +2 -2
  48. data/test/proto/float_default.proto +10 -0
  49. data/test/proto/lowercase.pb.rb +31 -0
  50. data/test/proto/lowercase.proto +9 -0
  51. data/test/{merge.rb → proto/merge.pb.rb} +2 -2
  52. data/test/proto/nested.pb.rb +31 -0
  53. data/test/proto/nested.proto +2 -0
  54. data/test/proto/optional_field.pb.rb +36 -0
  55. data/test/proto/optional_field.proto +12 -0
  56. data/test/proto/packed.pb.rb +23 -0
  57. data/test/proto/packed.proto +6 -0
  58. data/test/{types.rb → proto/types.pb.rb} +43 -1
  59. data/test/test_addressbook.rb +30 -17
  60. data/test/test_compiler.rb +79 -78
  61. data/test/test_descriptor.rb +12 -12
  62. data/test/test_enum_value.rb +41 -0
  63. data/test/test_extension.rb +10 -14
  64. data/test/test_lowercase.rb +11 -0
  65. data/test/test_message.rb +44 -41
  66. data/test/test_optional_field.rb +61 -38
  67. data/test/test_packed_field.rb +40 -0
  68. data/test/test_parse.rb +8 -8
  69. data/test/test_repeated_types.rb +29 -3
  70. data/test/test_serialize.rb +12 -12
  71. data/test/test_standard_message.rb +30 -30
  72. data/test/test_types.rb +95 -95
  73. metadata +69 -39
  74. data/test/addressbook.rb +0 -98
  75. data/test/addressbook_base.rb +0 -62
  76. data/test/addressbook_ext.rb +0 -12
  77. data/test/nested.rb +0 -25
  78. 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
- class Decoder
7
- class <<self
8
- def decode(stream, message)
9
- self.new(stream, message).decode
10
- end
11
- end
6
+ module Decoder
12
7
 
13
- def initialize(stream=nil, message=nil)
14
- @stream, @message = stream, message
15
- end
8
+ module_function
16
9
 
17
- def decode(stream=@stream, message=@message)
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 stream
13
+ tag, wire_type = read_key(stream)
20
14
  bytes =
21
15
  case wire_type
22
16
  when WireType::VARINT
23
- read_varint stream
17
+ read_varint(stream)
24
18
  when WireType::FIXED64
25
- read_fixed64 stream
19
+ read_fixed64(stream)
26
20
  when WireType::LENGTH_DELIMITED
27
- read_length_delimited stream
21
+ read_length_delimited(stream)
28
22
  when WireType::START_GROUP
29
- read_start_group stream
23
+ read_start_group(stream)
30
24
  when WireType::END_GROUP
31
- read_end_group stream
25
+ read_end_group(stream)
32
26
  when WireType::FIXED32
33
- read_fixed32 stream
27
+ read_fixed32(stream)
34
28
  else
35
- raise InvalidWireType.new(wire_type)
29
+ raise InvalidWireType, wire_type
36
30
  end
37
- message.set_field tag, bytes
31
+ message.set_field(tag, bytes)
38
32
  end
39
33
  message
40
34
  end
41
35
 
42
- protected
43
-
36
+ # Read key pair (tag and wire-type) from +stream+.
44
37
  def read_key(stream)
45
- bytes = read_varint stream
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
- bytes = []
47
+ value = index = 0
55
48
  begin
56
- byte = stream.send(read_method)
57
- bytes << (byte & 0x7f)
49
+ byte = stream.__send__(read_method)
50
+ value |= (byte & 0x7f) << (7 * index)
51
+ index += 1
58
52
  end while (byte & 0x80).nonzero?
59
- bytes
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
- bytes = read_varint stream
72
- value_length = Protobuf::Field::VarintField.decode bytes
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.new('Group is duplecated.')
74
+ raise NotImplementedError, 'Group is deprecated.'
79
75
  end
80
-
76
+
77
+ # Not implemented.
81
78
  def read_end_group(stream)
82
- raise NotImplementedError.new('Group is duplecated.')
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
- class Encoder
7
- class <<self
8
- def encode(stream, message)
9
- self.new(stream, message).encode
10
- end
11
- end
6
+ module Encoder
12
7
 
13
- def initialize(stream=nil, message=nil)
14
- @stream, @message = stream, message
15
- end
8
+ module_function
16
9
 
17
- def encode(stream=@stream, message=@message)
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
- value.each do |val|
24
- write_pair field, val, stream
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, stream
29
+ write_pair(stream, field, value)
28
30
  end
29
31
  end
30
32
  end
31
33
 
32
- def write_pair(field, value, stream)
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 = Protobuf::Field::VarintField.encode key
35
- stream.write key_bytes
36
- bytes = field.get value
37
- stream.write bytes
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 Protobuf::Protoable
8
+ include Protoable
8
9
 
9
- def get_name_by_tag(tag)
10
- constants.find do |name|
11
- const_get(name) == tag
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
- not get_name_by_tag(tag).nil?
24
+ !! name_by_value(tag)
17
25
  end
18
26
 
19
- def name_by_value(value)
20
- constants.find {|c| const_get(c) == value}
27
+ def descriptor
28
+ @descriptor ||= Descriptor::EnumDescriptor.new(self)
21
29
  end
22
30
 
23
- def descriptor
24
- @descriptor ||= Protobuf::Descriptor::EnumDescriptor.new(self)
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