upfluence-thrift 1.0.10 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +5 -5
  2. data/lib/thrift.rb +6 -4
  3. data/lib/thrift/client.rb +31 -1
  4. data/lib/thrift/definition.rb +64 -0
  5. data/lib/thrift/middleware.rb +50 -0
  6. data/lib/thrift/processor.rb +35 -11
  7. data/lib/thrift/protocol/json_protocol.rb +6 -6
  8. data/lib/thrift/server/rack_application.rb +30 -19
  9. data/lib/thrift/struct.rb +10 -10
  10. data/lib/thrift/transport/base_transport.rb +9 -7
  11. data/lib/thrift/transport/server_socket.rb +6 -6
  12. data/spec/rack_application_spec.rb +1 -1
  13. metadata +15 -84
  14. data/benchmark/gen-rb/benchmark_constants.rb +0 -11
  15. data/benchmark/gen-rb/benchmark_service.rb +0 -80
  16. data/benchmark/gen-rb/benchmark_types.rb +0 -10
  17. data/spec/gen-rb/base/base_service.rb +0 -80
  18. data/spec/gen-rb/base/base_service_constants.rb +0 -11
  19. data/spec/gen-rb/base/base_service_types.rb +0 -26
  20. data/spec/gen-rb/extended/extended_service.rb +0 -78
  21. data/spec/gen-rb/extended/extended_service_constants.rb +0 -11
  22. data/spec/gen-rb/extended/extended_service_types.rb +0 -12
  23. data/spec/gen-rb/flat/namespaced_nonblocking_service.rb +0 -272
  24. data/spec/gen-rb/flat/referenced_constants.rb +0 -11
  25. data/spec/gen-rb/flat/referenced_types.rb +0 -17
  26. data/spec/gen-rb/flat/thrift_namespaced_spec_constants.rb +0 -11
  27. data/spec/gen-rb/flat/thrift_namespaced_spec_types.rb +0 -28
  28. data/spec/gen-rb/namespaced_spec_namespace/namespaced_nonblocking_service.rb +0 -272
  29. data/spec/gen-rb/namespaced_spec_namespace/thrift_namespaced_spec_constants.rb +0 -11
  30. data/spec/gen-rb/namespaced_spec_namespace/thrift_namespaced_spec_types.rb +0 -28
  31. data/spec/gen-rb/nonblocking_service.rb +0 -272
  32. data/spec/gen-rb/other_namespace/referenced_constants.rb +0 -11
  33. data/spec/gen-rb/other_namespace/referenced_types.rb +0 -17
  34. data/spec/gen-rb/thrift_spec_constants.rb +0 -11
  35. data/spec/gen-rb/thrift_spec_types.rb +0 -538
  36. data/test/debug_proto/gen-rb/debug_proto_test_constants.rb +0 -274
  37. data/test/debug_proto/gen-rb/debug_proto_test_types.rb +0 -761
  38. data/test/debug_proto/gen-rb/empty_service.rb +0 -24
  39. data/test/debug_proto/gen-rb/inherited.rb +0 -79
  40. data/test/debug_proto/gen-rb/reverse_order_service.rb +0 -82
  41. data/test/debug_proto/gen-rb/service_for_exception_with_a_map.rb +0 -81
  42. data/test/debug_proto/gen-rb/srv.rb +0 -330
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 0585c5dcf84ea7d019dfe7391d031ad89b8f51ed
4
- data.tar.gz: 3569d6e14d9d6e2eac720b4569398c9e7b9ae537
2
+ SHA256:
3
+ metadata.gz: eb0c6edc7181a95e78d4f3d1da97bf55cff534a6c4f3784c998bbefa1fc0df1c
4
+ data.tar.gz: c225382fb7a4bc1a0c4896f74a970c669eaf7cac96414b6c7e1be3d1dc7e66e5
5
5
  SHA512:
6
- metadata.gz: 0c580dc31d5b3c8f31befb3baef6e3bc4e285698de536c35714f3a5c21d7f0b13bf42976f85945f0bd792381f6626ef2830d8c24fac9b5eeee1d509e14918704
7
- data.tar.gz: f4a193a77d42cd34efc2366dda0a70c1da5fb4d24433fe70fc0f38c39ef4679e432a99fbc6319dd609bd1228cd48554061975d68f1c4ffaba391817a4d1ce8ee
6
+ metadata.gz: 9bfbd290bda69a9ca7102791fd2291e35902691598be71d1a1429dd16329ef9dad76a38d363e76098ee2b88af7ce8df93927b0f0458f41b93b8e4ec133b227b7
7
+ data.tar.gz: ffbb8d16a28268f9cb6cc82fa089e9bb06f96aefb3097077923478e4547fbc5cec43b2b640e0a790434e5ed41422236405c75bb835c5e2ac7735352e0dae182b
@@ -1,4 +1,4 @@
1
- #
1
+ #
2
2
  # Licensed to the Apache Software Foundation (ASF) under one
3
3
  # or more contributor license agreements. See the NOTICE file
4
4
  # distributed with this work for additional information
@@ -6,28 +6,30 @@
6
6
  # to you under the Apache License, Version 2.0 (the
7
7
  # "License"); you may not use this file except in compliance
8
8
  # with the License. You may obtain a copy of the License at
9
- #
9
+ #
10
10
  # http://www.apache.org/licenses/LICENSE-2.0
11
- #
11
+ #
12
12
  # Unless required by applicable law or agreed to in writing,
13
13
  # software distributed under the License is distributed on an
14
14
  # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
15
  # KIND, either express or implied. See the License for the
16
16
  # specific language governing permissions and limitations
17
17
  # under the License.
18
- #
18
+ #
19
19
  # Contains some contributions under the Thrift Software License.
20
20
  # Please see doc/old-thrift-license.txt in the Thrift distribution for
21
21
  # details.
22
22
 
23
23
  $:.unshift File.dirname(__FILE__)
24
24
 
25
+ require 'thrift/definition'
25
26
  require 'thrift/bytes'
26
27
  require 'thrift/core_ext'
27
28
  require 'thrift/exceptions'
28
29
  require 'thrift/types'
29
30
  require 'thrift/processor'
30
31
  require 'thrift/multiplexed_processor'
32
+ require 'thrift/middleware'
31
33
  require 'thrift/client'
32
34
  require 'thrift/struct'
33
35
  require 'thrift/union'
@@ -16,10 +16,11 @@
16
16
  # specific language governing permissions and limitations
17
17
  # under the License.
18
18
  #
19
+ #
19
20
 
20
21
  module Thrift
21
22
  module Client
22
- def initialize(iprot, oprot=nil)
23
+ def initialize(iprot, oprot = nil)
23
24
  @iprot = iprot
24
25
  @oprot = oprot || iprot
25
26
  @seqid = 0
@@ -39,9 +40,15 @@ module Thrift
39
40
 
40
41
  def send_message_args(args_class, args)
41
42
  data = args_class.new
43
+
42
44
  args.each do |k, v|
43
45
  data.send("#{k.to_s}=", v)
44
46
  end
47
+
48
+ send_message_instance(data)
49
+ end
50
+
51
+ def send_message_instance(data)
45
52
  begin
46
53
  data.write(@oprot)
47
54
  rescue StandardError => e
@@ -86,4 +93,27 @@ module Thrift
86
93
  raise x
87
94
  end
88
95
  end
96
+
97
+ class BaseClient
98
+ include Client
99
+
100
+ def call_unary(name, req)
101
+ @oprot.write_message_begin(name, Thrift::MessageTypes::ONEWAY, @seqid)
102
+ send_message_instance(req)
103
+ end
104
+
105
+ def call_binary(name, req, resp_klass)
106
+ @oprot.write_message_begin(name, Thrift::MessageTypes::CALL, @seqid)
107
+ send_message_instance(req)
108
+ receive_message(resp_klass)
109
+ end
110
+ end
111
+
112
+ class << self
113
+ def build_client(input)
114
+ return BaseClient.new(input) if input.is_a? BaseProtocol
115
+
116
+ input
117
+ end
118
+ end
89
119
  end
@@ -0,0 +1,64 @@
1
+ module Thrift
2
+ class StructDefinition
3
+ attr_reader :klass
4
+
5
+ def initialize(klass)
6
+ @klass = klass
7
+ end
8
+
9
+ def namespace
10
+ @klass::NAMESPACE
11
+ end
12
+
13
+ def name
14
+ @klass::NAME
15
+ end
16
+
17
+ def struct_type
18
+ "#{namespace}.#{name}"
19
+ end
20
+ end
21
+
22
+ class ServiceDefinition < StructDefinition
23
+ attr_reader :klass
24
+
25
+ def initialize(klass)
26
+ @klass = klass
27
+ end
28
+
29
+ def client_class
30
+ @klass::Client
31
+ end
32
+
33
+ def processor_class
34
+ @klass::Processor
35
+ end
36
+
37
+ def namespace
38
+ @klass::NAMESPACE
39
+ end
40
+
41
+ def service
42
+ @klass::SERVICE
43
+ end
44
+
45
+ def service_type
46
+ "#{namespace}.#{service}"
47
+ end
48
+ end
49
+
50
+ STRUCT_DEFINITIONS = {}
51
+ SERVICE_DEFINITIONS = {}
52
+
53
+ class << self
54
+ def register_struct_type(klass)
55
+ definition = StructDefinition.new(klass)
56
+ STRUCT_DEFINITIONS[definition.struct_type] = definition
57
+ end
58
+
59
+ def register_service_type(klass)
60
+ definition = ServiceDefinition.new(klass)
61
+ SERVICE_DEFINITIONS[definition.service_type] = definition
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,50 @@
1
+ module Thrift
2
+ module Middleware
3
+ class NopMiddleware
4
+ def handle_binary(_mth, args = {}, &block)
5
+ block.call(args)
6
+ end
7
+
8
+ def handle_unary(_mth, args = {}, &block)
9
+ block.call(args)
10
+ end
11
+ end
12
+
13
+ class MultiMiddleware
14
+ def initialize(middlewares)
15
+ @middlewares = middlewares
16
+ end
17
+
18
+ def handle_unary(mth, args = {}, &block)
19
+ @middlewares.reverse.reduce(block) do |acc, m|
20
+ Proc.new do |args|
21
+ m.handle_unary(mth, args) { |args| acc.call(args) }
22
+ end
23
+ end.call(args)
24
+ end
25
+
26
+ def handle_binary(mth, args = {}, &block)
27
+ @middlewares.reverse.reduce(block) do |acc, m|
28
+ Proc.new do |args|
29
+ m.handle_binary(mth, args) { |args| acc.call(args) }
30
+ end
31
+ end.call(args)
32
+ end
33
+ end
34
+
35
+ NOP_MIDDLEWARE = NopMiddleware.new
36
+
37
+ class << self
38
+ def wrap(middlewares)
39
+ case middlewares.length
40
+ when 0
41
+ NOP_MIDDLEWARE
42
+ when 1
43
+ middlewares.first
44
+ else
45
+ MultiMiddleware.new(middlewares)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -6,36 +6,45 @@
6
6
  # to you under the Apache License, Version 2.0 (the
7
7
  # "License"); you may not use this file except in compliance
8
8
  # with the License. You may obtain a copy of the License at
9
- #
9
+ #
10
10
  # http://www.apache.org/licenses/LICENSE-2.0
11
- #
11
+ #
12
12
  # Unless required by applicable law or agreed to in writing,
13
13
  # software distributed under the License is distributed on an
14
14
  # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
15
  # KIND, either express or implied. See the License for the
16
16
  # specific language governing permissions and limitations
17
17
  # under the License.
18
- #
18
+ #
19
19
 
20
20
  module Thrift
21
21
  module Processor
22
- def initialize(handler)
22
+ def initialize(handler, middlewares = [])
23
23
  @handler = handler
24
+ @middleware = Middleware.wrap(middlewares)
24
25
  end
25
26
 
26
27
  def process(iprot, oprot)
27
- name, type, seqid = iprot.read_message_begin
28
+ name, _type, seqid = iprot.read_message_begin
28
29
  if respond_to?("process_#{name}")
29
- send("process_#{name}", seqid, iprot, oprot)
30
+ begin
31
+ send("process_#{name}", seqid, iprot, oprot)
32
+ rescue => e
33
+ write_exception(e, oprot, name, seqid)
34
+ end
30
35
  true
31
36
  else
32
37
  iprot.skip(Types::STRUCT)
33
38
  iprot.read_message_end
34
- x = ApplicationException.new(ApplicationException::UNKNOWN_METHOD, 'Unknown function '+name)
35
- oprot.write_message_begin(name, MessageTypes::EXCEPTION, seqid)
36
- x.write(oprot)
37
- oprot.write_message_end
38
- oprot.trans.flush
39
+ write_exception(
40
+ ApplicationException.new(
41
+ ApplicationException::UNKNOWN_METHOD,
42
+ 'Unknown function ' + name,
43
+ ),
44
+ oprot,
45
+ name,
46
+ seqid
47
+ )
39
48
  false
40
49
  end
41
50
  end
@@ -47,6 +56,21 @@ module Thrift
47
56
  args
48
57
  end
49
58
 
59
+ def write_exception(exception, oprot, name, seqid)
60
+ oprot.write_message_begin(name, MessageTypes::EXCEPTION, seqid)
61
+
62
+ unless exception.is_a? ApplicationException
63
+ exception = ApplicationException.new(
64
+ ApplicationException::INTERNAL_ERROR,
65
+ "Internal error processing #{name}: #{exception.class}: #{exception}"
66
+ )
67
+ end
68
+
69
+ exception.write(oprot)
70
+ oprot.write_message_end
71
+ oprot.trans.flush
72
+ end
73
+
50
74
  def write_result(result, oprot, name, seqid)
51
75
  oprot.write_message_begin(name, MessageTypes::REPLY, seqid)
52
76
  result.write(oprot)
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
- #
2
+ #
3
3
  # Licensed to the Apache Software Foundation (ASF) under one
4
4
  # or more contributor license agreements. See the NOTICE file
5
5
  # distributed with this work for additional information
@@ -7,16 +7,16 @@
7
7
  # to you under the Apache License, Version 2.0 (the
8
8
  # "License"); you may not use this file except in compliance
9
9
  # with the License. You may obtain a copy of the License at
10
- #
10
+ #
11
11
  # http://www.apache.org/licenses/LICENSE-2.0
12
- #
12
+ #
13
13
  # Unless required by applicable law or agreed to in writing,
14
14
  # software distributed under the License is distributed on an
15
15
  # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
16
  # KIND, either express or implied. See the License for the
17
17
  # specific language governing permissions and limitations
18
18
  # under the License.
19
- #
19
+ #
20
20
 
21
21
 
22
22
  module Thrift
@@ -469,7 +469,7 @@ module Thrift
469
469
  end
470
470
 
471
471
  def write_string(str)
472
- write_json_string(str)
472
+ write_json_string(Bytes.convert_to_utf8_byte_buffer(str))
473
473
  end
474
474
 
475
475
  def write_binary(str)
@@ -753,7 +753,7 @@ module Thrift
753
753
  end
754
754
 
755
755
  def read_string
756
- read_json_string
756
+ Bytes.convert_to_string(read_json_string)
757
757
  end
758
758
 
759
759
  def read_binary
@@ -21,37 +21,48 @@ require 'rack'
21
21
 
22
22
  module Thrift
23
23
  class RackApplication
24
- THRIFT_HEADER = "application/x-thrift"
24
+ THRIFT_HEADER = 'application/x-thrift'.freeze
25
25
 
26
26
  def self.for(path, processor, protocol_factory)
27
27
  Rack::Builder.new do
28
28
  map path do
29
- run lambda { |env|
30
- request = Rack::Request.new(env)
31
- if RackApplication.valid_thrift_request?(request)
32
- RackApplication.successful_request(request, processor, protocol_factory)
33
- else
34
- RackApplication.failed_request
35
- end
36
- }
29
+ run Thrift::RackApplication.new(processor, protocol_factory)
37
30
  end
38
31
  end
39
32
  end
40
33
 
41
- def self.successful_request(rack_request, processor, protocol_factory)
42
- response = Rack::Response.new([], 200, {'Content-Type' => THRIFT_HEADER})
43
- transport = IOStreamTransport.new rack_request.body, response
44
- protocol = protocol_factory.get_protocol transport
45
- processor.process protocol, protocol
46
- response
34
+ def initialize(processor, protocol_factory)
35
+ @processor = processor
36
+ @protocol_factory = protocol_factory
37
+ @headers = { 'Content-Type' => THRIFT_HEADER }
47
38
  end
48
39
 
49
- def self.failed_request
50
- Rack::Response.new(['Not Found'], 404, {'Content-Type' => THRIFT_HEADER})
40
+ def call(env)
41
+ req = Rack::Request.new(env)
42
+
43
+ if valid_thrift_request?(req)
44
+ successful_request(req)
45
+ else
46
+ failed_request
47
+ end
48
+ end
49
+
50
+ def successful_request(req)
51
+ resp = Rack::Response.new([], 200, @headers)
52
+ transport = IOStreamTransport.new req.body, resp
53
+ protocol = @protocol_factory.get_protocol transport
54
+
55
+ @processor.process protocol, protocol
56
+
57
+ [resp.status, resp.headers, resp.body]
58
+ end
59
+
60
+ def failed_request
61
+ [404, @headers, 'Not found']
51
62
  end
52
63
 
53
- def self.valid_thrift_request?(rack_request)
54
- rack_request.post? && rack_request.env["CONTENT_TYPE"] == THRIFT_HEADER
64
+ def valid_thrift_request?(req)
65
+ req.post? && req.env['CONTENT_TYPE'] == THRIFT_HEADER
55
66
  end
56
67
  end
57
68
  end
@@ -1,4 +1,4 @@
1
- #
1
+ #
2
2
  # Licensed to the Apache Software Foundation (ASF) under one
3
3
  # or more contributor license agreements. See the NOTICE file
4
4
  # distributed with this work for additional information
@@ -6,16 +6,16 @@
6
6
  # to you under the Apache License, Version 2.0 (the
7
7
  # "License"); you may not use this file except in compliance
8
8
  # with the License. You may obtain a copy of the License at
9
- #
9
+ #
10
10
  # http://www.apache.org/licenses/LICENSE-2.0
11
- #
11
+ #
12
12
  # Unless required by applicable law or agreed to in writing,
13
13
  # software distributed under the License is distributed on an
14
14
  # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
15
  # KIND, either express or implied. See the License for the
16
16
  # specific language governing permissions and limitations
17
17
  # under the License.
18
- #
18
+ #
19
19
 
20
20
  require 'set'
21
21
 
@@ -24,15 +24,15 @@ module Thrift
24
24
  def initialize(d={}, &block)
25
25
  # get a copy of the default values to work on, removing defaults in favor of arguments
26
26
  fields_with_defaults = fields_with_default_values.dup
27
-
28
- # check if the defaults is empty, or if there are no parameters for this
27
+
28
+ # check if the defaults is empty, or if there are no parameters for this
29
29
  # instantiation, and if so, don't bother overriding defaults.
30
30
  unless fields_with_defaults.empty? || d.empty?
31
31
  d.each_key do |name|
32
32
  fields_with_defaults.delete(name.to_s)
33
33
  end
34
34
  end
35
-
35
+
36
36
  # assign all the user-specified arguments
37
37
  unless d.empty?
38
38
  d.each do |name, value|
@@ -43,14 +43,14 @@ module Thrift
43
43
  instance_variable_set("@#{name}", value)
44
44
  end
45
45
  end
46
-
46
+
47
47
  # assign all the default values
48
48
  unless fields_with_defaults.empty?
49
49
  fields_with_defaults.each do |name, default_value|
50
50
  instance_variable_set("@#{name}", (default_value.dup rescue default_value))
51
51
  end
52
52
  end
53
-
53
+
54
54
  yield self if block_given?
55
55
  end
56
56
 
@@ -67,7 +67,7 @@ module Thrift
67
67
  end
68
68
  fields_with_default_values
69
69
  end
70
-
70
+
71
71
  def inspect(skip_optional_nulls = true)
72
72
  fields = []
73
73
  each_field do |fid, field_info|