protobuf 3.3.6 → 3.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rubocop.yml +48 -0
- data/.rubocop_todo.yml +79 -0
- data/.travis.yml +12 -4
- data/Rakefile +14 -11
- data/bin/protoc-gen-ruby +0 -1
- data/bin/rpc_server +1 -0
- data/install-protobuf.sh +8 -0
- data/lib/protobuf.rb +30 -24
- data/lib/protobuf/cli.rb +35 -35
- data/lib/protobuf/code_generator.rb +11 -8
- data/lib/protobuf/decoder.rb +4 -5
- data/lib/protobuf/deprecation.rb +20 -0
- data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +2 -0
- data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +2 -0
- data/lib/protobuf/encoder.rb +9 -4
- data/lib/protobuf/enum.rb +38 -54
- data/lib/protobuf/field.rb +2 -2
- data/lib/protobuf/field/base_field.rb +28 -32
- data/lib/protobuf/field/bool_field.rb +4 -4
- data/lib/protobuf/field/bytes_field.rb +5 -4
- data/lib/protobuf/field/double_field.rb +0 -1
- data/lib/protobuf/field/enum_field.rb +4 -7
- data/lib/protobuf/field/field_array.rb +3 -4
- data/lib/protobuf/field/fixed32_field.rb +1 -1
- data/lib/protobuf/field/fixed64_field.rb +0 -1
- data/lib/protobuf/field/float_field.rb +0 -1
- data/lib/protobuf/field/int32_field.rb +0 -1
- data/lib/protobuf/field/int64_field.rb +0 -1
- data/lib/protobuf/field/integer_field.rb +0 -1
- data/lib/protobuf/field/message_field.rb +2 -3
- data/lib/protobuf/field/sfixed32_field.rb +0 -1
- data/lib/protobuf/field/sfixed64_field.rb +0 -1
- data/lib/protobuf/field/signed_integer_field.rb +0 -1
- data/lib/protobuf/field/sint32_field.rb +0 -1
- data/lib/protobuf/field/sint64_field.rb +0 -1
- data/lib/protobuf/field/string_field.rb +0 -1
- data/lib/protobuf/field/uint32_field.rb +0 -1
- data/lib/protobuf/field/uint64_field.rb +0 -1
- data/lib/protobuf/field/varint_field.rb +0 -1
- data/lib/protobuf/generators/base.rb +1 -2
- data/lib/protobuf/generators/enum_generator.rb +1 -2
- data/lib/protobuf/generators/extension_generator.rb +1 -2
- data/lib/protobuf/generators/field_generator.rb +4 -5
- data/lib/protobuf/generators/file_generator.rb +22 -27
- data/lib/protobuf/generators/group_generator.rb +15 -16
- data/lib/protobuf/generators/message_generator.rb +13 -14
- data/lib/protobuf/generators/printable.rb +9 -10
- data/lib/protobuf/generators/service_generator.rb +1 -2
- data/lib/protobuf/lifecycle.rb +20 -33
- data/lib/protobuf/logging.rb +4 -6
- data/lib/protobuf/message.rb +22 -16
- data/lib/protobuf/message/fields.rb +14 -17
- data/lib/protobuf/message/serialization.rb +6 -5
- data/lib/protobuf/rpc/buffer.rb +10 -12
- data/lib/protobuf/rpc/client.rb +12 -12
- data/lib/protobuf/rpc/connectors/base.rb +4 -3
- data/lib/protobuf/rpc/connectors/common.rb +15 -17
- data/lib/protobuf/rpc/connectors/socket.rb +2 -2
- data/lib/protobuf/rpc/connectors/zmq.rb +118 -108
- data/lib/protobuf/rpc/dynamic_discovery.pb.rb +2 -0
- data/lib/protobuf/rpc/env.rb +12 -12
- data/lib/protobuf/rpc/error.rb +1 -1
- data/lib/protobuf/rpc/error/client_error.rb +4 -4
- data/lib/protobuf/rpc/error/server_error.rb +6 -6
- data/lib/protobuf/rpc/middleware/exception_handler.rb +1 -1
- data/lib/protobuf/rpc/middleware/logger.rb +3 -3
- data/lib/protobuf/rpc/middleware/request_decoder.rb +5 -5
- data/lib/protobuf/rpc/middleware/response_encoder.rb +3 -3
- data/lib/protobuf/rpc/rpc.pb.rb +2 -0
- data/lib/protobuf/rpc/servers/socket/server.rb +75 -65
- data/lib/protobuf/rpc/servers/socket/worker.rb +2 -2
- data/lib/protobuf/rpc/servers/socket_runner.rb +12 -6
- data/lib/protobuf/rpc/servers/zmq/broker.rb +10 -6
- data/lib/protobuf/rpc/servers/zmq/server.rb +20 -26
- data/lib/protobuf/rpc/servers/zmq/util.rb +7 -7
- data/lib/protobuf/rpc/servers/zmq/worker.rb +5 -7
- data/lib/protobuf/rpc/servers/zmq_runner.rb +14 -3
- data/lib/protobuf/rpc/service.rb +15 -15
- data/lib/protobuf/rpc/service_directory.rb +7 -11
- data/lib/protobuf/rpc/service_dispatcher.rb +3 -3
- data/lib/protobuf/rpc/service_filters.rb +27 -28
- data/lib/protobuf/rpc/stat.rb +4 -7
- data/lib/protobuf/socket.rb +0 -1
- data/lib/protobuf/tasks/compile.rake +2 -2
- data/lib/protobuf/version.rb +1 -1
- data/protobuf.gemspec +20 -4
- data/spec/benchmark/tasks.rb +49 -23
- data/spec/bin/protoc-gen-ruby_spec.rb +11 -6
- data/spec/encoding/all_types_spec.rb +91 -77
- data/spec/encoding/extreme_values_spec.rb +0 -0
- data/spec/functional/socket_server_spec.rb +9 -10
- data/spec/functional/zmq_server_spec.rb +21 -19
- data/spec/lib/protobuf/cli_spec.rb +20 -20
- data/spec/lib/protobuf/code_generator_spec.rb +6 -6
- data/spec/lib/protobuf/enum_spec.rb +57 -31
- data/spec/lib/protobuf/field/float_field_spec.rb +2 -2
- data/spec/lib/protobuf/field/int32_field_spec.rb +1 -1
- data/spec/lib/protobuf/field/string_field_spec.rb +7 -8
- data/spec/lib/protobuf/field_spec.rb +3 -6
- data/spec/lib/protobuf/generators/base_spec.rb +6 -6
- data/spec/lib/protobuf/generators/enum_generator_spec.rb +22 -17
- data/spec/lib/protobuf/generators/extension_generator_spec.rb +8 -9
- data/spec/lib/protobuf/generators/field_generator_spec.rb +14 -11
- data/spec/lib/protobuf/generators/file_generator_spec.rb +7 -4
- data/spec/lib/protobuf/generators/service_generator_spec.rb +14 -11
- data/spec/lib/protobuf/lifecycle_spec.rb +9 -4
- data/spec/lib/protobuf/message_spec.rb +63 -35
- data/spec/lib/protobuf/optionable_spec.rb +3 -3
- data/spec/lib/protobuf/rpc/client_spec.rb +2 -2
- data/spec/lib/protobuf/rpc/connector_spec.rb +1 -1
- data/spec/lib/protobuf/rpc/connectors/base_spec.rb +6 -6
- data/spec/lib/protobuf/rpc/connectors/common_spec.rb +26 -18
- data/spec/lib/protobuf/rpc/connectors/socket_spec.rb +4 -4
- data/spec/lib/protobuf/rpc/connectors/zmq_spec.rb +11 -9
- data/spec/lib/protobuf/rpc/middleware/exception_handler_spec.rb +2 -2
- data/spec/lib/protobuf/rpc/middleware/logger_spec.rb +7 -7
- data/spec/lib/protobuf/rpc/middleware/request_decoder_spec.rb +14 -14
- data/spec/lib/protobuf/rpc/middleware/response_encoder_spec.rb +5 -5
- data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +1 -1
- data/spec/lib/protobuf/rpc/servers/zmq/server_spec.rb +9 -7
- data/spec/lib/protobuf/rpc/servers/zmq/util_spec.rb +9 -9
- data/spec/lib/protobuf/rpc/servers/zmq/worker_spec.rb +2 -2
- data/spec/lib/protobuf/rpc/service_directory_spec.rb +24 -23
- data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +4 -4
- data/spec/lib/protobuf/rpc/service_filters_spec.rb +84 -51
- data/spec/lib/protobuf/rpc/service_spec.rb +15 -14
- data/spec/lib/protobuf/rpc/stat_spec.rb +1 -1
- data/spec/lib/protobuf_spec.rb +9 -9
- data/spec/spec_helper.rb +7 -19
- data/spec/support/server.rb +29 -59
- data/spec/support/test/defaults.pb.rb +2 -0
- data/spec/support/test/enum.pb.rb +2 -0
- data/spec/support/test/extended.pb.rb +2 -0
- data/spec/support/test/google_unittest_import.pb.rb +2 -0
- data/spec/support/test/google_unittest_import_public.pb.rb +2 -0
- data/spec/support/test/multi_field_extensions.pb.rb +2 -0
- data/spec/support/test/resource.pb.rb +2 -0
- data/spec/support/test/resource_service.rb +17 -20
- metadata +153 -112
- data/lib/protobuf/deprecator.rb +0 -42
@@ -12,7 +12,6 @@ module Protobuf
|
|
12
12
|
false
|
13
13
|
end
|
14
14
|
|
15
|
-
|
16
15
|
##
|
17
16
|
# Public Instance Methods
|
18
17
|
# #
|
@@ -39,15 +38,16 @@ module Protobuf
|
|
39
38
|
super
|
40
39
|
|
41
40
|
field = self
|
41
|
+
method_name = "#{field.getter}?"
|
42
|
+
|
42
43
|
message_class.class_eval do
|
43
|
-
define_method(
|
44
|
-
field.warn_if_deprecated
|
44
|
+
define_method(method_name) do
|
45
45
|
@values.fetch(field.name, field.default_value)
|
46
46
|
end
|
47
|
+
::Protobuf.deprecator.deprecate_methods(method_name)
|
47
48
|
end
|
48
49
|
end
|
49
50
|
|
50
51
|
end
|
51
52
|
end
|
52
53
|
end
|
53
|
-
|
@@ -53,10 +53,11 @@ module Protobuf
|
|
53
53
|
|
54
54
|
def define_setter
|
55
55
|
field = self
|
56
|
+
method_name = field.setter
|
57
|
+
|
56
58
|
message_class.class_eval do
|
57
|
-
define_method(
|
59
|
+
define_method(method_name) do |val|
|
58
60
|
begin
|
59
|
-
field.warn_if_deprecated
|
60
61
|
val = "#{val}" if val.is_a?(Symbol)
|
61
62
|
|
62
63
|
if val.nil?
|
@@ -64,7 +65,7 @@ module Protobuf
|
|
64
65
|
elsif field.acceptable?(val)
|
65
66
|
@values[field.name] = val.dup
|
66
67
|
else
|
67
|
-
|
68
|
+
fail TypeError, "Unacceptable value #{val} for field #{field.name} of type #{field.type_class}"
|
68
69
|
end
|
69
70
|
rescue NoMethodError => ex
|
70
71
|
logger.error { ex.message }
|
@@ -72,10 +73,10 @@ module Protobuf
|
|
72
73
|
raise TypeError, "Got NoMethodError attempting to set #{val} for field #{field.name} of type #{field.type_class}: #{ex.message}"
|
73
74
|
end
|
74
75
|
end
|
76
|
+
::Protobuf.deprecator.deprecate_methods(method_name)
|
75
77
|
end
|
76
78
|
end
|
77
79
|
|
78
80
|
end
|
79
81
|
end
|
80
82
|
end
|
81
|
-
|
@@ -9,7 +9,7 @@ module Protobuf
|
|
9
9
|
#
|
10
10
|
|
11
11
|
def self.default
|
12
|
-
|
12
|
+
fail NoMethodError, "#{self}.#{__method__} must be called on an instance"
|
13
13
|
end
|
14
14
|
|
15
15
|
##
|
@@ -17,7 +17,7 @@ module Protobuf
|
|
17
17
|
#
|
18
18
|
|
19
19
|
def acceptable?(val)
|
20
|
-
!
|
20
|
+
!type_class.fetch(val).nil?
|
21
21
|
end
|
22
22
|
|
23
23
|
def encode(value)
|
@@ -25,9 +25,7 @@ module Protobuf
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def decode(value)
|
28
|
-
if acceptable?(value)
|
29
|
-
value
|
30
|
-
end
|
28
|
+
value if acceptable?(value)
|
31
29
|
end
|
32
30
|
|
33
31
|
def enum?
|
@@ -49,7 +47,7 @@ module Protobuf
|
|
49
47
|
@values.delete(field.name)
|
50
48
|
else
|
51
49
|
value = field.type_class.fetch(value)
|
52
|
-
|
50
|
+
fail TypeError, "Invalid Enum value: #{orig_value.inspect} for #{field.name}" unless value
|
53
51
|
|
54
52
|
@values[field.name] = value
|
55
53
|
end
|
@@ -68,4 +66,3 @@ module Protobuf
|
|
68
66
|
end
|
69
67
|
end
|
70
68
|
end
|
71
|
-
|
@@ -41,7 +41,7 @@ module Protobuf
|
|
41
41
|
# Return a hash-representation of the given values for this field type.
|
42
42
|
# The value in this case would be an array.
|
43
43
|
def to_hash_value
|
44
|
-
|
44
|
+
map do |value|
|
45
45
|
value.respond_to?(:to_hash_value) ? value.to_hash_value : value
|
46
46
|
end
|
47
47
|
end
|
@@ -62,7 +62,7 @@ module Protobuf
|
|
62
62
|
|
63
63
|
def normalize(value)
|
64
64
|
value = value.to_proto if value.respond_to?(:to_proto)
|
65
|
-
|
65
|
+
fail TypeError, "Unacceptable value #{value} for field #{field.name} of type #{field.type_class}" unless field.acceptable?(value)
|
66
66
|
|
67
67
|
if field.is_a?(::Protobuf::Field::EnumField)
|
68
68
|
field.type_class.fetch(value)
|
@@ -74,7 +74,7 @@ module Protobuf
|
|
74
74
|
end
|
75
75
|
|
76
76
|
def raise_type_error(val)
|
77
|
-
|
77
|
+
fail TypeError, <<-TYPE_ERROR
|
78
78
|
Expected repeated value of type '#{field.type_class}'
|
79
79
|
Got '#{val.class}' for repeated protobuf field #{field.name}
|
80
80
|
TYPE_ERROR
|
@@ -83,4 +83,3 @@ module Protobuf
|
|
83
83
|
end
|
84
84
|
end
|
85
85
|
end
|
86
|
-
|
@@ -10,7 +10,7 @@ module Protobuf
|
|
10
10
|
|
11
11
|
def acceptable?(val)
|
12
12
|
unless val.instance_of?(type_class) || val.respond_to?(:to_hash)
|
13
|
-
|
13
|
+
fail TypeError, "Expected value of type '#{type_class}' for field #{name}, but got '#{val.class}'"
|
14
14
|
end
|
15
15
|
|
16
16
|
true
|
@@ -54,7 +54,7 @@ module Protobuf
|
|
54
54
|
when val.respond_to?(:to_hash) then
|
55
55
|
@values[field.name] = field.type_class.new(val.to_hash)
|
56
56
|
else
|
57
|
-
|
57
|
+
fail TypeError, "Expected value of type '#{field.type_class}' for field #{field.name}, but got '#{val.class}'"
|
58
58
|
end
|
59
59
|
end
|
60
60
|
end
|
@@ -63,4 +63,3 @@ module Protobuf
|
|
63
63
|
end
|
64
64
|
end
|
65
65
|
end
|
66
|
-
|
@@ -16,7 +16,7 @@ module Protobuf
|
|
16
16
|
print_class(@message_type, :message) do
|
17
17
|
group = GroupGenerator.new(current_indent)
|
18
18
|
group.add_extension_fields(@field_descriptors)
|
19
|
-
group.order = [
|
19
|
+
group.order = [:extension_field]
|
20
20
|
print group.to_s
|
21
21
|
end
|
22
22
|
end
|
@@ -25,4 +25,3 @@ module Protobuf
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
28
|
-
|
@@ -54,7 +54,7 @@ module Protobuf
|
|
54
54
|
|
55
55
|
def compile
|
56
56
|
run_once(:compile) do
|
57
|
-
field_definition = [
|
57
|
+
field_definition = ["#{label} #{type_name}", name, number, applicable_options]
|
58
58
|
puts field_definition.flatten.compact.join(', ')
|
59
59
|
end
|
60
60
|
end
|
@@ -91,10 +91,10 @@ module Protobuf
|
|
91
91
|
@type_name ||= begin
|
92
92
|
case descriptor.type.name
|
93
93
|
when :TYPE_MESSAGE, :TYPE_ENUM, :TYPE_GROUP then
|
94
|
-
|
94
|
+
modulize(descriptor.type_name)
|
95
95
|
else
|
96
96
|
type_name = descriptor.type.name.to_s.downcase.sub(/type_/, '')
|
97
|
-
|
97
|
+
":#{type_name}"
|
98
98
|
end
|
99
99
|
end
|
100
100
|
end
|
@@ -119,7 +119,7 @@ module Protobuf
|
|
119
119
|
end
|
120
120
|
|
121
121
|
def string_default_value
|
122
|
-
%
|
122
|
+
%("#{verbatim_default_value.gsub(/'/, '\\\\\'')}")
|
123
123
|
end
|
124
124
|
|
125
125
|
def verbatim_default_value
|
@@ -129,4 +129,3 @@ module Protobuf
|
|
129
129
|
end
|
130
130
|
end
|
131
131
|
end
|
132
|
-
|
@@ -22,8 +22,7 @@ module Protobuf
|
|
22
22
|
|
23
23
|
def compile
|
24
24
|
run_once(:compile) do
|
25
|
-
map_extensions(descriptor, [
|
26
|
-
extract_dangling_extensions
|
25
|
+
map_extensions(descriptor, [descriptor.package])
|
27
26
|
|
28
27
|
print_file_comment
|
29
28
|
print_generic_requires
|
@@ -31,10 +30,10 @@ module Protobuf
|
|
31
30
|
|
32
31
|
print_package do
|
33
32
|
group = GroupGenerator.new(current_indent)
|
34
|
-
group.add_enums(descriptor.enum_type, :namespace => [
|
33
|
+
group.add_enums(descriptor.enum_type, :namespace => [descriptor.package])
|
35
34
|
group.add_message_declarations(descriptor.message_type)
|
36
|
-
group.add_messages(descriptor.message_type, :extension_fields => @extension_fields, :namespace => [
|
37
|
-
group.add_extended_messages(
|
35
|
+
group.add_messages(descriptor.message_type, :extension_fields => @extension_fields, :namespace => [descriptor.package])
|
36
|
+
group.add_extended_messages(unknown_extensions)
|
38
37
|
group.add_services(descriptor.service)
|
39
38
|
|
40
39
|
group.add_header(:enum, 'Enum Classes')
|
@@ -48,9 +47,9 @@ module Protobuf
|
|
48
47
|
end
|
49
48
|
end
|
50
49
|
|
51
|
-
def
|
52
|
-
@unknown_extensions
|
53
|
-
|
50
|
+
def unknown_extensions
|
51
|
+
@unknown_extensions ||= @extension_fields.reject do |k, _|
|
52
|
+
@known_messages.include?(k)
|
54
53
|
end
|
55
54
|
end
|
56
55
|
|
@@ -79,20 +78,18 @@ module Protobuf
|
|
79
78
|
@extension_fields[field_descriptor.extendee] << field_descriptor
|
80
79
|
end
|
81
80
|
|
82
|
-
|
83
|
-
descriptor.
|
84
|
-
map_extensions(message_descriptor, (namespaces + [ message_descriptor.name ]))
|
85
|
-
end
|
86
|
-
end
|
81
|
+
[:message_type, :nested_type].each do |type|
|
82
|
+
next unless descriptor.respond_to_has_and_present?(type)
|
87
83
|
|
88
|
-
|
89
|
-
|
90
|
-
map_extensions(nested_descriptor, (namespaces + [ nested_descriptor.name ]))
|
84
|
+
descriptor.public_send(type).each do |type_descriptor|
|
85
|
+
map_extensions(type_descriptor, (namespaces + [type_descriptor.name]))
|
91
86
|
end
|
92
87
|
end
|
93
88
|
end
|
94
89
|
|
95
90
|
def print_file_comment
|
91
|
+
puts "# encoding: utf-8"
|
92
|
+
puts
|
96
93
|
puts "##"
|
97
94
|
puts "# This file is auto-generated. DO NOT EDIT!"
|
98
95
|
puts "#"
|
@@ -105,23 +102,22 @@ module Protobuf
|
|
105
102
|
end
|
106
103
|
|
107
104
|
def print_import_requires
|
108
|
-
if descriptor.dependency.
|
109
|
-
header "Imports"
|
105
|
+
return if descriptor.dependency.empty?
|
110
106
|
|
111
|
-
|
112
|
-
print_require(convert_filename(dependency))
|
113
|
-
end
|
107
|
+
header "Imports"
|
114
108
|
|
115
|
-
|
109
|
+
descriptor.dependency.each do |dependency|
|
110
|
+
print_require(convert_filename(dependency))
|
116
111
|
end
|
112
|
+
|
113
|
+
puts
|
117
114
|
end
|
118
115
|
|
119
116
|
def print_package(&block)
|
120
|
-
final = lambda { block.call }
|
121
117
|
namespaces = descriptor.package.split('.')
|
122
|
-
namespaces.reverse.
|
123
|
-
|
124
|
-
|
118
|
+
namespaces.reverse.reduce(block) do |previous, namespace|
|
119
|
+
-> { print_module(namespace, &previous) }
|
120
|
+
end.call
|
125
121
|
end
|
126
122
|
|
127
123
|
private
|
@@ -137,4 +133,3 @@ module Protobuf
|
|
137
133
|
end
|
138
134
|
end
|
139
135
|
end
|
140
|
-
|
@@ -18,7 +18,7 @@ module Protobuf
|
|
18
18
|
@comments = {}
|
19
19
|
@handlers = {}
|
20
20
|
@indent_level = indent_level
|
21
|
-
@order = [
|
21
|
+
@order = [:enum, :message_declaration, :message, :extended_message, :service]
|
22
22
|
init_printer(indent_level)
|
23
23
|
end
|
24
24
|
|
@@ -80,25 +80,25 @@ module Protobuf
|
|
80
80
|
def compile
|
81
81
|
@order.each do |type|
|
82
82
|
items = @groups[type]
|
83
|
-
if items.
|
84
|
-
item_handler = @handlers[type]
|
83
|
+
next if items.empty?
|
85
84
|
|
86
|
-
|
87
|
-
header(item_header) if item_header
|
85
|
+
item_handler = @handlers[type]
|
88
86
|
|
89
|
-
|
90
|
-
|
87
|
+
item_header = @headers[type]
|
88
|
+
header(item_header) if item_header
|
91
89
|
|
92
|
-
|
93
|
-
|
94
|
-
puts item_handler.call(item)
|
95
|
-
else
|
96
|
-
print item.to_s
|
97
|
-
end
|
98
|
-
end
|
90
|
+
item_comment = @comments[type]
|
91
|
+
comment(item_comment) if item_comment
|
99
92
|
|
100
|
-
|
93
|
+
items.each do |item|
|
94
|
+
if item_handler
|
95
|
+
puts item_handler.call(item)
|
96
|
+
else
|
97
|
+
print item.to_s
|
98
|
+
end
|
101
99
|
end
|
100
|
+
|
101
|
+
puts if type == :message_declaration
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
@@ -110,4 +110,3 @@ module Protobuf
|
|
110
110
|
end
|
111
111
|
end
|
112
112
|
end
|
113
|
-
|