protobuf-core 3.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +23 -0
- data/Rakefile +5 -0
- data/bin/protoc-gen-ruby +16 -0
- data/lib/protobuf.rb +27 -0
- data/lib/protobuf/code_generator.rb +44 -0
- data/lib/protobuf/core.rb +2 -0
- data/lib/protobuf/core/version.rb +5 -0
- data/lib/protobuf/decoder.rb +73 -0
- data/lib/protobuf/deprecation.rb +112 -0
- data/lib/protobuf/descriptors.rb +3 -0
- data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +54 -0
- data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +251 -0
- data/lib/protobuf/encoder.rb +67 -0
- data/lib/protobuf/enum.rb +303 -0
- data/lib/protobuf/exceptions.rb +9 -0
- data/lib/protobuf/field.rb +74 -0
- data/lib/protobuf/field/base_field.rb +267 -0
- data/lib/protobuf/field/bool_field.rb +59 -0
- data/lib/protobuf/field/bytes_field.rb +82 -0
- data/lib/protobuf/field/double_field.rb +25 -0
- data/lib/protobuf/field/enum_field.rb +68 -0
- data/lib/protobuf/field/field_array.rb +87 -0
- data/lib/protobuf/field/fixed32_field.rb +25 -0
- data/lib/protobuf/field/fixed64_field.rb +28 -0
- data/lib/protobuf/field/float_field.rb +41 -0
- data/lib/protobuf/field/int32_field.rb +21 -0
- data/lib/protobuf/field/int64_field.rb +21 -0
- data/lib/protobuf/field/integer_field.rb +23 -0
- data/lib/protobuf/field/message_field.rb +65 -0
- data/lib/protobuf/field/sfixed32_field.rb +27 -0
- data/lib/protobuf/field/sfixed64_field.rb +28 -0
- data/lib/protobuf/field/signed_integer_field.rb +29 -0
- data/lib/protobuf/field/sint32_field.rb +21 -0
- data/lib/protobuf/field/sint64_field.rb +21 -0
- data/lib/protobuf/field/string_field.rb +34 -0
- data/lib/protobuf/field/uint32_field.rb +21 -0
- data/lib/protobuf/field/uint64_field.rb +21 -0
- data/lib/protobuf/field/varint_field.rb +73 -0
- data/lib/protobuf/generators/base.rb +70 -0
- data/lib/protobuf/generators/enum_generator.rb +41 -0
- data/lib/protobuf/generators/extension_generator.rb +27 -0
- data/lib/protobuf/generators/field_generator.rb +131 -0
- data/lib/protobuf/generators/file_generator.rb +132 -0
- data/lib/protobuf/generators/group_generator.rb +105 -0
- data/lib/protobuf/generators/message_generator.rb +98 -0
- data/lib/protobuf/generators/printable.rb +160 -0
- data/lib/protobuf/logging.rb +39 -0
- data/lib/protobuf/message.rb +193 -0
- data/lib/protobuf/message/fields.rb +133 -0
- data/lib/protobuf/message/serialization.rb +89 -0
- data/lib/protobuf/optionable.rb +23 -0
- data/lib/protobuf/wire_type.rb +10 -0
- data/proto/dynamic_discovery.proto +44 -0
- data/proto/google/protobuf/compiler/plugin.proto +147 -0
- data/proto/google/protobuf/descriptor.proto +620 -0
- data/proto/rpc.proto +62 -0
- data/protobuf-core.gemspec +31 -0
- data/spec/bin/protoc-gen-ruby_spec.rb +23 -0
- data/spec/data/data.bin +3 -0
- data/spec/data/types.bin +0 -0
- data/spec/encoding/all_types_spec.rb +105 -0
- data/spec/encoding/extreme_values_spec.rb +0 -0
- data/spec/functional/class_inheritance_spec.rb +52 -0
- data/spec/functional/compile_and_require_spec.rb +29 -0
- data/spec/lib/protobuf/base_spec.rb +84 -0
- data/spec/lib/protobuf/code_generator_spec.rb +60 -0
- data/spec/lib/protobuf/enum_generator_spec.rb +73 -0
- data/spec/lib/protobuf/enum_spec.rb +265 -0
- data/spec/lib/protobuf/extension_generator_spec.rb +42 -0
- data/spec/lib/protobuf/field/bool_field_spec.rb +51 -0
- data/spec/lib/protobuf/field/field_array_spec.rb +69 -0
- data/spec/lib/protobuf/field/float_field_spec.rb +55 -0
- data/spec/lib/protobuf/field/int32_field_spec.rb +90 -0
- data/spec/lib/protobuf/field/string_field_spec.rb +45 -0
- data/spec/lib/protobuf/field_generator_spec.rb +102 -0
- data/spec/lib/protobuf/field_spec.rb +191 -0
- data/spec/lib/protobuf/file_generator_spec.rb +32 -0
- data/spec/lib/protobuf/message_generator_spec.rb +0 -0
- data/spec/lib/protobuf/message_spec.rb +526 -0
- data/spec/lib/protobuf/optionable_spec.rb +46 -0
- data/spec/lib/protobuf_spec.rb +45 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/support/packed_field.rb +22 -0
- data/spec/support/test/all_types.data.bin +0 -0
- data/spec/support/test/all_types.data.txt +119 -0
- data/spec/support/test/bacon.proto +14 -0
- data/spec/support/test/defaults.pb.rb +27 -0
- data/spec/support/test/defaults.proto +9 -0
- data/spec/support/test/enum.pb.rb +61 -0
- data/spec/support/test/enum.proto +34 -0
- data/spec/support/test/extended.pb.rb +24 -0
- data/spec/support/test/extended.proto +10 -0
- data/spec/support/test/extreme_values.data.bin +0 -0
- data/spec/support/test/google_unittest.pb.rb +530 -0
- data/spec/support/test/google_unittest.proto +713 -0
- data/spec/support/test/google_unittest_import.pb.rb +39 -0
- data/spec/support/test/google_unittest_import.proto +64 -0
- data/spec/support/test/google_unittest_import_public.pb.rb +10 -0
- data/spec/support/test/google_unittest_import_public.proto +38 -0
- data/spec/support/test/multi_field_extensions.pb.rb +58 -0
- data/spec/support/test/multi_field_extensions.proto +33 -0
- data/spec/support/test/resource.pb.rb +106 -0
- data/spec/support/test/resource.proto +94 -0
- 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,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
|