protobuf-core 3.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +18 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +23 -0
  8. data/Rakefile +5 -0
  9. data/bin/protoc-gen-ruby +16 -0
  10. data/lib/protobuf.rb +27 -0
  11. data/lib/protobuf/code_generator.rb +44 -0
  12. data/lib/protobuf/core.rb +2 -0
  13. data/lib/protobuf/core/version.rb +5 -0
  14. data/lib/protobuf/decoder.rb +73 -0
  15. data/lib/protobuf/deprecation.rb +112 -0
  16. data/lib/protobuf/descriptors.rb +3 -0
  17. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +54 -0
  18. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +251 -0
  19. data/lib/protobuf/encoder.rb +67 -0
  20. data/lib/protobuf/enum.rb +303 -0
  21. data/lib/protobuf/exceptions.rb +9 -0
  22. data/lib/protobuf/field.rb +74 -0
  23. data/lib/protobuf/field/base_field.rb +267 -0
  24. data/lib/protobuf/field/bool_field.rb +59 -0
  25. data/lib/protobuf/field/bytes_field.rb +82 -0
  26. data/lib/protobuf/field/double_field.rb +25 -0
  27. data/lib/protobuf/field/enum_field.rb +68 -0
  28. data/lib/protobuf/field/field_array.rb +87 -0
  29. data/lib/protobuf/field/fixed32_field.rb +25 -0
  30. data/lib/protobuf/field/fixed64_field.rb +28 -0
  31. data/lib/protobuf/field/float_field.rb +41 -0
  32. data/lib/protobuf/field/int32_field.rb +21 -0
  33. data/lib/protobuf/field/int64_field.rb +21 -0
  34. data/lib/protobuf/field/integer_field.rb +23 -0
  35. data/lib/protobuf/field/message_field.rb +65 -0
  36. data/lib/protobuf/field/sfixed32_field.rb +27 -0
  37. data/lib/protobuf/field/sfixed64_field.rb +28 -0
  38. data/lib/protobuf/field/signed_integer_field.rb +29 -0
  39. data/lib/protobuf/field/sint32_field.rb +21 -0
  40. data/lib/protobuf/field/sint64_field.rb +21 -0
  41. data/lib/protobuf/field/string_field.rb +34 -0
  42. data/lib/protobuf/field/uint32_field.rb +21 -0
  43. data/lib/protobuf/field/uint64_field.rb +21 -0
  44. data/lib/protobuf/field/varint_field.rb +73 -0
  45. data/lib/protobuf/generators/base.rb +70 -0
  46. data/lib/protobuf/generators/enum_generator.rb +41 -0
  47. data/lib/protobuf/generators/extension_generator.rb +27 -0
  48. data/lib/protobuf/generators/field_generator.rb +131 -0
  49. data/lib/protobuf/generators/file_generator.rb +132 -0
  50. data/lib/protobuf/generators/group_generator.rb +105 -0
  51. data/lib/protobuf/generators/message_generator.rb +98 -0
  52. data/lib/protobuf/generators/printable.rb +160 -0
  53. data/lib/protobuf/logging.rb +39 -0
  54. data/lib/protobuf/message.rb +193 -0
  55. data/lib/protobuf/message/fields.rb +133 -0
  56. data/lib/protobuf/message/serialization.rb +89 -0
  57. data/lib/protobuf/optionable.rb +23 -0
  58. data/lib/protobuf/wire_type.rb +10 -0
  59. data/proto/dynamic_discovery.proto +44 -0
  60. data/proto/google/protobuf/compiler/plugin.proto +147 -0
  61. data/proto/google/protobuf/descriptor.proto +620 -0
  62. data/proto/rpc.proto +62 -0
  63. data/protobuf-core.gemspec +31 -0
  64. data/spec/bin/protoc-gen-ruby_spec.rb +23 -0
  65. data/spec/data/data.bin +3 -0
  66. data/spec/data/types.bin +0 -0
  67. data/spec/encoding/all_types_spec.rb +105 -0
  68. data/spec/encoding/extreme_values_spec.rb +0 -0
  69. data/spec/functional/class_inheritance_spec.rb +52 -0
  70. data/spec/functional/compile_and_require_spec.rb +29 -0
  71. data/spec/lib/protobuf/base_spec.rb +84 -0
  72. data/spec/lib/protobuf/code_generator_spec.rb +60 -0
  73. data/spec/lib/protobuf/enum_generator_spec.rb +73 -0
  74. data/spec/lib/protobuf/enum_spec.rb +265 -0
  75. data/spec/lib/protobuf/extension_generator_spec.rb +42 -0
  76. data/spec/lib/protobuf/field/bool_field_spec.rb +51 -0
  77. data/spec/lib/protobuf/field/field_array_spec.rb +69 -0
  78. data/spec/lib/protobuf/field/float_field_spec.rb +55 -0
  79. data/spec/lib/protobuf/field/int32_field_spec.rb +90 -0
  80. data/spec/lib/protobuf/field/string_field_spec.rb +45 -0
  81. data/spec/lib/protobuf/field_generator_spec.rb +102 -0
  82. data/spec/lib/protobuf/field_spec.rb +191 -0
  83. data/spec/lib/protobuf/file_generator_spec.rb +32 -0
  84. data/spec/lib/protobuf/message_generator_spec.rb +0 -0
  85. data/spec/lib/protobuf/message_spec.rb +526 -0
  86. data/spec/lib/protobuf/optionable_spec.rb +46 -0
  87. data/spec/lib/protobuf_spec.rb +45 -0
  88. data/spec/spec_helper.rb +9 -0
  89. data/spec/support/packed_field.rb +22 -0
  90. data/spec/support/test/all_types.data.bin +0 -0
  91. data/spec/support/test/all_types.data.txt +119 -0
  92. data/spec/support/test/bacon.proto +14 -0
  93. data/spec/support/test/defaults.pb.rb +27 -0
  94. data/spec/support/test/defaults.proto +9 -0
  95. data/spec/support/test/enum.pb.rb +61 -0
  96. data/spec/support/test/enum.proto +34 -0
  97. data/spec/support/test/extended.pb.rb +24 -0
  98. data/spec/support/test/extended.proto +10 -0
  99. data/spec/support/test/extreme_values.data.bin +0 -0
  100. data/spec/support/test/google_unittest.pb.rb +530 -0
  101. data/spec/support/test/google_unittest.proto +713 -0
  102. data/spec/support/test/google_unittest_import.pb.rb +39 -0
  103. data/spec/support/test/google_unittest_import.proto +64 -0
  104. data/spec/support/test/google_unittest_import_public.pb.rb +10 -0
  105. data/spec/support/test/google_unittest_import_public.proto +38 -0
  106. data/spec/support/test/multi_field_extensions.pb.rb +58 -0
  107. data/spec/support/test/multi_field_extensions.proto +33 -0
  108. data/spec/support/test/resource.pb.rb +106 -0
  109. data/spec/support/test/resource.proto +94 -0
  110. metadata +306 -0
@@ -0,0 +1,25 @@
1
+ require 'protobuf/field/float_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class DoubleField < FloatField
6
+
7
+ ##
8
+ # Public Instance Methods
9
+ #
10
+
11
+ def decode(bytes)
12
+ bytes.unpack('E').first
13
+ end
14
+
15
+ def encode(value)
16
+ [value].pack('E')
17
+ end
18
+
19
+ def wire_type
20
+ WireType::FIXED64
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,68 @@
1
+ require 'protobuf/field/varint_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class EnumField < VarintField
6
+
7
+ ##
8
+ # Class Methods
9
+ #
10
+
11
+ def self.default
12
+ fail NoMethodError, "#{self}.#{__method__} must be called on an instance"
13
+ end
14
+
15
+ ##
16
+ # Public Instance Methods
17
+ #
18
+
19
+ def acceptable?(val)
20
+ !type_class.fetch(val).nil?
21
+ end
22
+
23
+ def encode(value)
24
+ super(value.to_i)
25
+ end
26
+
27
+ def decode(value)
28
+ value if acceptable?(value)
29
+ end
30
+
31
+ def enum?
32
+ true
33
+ end
34
+
35
+ private
36
+
37
+ ##
38
+ # Private Instance Methods
39
+ #
40
+
41
+ def define_setter
42
+ field = self
43
+ message_class.class_eval do
44
+ define_method("#{field.name}=") do |value|
45
+ orig_value = value
46
+ if value.nil?
47
+ @values.delete(field.name)
48
+ else
49
+ value = field.type_class.fetch(value)
50
+ fail TypeError, "Invalid Enum value: #{orig_value.inspect} for #{field.name}" unless value
51
+
52
+ @values[field.name] = value
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ def typed_default_value
59
+ if default.is_a?(Symbol)
60
+ type_class.const_get(default)
61
+ else
62
+ type_class.fetch(default) || type_class.enums.first
63
+ end
64
+ end
65
+
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,87 @@
1
+ module Protobuf
2
+ module Field
3
+ class FieldArray < Array
4
+
5
+ ##
6
+ # Attributes
7
+ #
8
+
9
+ attr_reader :field
10
+
11
+ ##
12
+ # Constructor
13
+ #
14
+
15
+ def initialize(field)
16
+ @field = field
17
+ end
18
+
19
+ ##
20
+ # Public Instance Methods
21
+ #
22
+
23
+ def []=(nth, val)
24
+ super(nth, normalize(val)) unless val.nil?
25
+ end
26
+
27
+ def <<(val)
28
+ super(normalize(val)) unless val.nil?
29
+ end
30
+
31
+ def push(val)
32
+ super(normalize(val)) unless val.nil?
33
+ end
34
+
35
+ def replace(val)
36
+ raise_type_error(val) unless val.is_a?(Array)
37
+ val.map! { |v| normalize(v) }
38
+ super(val)
39
+ end
40
+
41
+ # Return a hash-representation of the given values for this field type.
42
+ # The value in this case would be an array.
43
+ def to_hash_value
44
+ map do |value|
45
+ value.respond_to?(:to_hash_value) ? value.to_hash_value : value
46
+ end
47
+ end
48
+
49
+ def to_s
50
+ "[#{field.name}]"
51
+ end
52
+
53
+ def unshift(val)
54
+ super(normalize(val)) unless val.nil?
55
+ end
56
+
57
+ private
58
+
59
+ ##
60
+ # Private Instance Methods
61
+ #
62
+
63
+ def normalize(value)
64
+ value = value.to_proto if value.respond_to?(:to_proto)
65
+ fail TypeError, "Unacceptable value #{value} for field #{field.name} of type #{field.type_class}" unless field.acceptable?(value)
66
+
67
+ if field.is_a?(::Protobuf::Field::EnumField)
68
+ field.type_class.fetch(value)
69
+ elsif field.is_a?(::Protobuf::Field::MessageField) && value.is_a?(field.type_class)
70
+ value
71
+ elsif field.is_a?(::Protobuf::Field::MessageField) && value.respond_to?(:to_hash)
72
+ field.type_class.new(value.to_hash)
73
+ else
74
+ value
75
+ end
76
+ end
77
+
78
+ def raise_type_error(val)
79
+ fail TypeError, <<-TYPE_ERROR
80
+ Expected repeated value of type '#{field.type_class}'
81
+ Got '#{val.class}' for repeated protobuf field #{field.name}
82
+ TYPE_ERROR
83
+ end
84
+
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,25 @@
1
+ require 'protobuf/field/uint32_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class Fixed32Field < Uint32Field
6
+
7
+ ##
8
+ # Public Instance Methods
9
+ #
10
+
11
+ def decode(bytes)
12
+ bytes.unpack('V').first
13
+ end
14
+
15
+ def encode(value)
16
+ [value].pack('V')
17
+ end
18
+
19
+ def wire_type
20
+ ::Protobuf::WireType::FIXED32
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,28 @@
1
+ require 'protobuf/field/uint64_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class Fixed64Field < Uint64Field
6
+
7
+ ##
8
+ # Public Instance Methods
9
+ #
10
+
11
+ def decode(bytes)
12
+ # we don't use 'Q' for pack/unpack. 'Q' is machine-dependent.
13
+ values = bytes.unpack('VV')
14
+ values[0] + (values[1] << 32)
15
+ end
16
+
17
+ def encode(value)
18
+ # we don't use 'Q' for pack/unpack. 'Q' is machine-dependent.
19
+ [value & 0xffff_ffff, value >> 32].pack('VV')
20
+ end
21
+
22
+ def wire_type
23
+ ::Protobuf::WireType::FIXED64
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,41 @@
1
+ require 'protobuf/field/base_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class FloatField < BaseField
6
+
7
+ ##
8
+ # Class Methods
9
+ #
10
+
11
+ def self.default
12
+ 0.0
13
+ end
14
+
15
+ ##
16
+ # Public Instance Methods
17
+ #
18
+
19
+ def acceptable?(val)
20
+ val.respond_to?(:to_f)
21
+ end
22
+
23
+ def coerce!(val)
24
+ Float(val)
25
+ end
26
+
27
+ def decode(bytes)
28
+ bytes.unpack('e').first
29
+ end
30
+
31
+ def encode(value)
32
+ [value].pack('e')
33
+ end
34
+
35
+ def wire_type
36
+ WireType::FIXED32
37
+ end
38
+
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,21 @@
1
+ require 'protobuf/field/integer_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class Int32Field < IntegerField
6
+
7
+ ##
8
+ # Class Methods
9
+ #
10
+
11
+ def self.max
12
+ INT32_MAX
13
+ end
14
+
15
+ def self.min
16
+ INT32_MIN
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ require 'protobuf/field/integer_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class Int64Field < IntegerField
6
+
7
+ ##
8
+ # Class Methods
9
+ #
10
+
11
+ def self.max
12
+ INT64_MAX
13
+ end
14
+
15
+ def self.min
16
+ INT64_MIN
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ require 'protobuf/field/varint_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class IntegerField < VarintField
6
+
7
+ ##
8
+ # Public Instance Methods
9
+ #
10
+
11
+ def decode(value)
12
+ value -= 0x1_0000_0000_0000_0000 if (value & 0x8000_0000_0000_0000).nonzero?
13
+ value
14
+ end
15
+
16
+ def encode(value)
17
+ # original Google's library uses 64bits integer for negative value
18
+ ::Protobuf::Field::VarintField.encode(value & 0xffff_ffff_ffff_ffff)
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,65 @@
1
+ require 'protobuf/field/base_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class MessageField < BaseField
6
+
7
+ ##
8
+ # Public Instance Methods
9
+ #
10
+
11
+ def acceptable?(val)
12
+ unless val.is_a?(type_class) || val.respond_to?(:to_hash)
13
+ fail TypeError, "Expected value of type '#{type_class}' for field #{name}, but got '#{val.class}'"
14
+ end
15
+
16
+ true
17
+ end
18
+
19
+ def decode(bytes)
20
+ type_class.decode(bytes)
21
+ end
22
+
23
+ def encode(value)
24
+ bytes = value.encode
25
+ result = ::Protobuf::Field::VarintField.encode(bytes.size)
26
+ result << bytes
27
+ end
28
+
29
+ def message?
30
+ true
31
+ end
32
+
33
+ def wire_type
34
+ ::Protobuf::WireType::LENGTH_DELIMITED
35
+ end
36
+
37
+ private
38
+
39
+ ##
40
+ # Private Instance Methods
41
+ #
42
+
43
+ def define_setter
44
+ field = self
45
+ message_class.class_eval do
46
+ define_method("#{field.name}=") do |val|
47
+ case
48
+ when val.nil? then
49
+ @values.delete(field.name)
50
+ when val.is_a?(field.type_class) then
51
+ @values[field.name] = val
52
+ when val.respond_to?(:to_proto) then
53
+ @values[field.name] = val.to_proto
54
+ when val.respond_to?(:to_hash) then
55
+ @values[field.name] = field.type_class.new(val.to_hash)
56
+ else
57
+ fail TypeError, "Expected value of type '#{field.type_class}' for field #{field.name}, but got '#{val.class}'"
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,27 @@
1
+ require 'protobuf/field/int32_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class Sfixed32Field < Int32Field
6
+
7
+ ##
8
+ # Public Instance Methods
9
+ #
10
+
11
+ def decode(bytes)
12
+ value = bytes.unpack('V').first
13
+ value -= 0x1_0000_0000 if (value & 0x8000_0000).nonzero?
14
+ value
15
+ end
16
+
17
+ def encode(value)
18
+ [value].pack('V')
19
+ end
20
+
21
+ def wire_type
22
+ ::Protobuf::WireType::FIXED32
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ require 'protobuf/field/int64_field'
2
+
3
+ module Protobuf
4
+ module Field
5
+ class Sfixed64Field < Int64Field
6
+
7
+ ##
8
+ # Public Instance Methods
9
+ #
10
+
11
+ def decode(bytes)
12
+ values = bytes.unpack('VV') # 'Q' is machine-dependent, don't use
13
+ value = values[0] + (values[1] << 32)
14
+ value -= 0x1_0000_0000_0000_0000 if (value & 0x8000_0000_0000_0000).nonzero?
15
+ value
16
+ end
17
+
18
+ def encode(value)
19
+ [value & 0xffff_ffff, value >> 32].pack('VV') # 'Q' is machine-dependent, don't use
20
+ end
21
+
22
+ def wire_type
23
+ ::Protobuf::WireType::FIXED64
24
+ end
25
+
26
+ end
27
+ end
28
+ end