protobuf 2.8.13 → 3.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +84 -5
  3. data/CONTRIBUTING.md +3 -3
  4. data/Rakefile +46 -7
  5. data/lib/protobuf/cli.rb +2 -20
  6. data/lib/protobuf/decoder.rb +74 -0
  7. data/lib/protobuf/deprecator.rb +42 -0
  8. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +17 -16
  9. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +86 -85
  10. data/lib/protobuf/encoder.rb +62 -0
  11. data/lib/protobuf/enum.rb +298 -37
  12. data/lib/protobuf/field/base_field.rb +41 -27
  13. data/lib/protobuf/field/bool_field.rb +22 -4
  14. data/lib/protobuf/field/bytes_field.rb +36 -15
  15. data/lib/protobuf/field/double_field.rb +10 -3
  16. data/lib/protobuf/field/enum_field.rb +21 -18
  17. data/lib/protobuf/field/field_array.rb +26 -16
  18. data/lib/protobuf/field/fixed32_field.rb +10 -4
  19. data/lib/protobuf/field/fixed64_field.rb +10 -3
  20. data/lib/protobuf/field/float_field.rb +18 -5
  21. data/lib/protobuf/field/int32_field.rb +14 -4
  22. data/lib/protobuf/field/int64_field.rb +14 -4
  23. data/lib/protobuf/field/integer_field.rb +9 -4
  24. data/lib/protobuf/field/message_field.rb +16 -7
  25. data/lib/protobuf/field/sfixed32_field.rb +10 -3
  26. data/lib/protobuf/field/sfixed64_field.rb +12 -7
  27. data/lib/protobuf/field/signed_integer_field.rb +7 -0
  28. data/lib/protobuf/field/sint32_field.rb +14 -4
  29. data/lib/protobuf/field/sint64_field.rb +14 -4
  30. data/lib/protobuf/field/string_field.rb +11 -1
  31. data/lib/protobuf/field/uint32_field.rb +14 -4
  32. data/lib/protobuf/field/uint64_field.rb +14 -4
  33. data/lib/protobuf/field/varint_field.rb +11 -9
  34. data/lib/protobuf/field.rb +42 -25
  35. data/lib/protobuf/generators/enum_generator.rb +12 -1
  36. data/lib/protobuf/generators/field_generator.rb +1 -1
  37. data/lib/protobuf/lifecycle.rb +3 -4
  38. data/lib/protobuf/message/fields.rb +122 -0
  39. data/lib/protobuf/message/serialization.rb +84 -0
  40. data/lib/protobuf/message.rb +21 -221
  41. data/lib/protobuf/optionable.rb +23 -0
  42. data/lib/protobuf/rpc/client.rb +2 -4
  43. data/lib/protobuf/rpc/connector.rb +0 -2
  44. data/lib/protobuf/rpc/connectors/common.rb +2 -2
  45. data/lib/protobuf/rpc/dynamic_discovery.pb.rb +14 -16
  46. data/lib/protobuf/rpc/env.rb +58 -0
  47. data/lib/protobuf/rpc/error.rb +8 -5
  48. data/lib/protobuf/rpc/middleware/exception_handler.rb +36 -0
  49. data/lib/protobuf/rpc/middleware/logger.rb +91 -0
  50. data/lib/protobuf/rpc/middleware/request_decoder.rb +83 -0
  51. data/lib/protobuf/rpc/middleware/response_encoder.rb +88 -0
  52. data/lib/protobuf/rpc/middleware/runner.rb +18 -0
  53. data/lib/protobuf/rpc/middleware.rb +25 -0
  54. data/lib/protobuf/rpc/rpc.pb.rb +15 -16
  55. data/lib/protobuf/rpc/server.rb +14 -64
  56. data/lib/protobuf/rpc/servers/socket/server.rb +0 -2
  57. data/lib/protobuf/rpc/servers/socket/worker.rb +11 -15
  58. data/lib/protobuf/rpc/servers/zmq/util.rb +4 -1
  59. data/lib/protobuf/rpc/servers/zmq/worker.rb +5 -13
  60. data/lib/protobuf/rpc/servers/zmq_runner.rb +1 -1
  61. data/lib/protobuf/rpc/service.rb +38 -72
  62. data/lib/protobuf/rpc/service_dispatcher.rb +20 -108
  63. data/lib/protobuf/version.rb +1 -2
  64. data/lib/protobuf.rb +5 -13
  65. data/protobuf.gemspec +5 -5
  66. data/spec/benchmark/tasks.rb +2 -77
  67. data/spec/functional/zmq_server_spec.rb +13 -21
  68. data/spec/lib/protobuf/cli_spec.rb +5 -43
  69. data/spec/lib/protobuf/enum_spec.rb +194 -61
  70. data/spec/lib/protobuf/field_spec.rb +194 -0
  71. data/spec/lib/protobuf/generators/enum_generator_spec.rb +24 -1
  72. data/spec/lib/protobuf/generators/field_generator_spec.rb +6 -6
  73. data/spec/lib/protobuf/message_spec.rb +52 -70
  74. data/spec/lib/protobuf/optionable_spec.rb +46 -0
  75. data/spec/lib/protobuf/rpc/client_spec.rb +1 -93
  76. data/spec/lib/protobuf/rpc/connector_spec.rb +1 -7
  77. data/spec/lib/protobuf/rpc/connectors/zmq_spec.rb +8 -0
  78. data/spec/lib/protobuf/rpc/middleware/exception_handler_spec.rb +62 -0
  79. data/spec/lib/protobuf/rpc/middleware/logger_spec.rb +49 -0
  80. data/spec/lib/protobuf/rpc/middleware/request_decoder_spec.rb +115 -0
  81. data/spec/lib/protobuf/rpc/middleware/response_encoder_spec.rb +75 -0
  82. data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +0 -6
  83. data/spec/lib/protobuf/rpc/servers/zmq/util_spec.rb +10 -0
  84. data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +30 -105
  85. data/spec/lib/protobuf/rpc/service_filters_spec.rb +4 -4
  86. data/spec/lib/protobuf/rpc/service_spec.rb +20 -24
  87. data/spec/lib/protobuf_spec.rb +3 -3
  88. data/spec/spec_helper.rb +5 -4
  89. data/spec/support/packed_field.rb +15 -14
  90. data/spec/support/server.rb +4 -21
  91. data/spec/support/test/defaults.pb.rb +4 -4
  92. data/spec/support/test/enum.pb.rb +13 -1
  93. data/spec/support/test/enum.proto +15 -0
  94. data/spec/support/test/extended.pb.rb +1 -1
  95. data/spec/support/test/google_unittest.pb.rb +239 -241
  96. data/spec/support/test/google_unittest_import.pb.rb +2 -2
  97. data/spec/support/test/multi_field_extensions.pb.rb +2 -2
  98. data/spec/support/test/resource.pb.rb +19 -18
  99. data/spec/support/test/resource.proto +1 -0
  100. data/spec/support/test/resource_service.rb +5 -0
  101. metadata +78 -57
  102. data/bin/rprotoc +0 -8
  103. data/lib/protobuf/enum_value.rb +0 -85
  104. data/lib/protobuf/evented.rb +0 -37
  105. data/lib/protobuf/ext/eventmachine.rb +0 -14
  106. data/lib/protobuf/field/extension_fields.rb +0 -32
  107. data/lib/protobuf/message/decoder.rb +0 -72
  108. data/lib/protobuf/message/message.rb +0 -1
  109. data/lib/protobuf/rpc/connectors/em_client.rb +0 -84
  110. data/lib/protobuf/rpc/connectors/eventmachine.rb +0 -87
  111. data/lib/protobuf/rpc/servers/evented/server.rb +0 -36
  112. data/lib/protobuf/rpc/servers/evented_runner.rb +0 -31
  113. data/spec/functional/embedded_service_spec.rb +0 -7
  114. data/spec/functional/evented_server_spec.rb +0 -64
  115. data/spec/lib/protobuf/enum_value_spec.rb +0 -29
  116. data/spec/lib/protobuf/rpc/servers/evented_server_spec.rb +0 -19
@@ -1,32 +0,0 @@
1
- module Protobuf
2
- module Field
3
- class ExtensionFields < Array
4
- ##
5
- # Constructor
6
- #
7
- def initialize
8
- @ranges = []
9
- end
10
-
11
- ##
12
- # Public Instance Methods
13
- #
14
-
15
- # Append a range to the list of ranges.
16
- def add_range(range = (0..-1))
17
- @ranges << range
18
- end
19
-
20
- # Set value at tag location, if tag is in a valid range.
21
- def []=(key, value)
22
- raise RangeError, "#{key} is not in #{@ranges.join(', ')}" unless include_tag?(key)
23
- super
24
- end
25
-
26
- # Check if the given tag exists in any of the defined ranges.
27
- def include_tag?(tag)
28
- @ranges.any? { |range| range.include?(tag) }
29
- end
30
- end
31
- end
32
- end
@@ -1,72 +0,0 @@
1
- require 'protobuf/wire_type'
2
- require 'protobuf/exceptions'
3
-
4
- module Protobuf
5
-
6
- module Decoder
7
-
8
- module_function
9
-
10
- # Read bytes from +stream+ and pass to +message+ object.
11
- def decode(stream, message)
12
- until stream.eof?
13
- tag, wire_type = read_key(stream)
14
- bytes =
15
- case wire_type
16
- when ::Protobuf::WireType::VARINT then
17
- read_varint(stream)
18
- when ::Protobuf::WireType::FIXED64 then
19
- read_fixed64(stream)
20
- when ::Protobuf::WireType::LENGTH_DELIMITED then
21
- read_length_delimited(stream)
22
- when ::Protobuf::WireType::FIXED32 then
23
- read_fixed32(stream)
24
- when ::Protobuf::WireType::START_GROUP then
25
- raise NotImplementedError, 'Group is deprecated.'
26
- when ::Protobuf::WireType::END_GROUP then
27
- raise NotImplementedError, 'Group is deprecated.'
28
- else
29
- raise InvalidWireType, wire_type
30
- end
31
- message.set_field(tag, bytes)
32
- end
33
- message
34
- end
35
-
36
- # Read key pair (tag and wire-type) from +stream+.
37
- def read_key(stream)
38
- bits = read_varint(stream)
39
- wire_type = bits & 0x07
40
- tag = bits >> 3
41
- [tag, wire_type]
42
- end
43
-
44
- # Read varint integer value from +stream+.
45
- def read_varint(stream)
46
- value = index = 0
47
- begin
48
- byte = stream.readbyte
49
- value |= (byte & 0x7f) << (7 * index)
50
- index += 1
51
- end while (byte & 0x80).nonzero?
52
- value
53
- end
54
-
55
- # Read 32-bit string value from +stream+.
56
- def read_fixed32(stream)
57
- stream.read(4)
58
- end
59
-
60
- # Read 64-bit string value from +stream+.
61
- def read_fixed64(stream)
62
- stream.read(8)
63
- end
64
-
65
- # Read length-delimited string value from +stream+.
66
- def read_length_delimited(stream)
67
- value_length = read_varint(stream)
68
- stream.read(value_length)
69
- end
70
-
71
- end
72
- end
@@ -1 +0,0 @@
1
- require 'protobuf/message' # Need to get rid of this
@@ -1,84 +0,0 @@
1
- # Handles client connections to the server
2
- module Protobuf
3
- module Rpc
4
- module Connectors
5
-
6
- class EMClient < EM::Connection
7
- include Protobuf::Logger::LogMethods
8
- include Protobuf::Rpc::Connectors::Common
9
-
10
- attr_reader :options, :request, :response
11
- attr_reader :error, :error_reason, :error_message
12
-
13
- ##
14
- # Constructor
15
- #
16
- def initialize(options = {}, &failure_cb)
17
- @failure_cb = failure_cb
18
- @options = DEFAULT_OPTIONS.merge(options)
19
- @response_buffer = ::Protobuf::Rpc::Buffer.new(:read)
20
- verify_options!
21
-
22
- log_debug { sign_message("Client Initialized: #{options.inspect}") }
23
- rescue => e
24
- fail(:RPC_ERROR, "Failed to initialize connection: #{e.message}")
25
- end
26
-
27
- ##
28
- # Class Methods
29
- #
30
- def self.connect(options = {})
31
- options = DEFAULT_OPTIONS.merge(options)
32
- log_debug { sign_message("Connecting to server: #{options.inspect}") }
33
- EM.connect(options[:host], options[:port], self, options)
34
- end
35
-
36
- # turn post_init back into a no-op for event machine
37
- def post_init
38
- end
39
-
40
- ##
41
- # Instance Methods
42
- #
43
- # Completion callback registration
44
- def on_complete(&complete_cb)
45
- @complete_cb = complete_cb
46
- end
47
-
48
- # Failure callback registration
49
- def on_failure(&failure_cb)
50
- @failure_cb = failure_cb
51
- end
52
-
53
- # Success callback registration
54
- def on_success(&success_cb)
55
- @success_cb = success_cb
56
- end
57
-
58
- def receive_data(data)
59
- log_debug { sign_message("receive_data: #{data}") }
60
- @response_buffer << data
61
- @response_data = @response_buffer.data
62
- parse_response if !@response_data.nil? && @response_buffer.flushed?
63
- end
64
-
65
- def send_data
66
- request_buffer = ::Protobuf::Rpc::Buffer.new(:write)
67
- request_buffer.set_data(@request_data)
68
- log_debug { sign_message("sending data: #{request_buffer.inspect}") }
69
- super(request_buffer.write)
70
- rescue => e
71
- fail(:RPC_ERROR, "Connection error: #{e.message}")
72
- end
73
-
74
- # overwriting this method for java because it's broken in eventmachine. See https://github.com/eventmachine/eventmachine/issues/14
75
- if RUBY_PLATFORM =~ /java/
76
- def error?
77
- !!@error
78
- end
79
- end
80
-
81
- end
82
- end
83
- end
84
- end
@@ -1,87 +0,0 @@
1
- require 'protobuf/rpc/connectors/base'
2
- require 'protobuf/rpc/connectors/em_client'
3
-
4
- module Protobuf
5
- module Rpc
6
- module Connectors
7
- class EventMachine < Base
8
-
9
- def send_request
10
- ensure_em_running do
11
- f = Fiber.current
12
-
13
- ::EM.next_tick do
14
- log_debug { sign_message('Scheduling EventMachine client request to be created on next tick') }
15
- cnxn = EMClient.connect(options, &ensure_cb)
16
- cnxn.on_success(&success_cb) if success_cb
17
- cnxn.on_failure(&ensure_cb)
18
- cnxn.on_complete { resume_fiber(f) }
19
- cnxn.setup_connection
20
- cnxn.send_data
21
- log_debug { sign_message('Connection scheduled') }
22
- end
23
-
24
- set_timeout_and_validate_fiber
25
- end
26
- end
27
-
28
- # Returns a callable that ensures any errors will be returned to the client
29
- #
30
- # If a failure callback was set, just use that as a direct assignment
31
- # otherwise implement one here that simply throws an exception, since we
32
- # don't want to swallow the black holes.
33
- #
34
- def ensure_cb
35
- @ensure_cb ||= (@failure_cb || lambda { |error| raise "#{error.code.name}: #{error.message}" })
36
- end
37
-
38
- def log_signature
39
- @_log_signature ||= "client-#{self.class}"
40
- end
41
-
42
- private
43
-
44
- def ensure_em_running(&blk)
45
- if ::EM.reactor_running?
46
- @using_global_reactor = true
47
- yield
48
- else
49
- @using_global_reactor = false
50
- ::EM.fiber_run do
51
- blk.call
52
- ::EM.stop
53
- end
54
- end
55
- end
56
-
57
- def resume_fiber(fib)
58
- ::EM::cancel_timer(@timeout_timer)
59
- fib.resume(true)
60
- rescue => ex
61
- log_exception(ex)
62
- message = "Synchronous client failed: #{ex.message}"
63
- error_stop_reactor(message)
64
- end
65
-
66
- def set_timeout_and_validate_fiber
67
- @timeout_timer = ::EM::add_timer(@options[:timeout]) do
68
- message = "Client timeout of #{@options[:timeout]} seconds expired"
69
- error_stop_reactor(message)
70
- end
71
-
72
- Fiber.yield
73
- rescue FiberError => ex
74
- log_exception(ex)
75
- message = "Synchronous calls must be in 'EM.fiber_run' block"
76
- error_stop_reactor(message)
77
- end
78
-
79
- def error_stop_reactor(message)
80
- err = Protobuf::Rpc::ClientError.new(Protobuf::Socketrpc::ErrorReason::RPC_ERROR, message)
81
- ensure_cb.call(err)
82
- ::EM.stop unless @using_global_reactor
83
- end
84
- end
85
- end
86
- end
87
- end
@@ -1,36 +0,0 @@
1
- require 'protobuf/rpc/server'
2
-
3
- module Protobuf
4
- module Rpc
5
- module Evented
6
- class Server < ::EventMachine::Connection
7
- include ::Protobuf::Rpc::Server
8
- include ::Protobuf::Logger::LogMethods
9
-
10
- # Initialize a new read buffer for storing client request info
11
- def post_init
12
- initialize_request!
13
- @request_buffer = Protobuf::Rpc::Buffer.new(:read)
14
- end
15
-
16
- # Receive a chunk of data, potentially flushed to handle_client
17
- def receive_data(data)
18
- log_debug { sign_message("receive_data: #{data}") }
19
-
20
- @request_buffer << data
21
- @request_data = @request_buffer.data
22
-
23
- handle_client if @request_buffer.flushed?
24
- end
25
-
26
- def send_data
27
- response_buffer = Protobuf::Rpc::Buffer.new(:write)
28
- response_buffer.set_data(@response)
29
- @stats.response_size = response_buffer.size
30
- log_debug { sign_message("sending data: #{response_buffer.inspect}") }
31
- super(response_buffer.write)
32
- end
33
- end
34
- end
35
- end
36
- end
@@ -1,31 +0,0 @@
1
- module Protobuf
2
- module Rpc
3
- class EventedRunner
4
-
5
- def initialize(options)
6
- @options = options
7
- end
8
-
9
- def run
10
- # Startup and run the rpc server
11
- ::EventMachine.schedule do
12
- ::EventMachine.start_server(
13
- @options[:host],
14
- @options[:port],
15
- ::Protobuf::Rpc::Evented::Server
16
- )
17
- end
18
-
19
- # Join or start the reactor
20
- yield if block_given?
21
-
22
- ::EM.reactor_running? ? ::EM.reactor_thread.join : ::EM.run
23
- end
24
-
25
- def stop
26
- ::EventMachine.stop_event_loop if ::EventMachine.reactor_running?
27
- end
28
-
29
- end
30
- end
31
- end
@@ -1,7 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'An Embedded Service Call Hierarchy' do
4
-
5
-
6
-
7
- end
@@ -1,64 +0,0 @@
1
- require 'spec_helper'
2
- require 'spec/support/test/resource_service'
3
-
4
- describe 'Functional EventMachine Client' do
5
- before(:each) do
6
- load 'protobuf/evented.rb'
7
- end
8
-
9
- it 'runs fine when required fields are set' do
10
- expect {
11
- EventMachine.fiber_run do
12
- StubServer.new do |server|
13
- client = ::Test::ResourceService.client(:timeout => 5)
14
-
15
- client.find(:name => 'Test Name', :active => true) do |c|
16
- c.on_success do |succ|
17
- succ.name.should eq("Test Name")
18
- succ.status.should eq(::Test::StatusType::ENABLED)
19
- end
20
-
21
- c.on_failure do |err|
22
- raise err.inspect
23
- end
24
- end
25
- end
26
- EM.stop
27
- end
28
- }.to_not raise_error
29
- end
30
-
31
- it 'calls the on_failure callback when a message is malformed' do
32
- error = nil
33
- EventMachine.fiber_run do
34
- StubServer.new do |server|
35
- request = ::Test::ResourceFindRequest.new(:active => true)
36
- client = ::Test::ResourceService.client
37
-
38
- client.find(request) do |c|
39
- c.on_success { raise "shouldn't pass"}
40
- c.on_failure {|e| error = e}
41
- end
42
- end
43
- EM.stop
44
- end
45
- error.message.should =~ /name.*required/
46
- end
47
-
48
- it 'calls the on_failure callback when the request type is wrong' do
49
- error = nil
50
- EventMachine.fiber_run do
51
- StubServer.new do |server|
52
- request = ::Test::Resource.new(:name => 'Test Name')
53
- client = ::Test::ResourceService.client
54
-
55
- client.find(request) do |c|
56
- c.on_success { raise "shouldn't pass"}
57
- c.on_failure {|e| error = e}
58
- end
59
- end
60
- EM.stop
61
- end
62
- error.message.should =~ /expected request.*ResourceFindRequest.*Resource instead/i
63
- end
64
- end
@@ -1,29 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Protobuf::EnumValue do
4
-
5
- subject { Test::EnumTestType::ONE }
6
- its(:class) { should eq(Fixnum) }
7
- its(:parent_class) { should eq(Test::EnumTestType) }
8
- its(:name) { should eq(:ONE) }
9
- its(:value) { should eq(1) }
10
- its(:to_hash_value) { should eq(1) }
11
- its(:to_s) { should eq("1") }
12
- its(:inspect) { should eq('#<Protobuf::EnumValue Test::EnumTestType::ONE=1>') }
13
- specify { subject.to_s(:name).should eq("ONE") }
14
-
15
- it "can be used as the index to an array" do
16
- array = [0, 1, 2, 3]
17
- array[::Test::EnumTestType::ONE].should eq(1)
18
- end
19
-
20
- describe '#try' do
21
- specify { subject.try(:class).should eq subject.class }
22
- specify { subject.try(:name).should eq subject.name }
23
- specify { subject.try(:value).should eq subject.value }
24
- specify { subject.try(:to_i).should eq subject.to_i }
25
- specify { subject.try(:to_int).should eq subject.to_int }
26
- specify { subject.try { |yielded| yielded.should eq subject } }
27
- end
28
-
29
- end
@@ -1,19 +0,0 @@
1
- require 'spec_helper'
2
- require 'spec/support/test/resource_service'
3
- require 'protobuf/rpc/servers/evented_runner'
4
- require 'protobuf/evented'
5
-
6
- describe Protobuf::Rpc::Evented::Server do
7
-
8
- it "provides a Runner class" do
9
- runner_class = described_class.to_s.gsub(/Evented::Server/, "EventedRunner")
10
- expect { runner_class.constantize }.to_not raise_error
11
- end
12
-
13
- it "Runner provides a stop method" do
14
- runner_class = described_class.to_s.gsub(/Evented::Server/, "EventedRunner").constantize
15
- runner = runner_class.new({})
16
- runner.respond_to?(:stop).should be_true
17
- end
18
-
19
- end