stark 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +23 -0
- data/.gemtest +0 -0
- data/History.txt +6 -0
- data/Manifest.txt +31 -0
- data/README.txt +63 -0
- data/Rakefile +21 -0
- data/bin/stark +17 -0
- data/lib/stark/ast.rb +47 -0
- data/lib/stark/client.rb +63 -0
- data/lib/stark/converters.rb +29 -0
- data/lib/stark/field.rb +21 -0
- data/lib/stark/log_transport.rb +20 -0
- data/lib/stark/parser.rb +4 -0
- data/lib/stark/processor.rb +50 -0
- data/lib/stark/raw_parser.rb +4019 -0
- data/lib/stark/ruby.rb +468 -0
- data/lib/stark/struct.rb +26 -0
- data/lib/stark/thrift.kpeg +330 -0
- data/lib/stark.rb +37 -0
- data/stark.gemspec +40 -0
- data/test/ThriftSpec.thrift +183 -0
- data/test/gen-rb/profile_constants.rb +8 -0
- data/test/gen-rb/profile_types.rb +36 -0
- data/test/gen-rb/user_storage.rb +451 -0
- data/test/leg.rb +77 -0
- data/test/legacy_profile/profile_constants.rb +8 -0
- data/test/legacy_profile/profile_types.rb +36 -0
- data/test/legacy_profile/user_storage.rb +451 -0
- data/test/profile.thrift +24 -0
- data/test/test_client.rb +184 -0
- data/test/test_parser.rb +278 -0
- data/test/test_server.rb +175 -0
- metadata +134 -0
data/.autotest
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'autotest/restart'
|
4
|
+
|
5
|
+
# Autotest.add_hook :initialize do |at|
|
6
|
+
# at.extra_files << "../some/external/dependency.rb"
|
7
|
+
#
|
8
|
+
# at.libs << ":../some/external"
|
9
|
+
#
|
10
|
+
# at.add_exception 'vendor'
|
11
|
+
#
|
12
|
+
# at.add_mapping(/dependency.rb/) do |f, _|
|
13
|
+
# at.files_matching(/test_.*rb$/)
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# %w(TestA TestB).each do |klass|
|
17
|
+
# at.extra_class_map[klass] = "test/test_misc.rb"
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
|
21
|
+
# Autotest.add_hook :run_command do |at|
|
22
|
+
# system "rake build"
|
23
|
+
# end
|
data/.gemtest
ADDED
File without changes
|
data/History.txt
ADDED
data/Manifest.txt
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
.autotest
|
2
|
+
History.txt
|
3
|
+
Manifest.txt
|
4
|
+
README.txt
|
5
|
+
Rakefile
|
6
|
+
bin/stark
|
7
|
+
lib/stark.rb
|
8
|
+
lib/stark/ast.rb
|
9
|
+
lib/stark/client.rb
|
10
|
+
lib/stark/converters.rb
|
11
|
+
lib/stark/field.rb
|
12
|
+
lib/stark/log_transport.rb
|
13
|
+
lib/stark/parser.rb
|
14
|
+
lib/stark/processor.rb
|
15
|
+
lib/stark/raw_parser.rb
|
16
|
+
lib/stark/ruby.rb
|
17
|
+
lib/stark/struct.rb
|
18
|
+
lib/stark/thrift.kpeg
|
19
|
+
stark.gemspec
|
20
|
+
test/ThriftSpec.thrift
|
21
|
+
test/gen-rb/profile_constants.rb
|
22
|
+
test/gen-rb/profile_types.rb
|
23
|
+
test/gen-rb/user_storage.rb
|
24
|
+
test/leg.rb
|
25
|
+
test/legacy_profile/profile_constants.rb
|
26
|
+
test/legacy_profile/profile_types.rb
|
27
|
+
test/legacy_profile/user_storage.rb
|
28
|
+
test/profile.thrift
|
29
|
+
test/test_client.rb
|
30
|
+
test/test_parser.rb
|
31
|
+
test/test_server.rb
|
data/README.txt
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
= stark
|
2
|
+
|
3
|
+
* http://github.com/evanphx/stark
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Optimized thrift bindings for ruby
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* Generates much more straightforward code for thrift clients and servers
|
12
|
+
than the default thrift bindings for ruby
|
13
|
+
|
14
|
+
== SYNOPSIS:
|
15
|
+
|
16
|
+
$ stark service.thrift > service.rb
|
17
|
+
require 'service'
|
18
|
+
|
19
|
+
Use Service::Client and Service::Processor like the default thrift
|
20
|
+
docs describe them.
|
21
|
+
|
22
|
+
== REQUIREMENTS:
|
23
|
+
|
24
|
+
* thrift gem
|
25
|
+
* .thift files
|
26
|
+
|
27
|
+
== INSTALL:
|
28
|
+
|
29
|
+
* gem install stark
|
30
|
+
|
31
|
+
== DEVELOPERS:
|
32
|
+
|
33
|
+
After checking out the source, run:
|
34
|
+
|
35
|
+
$ rake newb
|
36
|
+
|
37
|
+
This task will install any missing dependencies, run the tests/specs,
|
38
|
+
and generate the RDoc.
|
39
|
+
|
40
|
+
== LICENSE:
|
41
|
+
|
42
|
+
(The MIT License)
|
43
|
+
|
44
|
+
Copyright (c) 2013 Evan Phoenix
|
45
|
+
|
46
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
47
|
+
a copy of this software and associated documentation files (the
|
48
|
+
'Software'), to deal in the Software without restriction, including
|
49
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
50
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
51
|
+
permit persons to whom the Software is furnished to do so, subject to
|
52
|
+
the following conditions:
|
53
|
+
|
54
|
+
The above copyright notice and this permission notice shall be
|
55
|
+
included in all copies or substantial portions of the Software.
|
56
|
+
|
57
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
58
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
59
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
60
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
61
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
62
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
63
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
|
6
|
+
Hoe.plugin :gemspec
|
7
|
+
Hoe.plugin :git
|
8
|
+
|
9
|
+
Hoe.spec 'stark' do
|
10
|
+
developer('Evan Phoenix', 'evan@phx.io')
|
11
|
+
|
12
|
+
dependency "thrift", "~> 0.9.0"
|
13
|
+
end
|
14
|
+
|
15
|
+
task :parser do
|
16
|
+
sh "kpeg -o lib/stark/raw_parser.rb -s -f lib/stark/thrift.kpeg"
|
17
|
+
end
|
18
|
+
|
19
|
+
task :test => :parser
|
20
|
+
|
21
|
+
# vim: syntax=ruby
|
data/bin/stark
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'stark/parser'
|
5
|
+
require 'stark/ruby'
|
6
|
+
|
7
|
+
data = File.read ARGV.shift
|
8
|
+
|
9
|
+
tg = Stark::Parser.new data
|
10
|
+
|
11
|
+
unless tg.parse
|
12
|
+
tg.raise_error
|
13
|
+
end
|
14
|
+
|
15
|
+
ruby = Stark::Ruby.new
|
16
|
+
|
17
|
+
tg.result.each { |i| i.accept ruby }
|
data/lib/stark/ast.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'stark/parser'
|
2
|
+
|
3
|
+
class Stark::Parser
|
4
|
+
module AST
|
5
|
+
class Namespace
|
6
|
+
def accept(obj)
|
7
|
+
obj.process_namespace self
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Include
|
12
|
+
def accept(obj)
|
13
|
+
obj.process_include self
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Struct
|
18
|
+
def accept(obj)
|
19
|
+
obj.process_struct self
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class Field
|
24
|
+
def accept(obj)
|
25
|
+
obj.process_field self
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Function
|
30
|
+
def accept(obj)
|
31
|
+
obj.process_function self
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class Service
|
36
|
+
def accept(obj)
|
37
|
+
obj.process_service self
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Enum
|
42
|
+
def accept(obj)
|
43
|
+
obj.process_enum self
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/stark/client.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'stark'
|
2
|
+
|
3
|
+
module Stark
|
4
|
+
class Client
|
5
|
+
def initialize(iprot, oprot)
|
6
|
+
@iprot = iprot
|
7
|
+
@oprot = oprot
|
8
|
+
end
|
9
|
+
|
10
|
+
def handle_exception(mtype)
|
11
|
+
if mtype == Thrift::MessageTypes::EXCEPTION
|
12
|
+
x = Thrift::ApplicationException.new
|
13
|
+
x.read(@iprot)
|
14
|
+
@iprot.read_message_end
|
15
|
+
raise x
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def handle_unexpected(rtype)
|
20
|
+
return if rtype == ::Thrift::Types::STOP
|
21
|
+
@iprot.skip(rtype)
|
22
|
+
end
|
23
|
+
|
24
|
+
def handle_bad_list(rtype, size)
|
25
|
+
size.times { @iprot.skip(rtype) }
|
26
|
+
end
|
27
|
+
|
28
|
+
def handle_bad_map(key, value, size)
|
29
|
+
size.times do
|
30
|
+
@iprot.skip(key)
|
31
|
+
@iprot.skip(value)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def hash_cast(obj)
|
36
|
+
return obj if obj.kind_of? Hash
|
37
|
+
return obj.to_h if obj.respond_to? :to_h
|
38
|
+
|
39
|
+
raise TypeError, "Unable to convert #{obj.class} to Hash"
|
40
|
+
end
|
41
|
+
|
42
|
+
def read_generic(type, id, cls)
|
43
|
+
ip = @iprot
|
44
|
+
|
45
|
+
obj = cls.new
|
46
|
+
|
47
|
+
ip.read_struct_begin
|
48
|
+
|
49
|
+
while true
|
50
|
+
_, ftype, fid = ip.read_field_begin
|
51
|
+
break if ftype == ::Thrift::Types::STOP
|
52
|
+
|
53
|
+
obj.set_from_index ftype, fid, ip
|
54
|
+
|
55
|
+
ip.read_field_end
|
56
|
+
end
|
57
|
+
|
58
|
+
ip.read_struct_end
|
59
|
+
|
60
|
+
obj
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'thrift'
|
2
|
+
|
3
|
+
module Stark
|
4
|
+
module Converters
|
5
|
+
module I32
|
6
|
+
module_function
|
7
|
+
|
8
|
+
def type
|
9
|
+
Thrift::Types::I32
|
10
|
+
end
|
11
|
+
|
12
|
+
def read(ip)
|
13
|
+
ip.read_i32
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module STRING
|
18
|
+
module_function
|
19
|
+
|
20
|
+
def type
|
21
|
+
Thrift::Types::STRING
|
22
|
+
end
|
23
|
+
|
24
|
+
def read(ip)
|
25
|
+
ip.read_string
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/stark/field.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'stark/converters'
|
2
|
+
|
3
|
+
module Stark
|
4
|
+
class Field
|
5
|
+
def initialize(idx, name, converter)
|
6
|
+
@index = idx
|
7
|
+
@name = name
|
8
|
+
@converter = converter
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_reader :index, :name, :converter
|
12
|
+
|
13
|
+
def type
|
14
|
+
@converter.type
|
15
|
+
end
|
16
|
+
|
17
|
+
def read(ip)
|
18
|
+
@converter.read ip
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'thrift'
|
2
|
+
|
3
|
+
module Stark
|
4
|
+
class LogTransport < Thrift::BaseTransport
|
5
|
+
def initialize(inner, prefix="log")
|
6
|
+
@prefix = prefix
|
7
|
+
@inner = inner
|
8
|
+
end
|
9
|
+
|
10
|
+
def log(name)
|
11
|
+
puts "#{@prefix}: #{name}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def open?; log :open?; @inner.open? end
|
15
|
+
def read(sz); log :read; @inner.read(sz) end
|
16
|
+
def write(buf); log :write; @inner.write(buf) end
|
17
|
+
def close; log :close; @inner.close end
|
18
|
+
def to_io; @inner.to_io end
|
19
|
+
end
|
20
|
+
end
|
data/lib/stark/parser.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
module Stark
|
2
|
+
class Processor
|
3
|
+
def initialize(handler)
|
4
|
+
@handler = handler
|
5
|
+
end
|
6
|
+
|
7
|
+
def process(iprot, oprot)
|
8
|
+
name, type, seqid = iprot.read_message_begin
|
9
|
+
if respond_to?("process_#{name}")
|
10
|
+
send("process_#{name}", seqid, iprot, oprot)
|
11
|
+
true
|
12
|
+
else
|
13
|
+
iprot.skip(::Thrift::Types::STRUCT)
|
14
|
+
iprot.read_message_end
|
15
|
+
x = ::Thrift::ApplicationException.new(Thrift::ApplicationException::UNKNOWN_METHOD, 'Unknown function '+name)
|
16
|
+
oprot.write_message_begin(name, ::Thrift::MessageTypes::EXCEPTION, seqid)
|
17
|
+
x.write(oprot)
|
18
|
+
oprot.write_message_end
|
19
|
+
oprot.trans.flush
|
20
|
+
false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def hash_cast(obj)
|
25
|
+
return obj if obj.kind_of? Hash
|
26
|
+
return obj.to_h if obj.respond_to? :to_h
|
27
|
+
|
28
|
+
raise TypeError, "Unable to convert #{obj.class} to Hash"
|
29
|
+
end
|
30
|
+
|
31
|
+
def read_struct(ip, type, id, cls)
|
32
|
+
obj = cls.new
|
33
|
+
|
34
|
+
ip.read_struct_begin
|
35
|
+
|
36
|
+
while true
|
37
|
+
_, ftype, fid = ip.read_field_begin
|
38
|
+
break if ftype == ::Thrift::Types::STOP
|
39
|
+
|
40
|
+
obj.set_from_index ftype, fid, ip
|
41
|
+
|
42
|
+
ip.read_field_end
|
43
|
+
end
|
44
|
+
|
45
|
+
ip.read_struct_end
|
46
|
+
|
47
|
+
obj
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|