rb: Add more classes/modules to the Thrift namespace. Provide backwards compatable mappings.
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@668904 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/generate/t_rb_generator.cc b/compiler/cpp/src/generate/t_rb_generator.cc
index c3d1d41..81e7d66 100644
--- a/compiler/cpp/src/generate/t_rb_generator.cc
+++ b/compiler/cpp/src/generate/t_rb_generator.cc
@@ -452,7 +452,7 @@
out << endl;
indent_up();
- indent(out) << "include ThriftStruct" << endl;
+ indent(out) << "include Thrift::Struct" << endl;
if (is_exception) {
generate_rb_simple_exception_constructor(out, tstruct);
@@ -677,7 +677,7 @@
indent_up();
indent(f_service_) <<
- "include ThriftClient" << endl << endl;
+ "include Thrift::Client" << endl << endl;
// Generate client method implementations
vector<t_function*> functions = tservice->get_functions();
@@ -774,7 +774,7 @@
"return" << endl;
} else {
f_service_ <<
- indent() << "raise TApplicationException.new(TApplicationException::MISSING_RESULT, '" << (*f_iter)->get_name() << " failed: unknown result')" << endl;
+ indent() << "raise Thrift::ApplicationException.new(Thrift::ApplicationException::MISSING_RESULT, '" << (*f_iter)->get_name() << " failed: unknown result')" << endl;
}
// Close function
@@ -810,7 +810,7 @@
indent_up();
f_service_ <<
- indent() << "include TProcessor" << endl <<
+ indent() << "include Thrift::Processor" << endl <<
endl;
// Generate the process subfunctions
@@ -969,30 +969,30 @@
case t_base_type::TYPE_VOID:
throw "NO T_VOID CONSTRUCT";
case t_base_type::TYPE_STRING:
- return "TType::STRING";
+ return "Thrift::Types::STRING";
case t_base_type::TYPE_BOOL:
- return "TType::BOOL";
+ return "Thrift::Types::BOOL";
case t_base_type::TYPE_BYTE:
- return "TType::BYTE";
+ return "Thrift::Types::BYTE";
case t_base_type::TYPE_I16:
- return "TType::I16";
+ return "Thrift::Types::I16";
case t_base_type::TYPE_I32:
- return "TType::I32";
+ return "Thrift::Types::I32";
case t_base_type::TYPE_I64:
- return "TType::I64";
+ return "Thrift::Types::I64";
case t_base_type::TYPE_DOUBLE:
- return "TType::DOUBLE";
+ return "Thrift::Types::DOUBLE";
}
} else if (type->is_enum()) {
- return "TType::I32";
+ return "Thrift::Types::I32";
} else if (type->is_struct() || type->is_xception()) {
- return "TType::STRUCT";
+ return "Thrift::Types::STRUCT";
} else if (type->is_map()) {
- return "TType::MAP";
+ return "Thrift::Types::MAP";
} else if (type->is_set()) {
- return "TType::SET";
+ return "Thrift::Types::SET";
} else if (type->is_list()) {
- return "TType::LIST";
+ return "Thrift::Types::LIST";
}
throw "INVALID TYPE IN type_to_enum: " + type->get_name();
diff --git a/lib/rb/lib/thrift.rb b/lib/rb/lib/thrift.rb
index b9a65d2..210e60b 100644
--- a/lib/rb/lib/thrift.rb
+++ b/lib/rb/lib/thrift.rb
@@ -28,6 +28,8 @@
ThriftStruct = Thrift::Struct
require 'thrift/protocol/tprotocol'
+TProtocol = Thrift::Protocol
+TProtocolException = Thrift::ProtocolException
require 'thrift/transport/tsocket'
require 'thrift/transport/ttransport'
diff --git a/lib/rb/lib/thrift/client.rb b/lib/rb/lib/thrift/client.rb
index 168c432..7f1297c 100644
--- a/lib/rb/lib/thrift/client.rb
+++ b/lib/rb/lib/thrift/client.rb
@@ -7,7 +7,7 @@
end
def send_message(name, args_class, args = {})
- @oprot.writeMessageBegin(name, TMessageType::CALL, @seqid)
+ @oprot.writeMessageBegin(name, MessageTypes::CALL, @seqid)
data = args_class.new
args.each do |k, v|
data.send("#{k.to_s}=", v)
@@ -27,8 +27,8 @@
end
def handle_exception(mtype)
- if mtype == TMessageType::EXCEPTION
- x = TApplicationException.new()
+ if mtype == MessageTypes::EXCEPTION
+ x = ApplicationException.new()
x.read(@iprot)
@iprot.readMessageEnd()
raise x
diff --git a/lib/rb/lib/thrift/exceptions.rb b/lib/rb/lib/thrift/exceptions.rb
index 673e9e3..972d571 100644
--- a/lib/rb/lib/thrift/exceptions.rb
+++ b/lib/rb/lib/thrift/exceptions.rb
@@ -28,17 +28,17 @@
iprot.readStructBegin()
while true
fname, ftype, fid = iprot.readFieldBegin()
- if (ftype === TType::STOP)
+ if (ftype === Types::STOP)
break
end
if (fid == 1)
- if (ftype === TType::STRING)
+ if (ftype === Types::STRING)
@message = iprot.readString();
else
iprot.skip(ftype)
end
elsif (fid == 2)
- if (ftype === TType::I32)
+ if (ftype === Types::I32)
@type = iprot.readI32();
else
iprot.skip(ftype)
@@ -52,14 +52,14 @@
end
def write(oprot)
- oprot.writeStructBegin('TApplicationException')
+ oprot.writeStructBegin('Thrift::ApplicationException')
if (@message != nil)
- oprot.writeFieldBegin('message', TType::STRING, 1)
+ oprot.writeFieldBegin('message', Types::STRING, 1)
oprot.writeString(@message)
oprot.writeFieldEnd()
end
if (@type != nil)
- oprot.writeFieldBegin('type', TType::I32, 2)
+ oprot.writeFieldBegin('type', Types::I32, 2)
oprot.writeI32(@type)
oprot.writeFieldEnd()
end
diff --git a/lib/rb/lib/thrift/processor.rb b/lib/rb/lib/thrift/processor.rb
index a234c61..5ec881f 100644
--- a/lib/rb/lib/thrift/processor.rb
+++ b/lib/rb/lib/thrift/processor.rb
@@ -10,10 +10,10 @@
send("process_#{name}", seqid, iprot, oprot)
return true
else
- iprot.skip(TType::STRUCT)
+ iprot.skip(Types::STRUCT)
iprot.readMessageEnd()
- x = TApplicationException.new(TApplicationException::UNKNOWN_METHOD, 'Unknown function '+name)
- oprot.writeMessageBegin(name, TMessageType::EXCEPTION, seqid)
+ x = ApplicationException.new(ApplicationException::UNKNOWN_METHOD, 'Unknown function '+name)
+ oprot.writeMessageBegin(name, MessageTypes::EXCEPTION, seqid)
x.write(oprot)
oprot.writeMessageEnd()
oprot.trans.flush()
@@ -29,7 +29,7 @@
end
def write_result(result, oprot, name, seqid)
- oprot.writeMessageBegin(name, TMessageType::REPLY, seqid)
+ oprot.writeMessageBegin(name, MessageTypes::REPLY, seqid)
result.write(oprot)
oprot.writeMessageEnd()
oprot.trans.flush()
diff --git a/lib/rb/lib/thrift/protocol/tbinaryprotocol.rb b/lib/rb/lib/thrift/protocol/tbinaryprotocol.rb
index ce0b1bb..87211f2 100644
--- a/lib/rb/lib/thrift/protocol/tbinaryprotocol.rb
+++ b/lib/rb/lib/thrift/protocol/tbinaryprotocol.rb
@@ -10,7 +10,7 @@
#
require 'thrift/protocol/tprotocol'
-class TBinaryProtocol < TProtocol
+class TBinaryProtocol < Thrift::Protocol
VERSION_MASK = 0xffff0000
VERSION_1 = 0x80010000
@@ -31,7 +31,7 @@
end
def writeFieldStop()
- writeByte(TType::STOP)
+ writeByte(Thrift::Types::STOP)
end
def writeMapBegin(ktype, vtype, size)
@@ -88,7 +88,7 @@
def readMessageBegin()
version = readI32()
if (version & VERSION_MASK != VERSION_1)
- raise TProtocolException.new(TProtocolException::BAD_VERSION, 'Missing version identifier')
+ raise Thrift::ProtocolException.new(Thrift::ProtocolException::BAD_VERSION, 'Missing version identifier')
end
type = version & 0x000000ff
name = readString()
@@ -98,7 +98,7 @@
def readFieldBegin()
type = readByte()
- if (type === TType::STOP)
+ if (type === Thrift::Types::STOP)
return nil, type, 0
end
id = readI16()
diff --git a/lib/rb/lib/thrift/protocol/tprotocol.rb b/lib/rb/lib/thrift/protocol/tprotocol.rb
index a06b4cd..edc894c 100644
--- a/lib/rb/lib/thrift/protocol/tprotocol.rb
+++ b/lib/rb/lib/thrift/protocol/tprotocol.rb
@@ -11,213 +11,214 @@
require 'thrift/thrift'
-class TProtocolException < TException
+module Thrift
+ class ProtocolException < Exception
- UNKNOWN = 0
- INVALID_DATA = 1
- NEGATIVE_SIZE = 2
- SIZE_LIMIT = 3
- BAD_VERSION = 4
+ UNKNOWN = 0
+ INVALID_DATA = 1
+ NEGATIVE_SIZE = 2
+ SIZE_LIMIT = 3
+ BAD_VERSION = 4
- attr_reader :type
+ attr_reader :type
- def initialize(type=UNKNOWN, message=nil)
- super(message)
- @type = type
- end
-
-end
-
-class TProtocol
-
- attr_reader :trans
-
- def initialize(trans)
- @trans = trans
- end
-
- def writeMessageBegin(name, type, seqid); nil; end
-
- def writeMessageEnd; nil; end
-
- def writeStructBegin(name); nil; end
-
- def writeStructEnd(); nil; end
-
- def writeFieldBegin(name, type, id); nil; end
-
- def writeFieldEnd(); nil; end
-
- def writeFieldStop(); nil; end
-
- def writeMapBegin(ktype, vtype, size); nil; end
-
- def writeMapEnd(); nil; end
-
- def writeListBegin(etype, size); nil; end
-
- def writeListEnd(); nil; end
-
- def writeSetBegin(etype, size); nil; end
-
- def writeSetEnd(); nil; end
-
- def writeBool(bool); nil; end
-
- def writeByte(byte); nil; end
-
- def writeI16(i16); nil; end
-
- def writeI32(i32); nil; end
-
- def writeI64(i64); nil; end
-
- def writeDouble(dub); nil; end
-
- def writeString(str); nil; end
-
- def readMessageBegin(); nil; end
-
- def readMessageEnd(); nil; end
-
- def readStructBegin(); nil; end
-
- def readStructEnd(); nil; end
-
- def readFieldBegin(); nil; end
-
- def readFieldEnd(); nil; end
-
- def readMapBegin(); nil; end
-
- def readMapEnd(); nil; end
-
- def readListBegin(); nil; end
-
- def readListEnd(); nil; end
-
- def readSetBegin(); nil; end
-
- def readSetEnd(); nil; end
-
- def readBool(); nil; end
-
- def readByte(); nil; end
-
- def readI16(); nil; end
-
- def readI32(); nil; end
-
- def readI64(); nil; end
-
- def readDouble(); nil; end
-
- def readString(); nil; end
-
- def write_field(name, type, fid, value)
- writeFieldBegin(name, type, fid)
- write_type(type, value)
- writeFieldEnd
- end
-
- def write_type(type, value)
- case type
- when TType::BOOL
- writeBool(value)
- when TType::BYTE
- writeByte(value)
- when TType::DOUBLE
- writeDouble(value)
- when TType::I16
- writeI16(value)
- when TType::I32
- writeI32(value)
- when TType::I64
- writeI64(value)
- when TType::STRING
- writeString(value)
- when TType::STRUCT
- value.write(self)
- else
- raise NotImplementedError
+ def initialize(type=UNKNOWN, message=nil)
+ super(message)
+ @type = type
end
+
end
- def read_type(type)
- case type
- when TType::BOOL
- readBool
- when TType::BYTE
- readByte
- when TType::DOUBLE
- readDouble
- when TType::I16
- readI16
- when TType::I32
- readI32
- when TType::I64
- readI64
- when TType::STRING
- readString
- else
- raise NotImplementedError
+ class Protocol
+
+ attr_reader :trans
+
+ def initialize(trans)
+ @trans = trans
end
- end
- def skip(type)
- if type === TType::STOP
- nil
- elsif type === TType::BOOL
- readBool()
- elsif type === TType::BYTE
- readByte()
- elsif type === TType::I16
- readI16()
- elsif type === TType::I32
- readI32()
- elsif type === TType::I64
- readI64()
- elsif type === TType::DOUBLE
- readDouble()
- elsif type === TType::STRING
- readString()
- elsif type === TType::STRUCT
- readStructBegin()
- while true
- name, type, id = readFieldBegin()
- if type === TType::STOP
- break
- else
- skip(type)
- readFieldEnd()
+ def writeMessageBegin(name, type, seqid); nil; end
+
+ def writeMessageEnd; nil; end
+
+ def writeStructBegin(name); nil; end
+
+ def writeStructEnd(); nil; end
+
+ def writeFieldBegin(name, type, id); nil; end
+
+ def writeFieldEnd(); nil; end
+
+ def writeFieldStop(); nil; end
+
+ def writeMapBegin(ktype, vtype, size); nil; end
+
+ def writeMapEnd(); nil; end
+
+ def writeListBegin(etype, size); nil; end
+
+ def writeListEnd(); nil; end
+
+ def writeSetBegin(etype, size); nil; end
+
+ def writeSetEnd(); nil; end
+
+ def writeBool(bool); nil; end
+
+ def writeByte(byte); nil; end
+
+ def writeI16(i16); nil; end
+
+ def writeI32(i32); nil; end
+
+ def writeI64(i64); nil; end
+
+ def writeDouble(dub); nil; end
+
+ def writeString(str); nil; end
+
+ def readMessageBegin(); nil; end
+
+ def readMessageEnd(); nil; end
+
+ def readStructBegin(); nil; end
+
+ def readStructEnd(); nil; end
+
+ def readFieldBegin(); nil; end
+
+ def readFieldEnd(); nil; end
+
+ def readMapBegin(); nil; end
+
+ def readMapEnd(); nil; end
+
+ def readListBegin(); nil; end
+
+ def readListEnd(); nil; end
+
+ def readSetBegin(); nil; end
+
+ def readSetEnd(); nil; end
+
+ def readBool(); nil; end
+
+ def readByte(); nil; end
+
+ def readI16(); nil; end
+
+ def readI32(); nil; end
+
+ def readI64(); nil; end
+
+ def readDouble(); nil; end
+
+ def readString(); nil; end
+
+ def write_field(name, type, fid, value)
+ writeFieldBegin(name, type, fid)
+ write_type(type, value)
+ writeFieldEnd
+ end
+
+ def write_type(type, value)
+ case type
+ when Types::BOOL
+ writeBool(value)
+ when Types::BYTE
+ writeByte(value)
+ when Types::DOUBLE
+ writeDouble(value)
+ when Types::I16
+ writeI16(value)
+ when Types::I32
+ writeI32(value)
+ when Types::I64
+ writeI64(value)
+ when Types::STRING
+ writeString(value)
+ when Types::STRUCT
+ value.write(self)
+ else
+ raise NotImplementedError
+ end
+ end
+
+ def read_type(type)
+ case type
+ when Types::BOOL
+ readBool
+ when Types::BYTE
+ readByte
+ when Types::DOUBLE
+ readDouble
+ when Types::I16
+ readI16
+ when Types::I32
+ readI32
+ when Types::I64
+ readI64
+ when Types::STRING
+ readString
+ else
+ raise NotImplementedError
+ end
+ end
+
+ def skip(type)
+ if type === Types::STOP
+ nil
+ elsif type === Types::BOOL
+ readBool()
+ elsif type === Types::BYTE
+ readByte()
+ elsif type === Types::I16
+ readI16()
+ elsif type === Types::I32
+ readI32()
+ elsif type === Types::I64
+ readI64()
+ elsif type === Types::DOUBLE
+ readDouble()
+ elsif type === Types::STRING
+ readString()
+ elsif type === Types::STRUCT
+ readStructBegin()
+ while true
+ name, type, id = readFieldBegin()
+ if type === Types::STOP
+ break
+ else
+ skip(type)
+ readFieldEnd()
+ end
+ readStructEnd()
end
- readStructEnd()
+ elsif type === Types::MAP
+ ktype, vtype, size = readMapBegin()
+ for i in 1..size
+ skip(ktype)
+ skip(vtype)
+ end
+ readMapEnd()
+ elsif type === Types::SET
+ etype, size = readSetBegin()
+ for i in 1..size
+ skip(etype)
+ end
+ readSetEnd()
+ elsif type === Types::LIST
+ etype, size = readListBegin()
+ for i in 1..size
+ skip(etype)
+ end
+ readListEnd()
end
- elsif type === TType::MAP
- ktype, vtype, size = readMapBegin()
- for i in 1..size
- skip(ktype)
- skip(vtype)
- end
- readMapEnd()
- elsif type === TType::SET
- etype, size = readSetBegin()
- for i in 1..size
- skip(etype)
- end
- readSetEnd()
- elsif type === TType::LIST
- etype, size = readListBegin()
- for i in 1..size
- skip(etype)
- end
- readListEnd()
end
- end
+ end
end
class TProtocolFactory
def getProtocol(trans); nil; end
end
-
diff --git a/lib/rb/lib/thrift/server/tserver.rb b/lib/rb/lib/thrift/server/tserver.rb
index f2239c8..788467e 100644
--- a/lib/rb/lib/thrift/server/tserver.rb
+++ b/lib/rb/lib/thrift/server/tserver.rb
@@ -42,7 +42,7 @@
while (true)
@processor.process(prot, prot)
end
- rescue TTransportException, TProtocolException => ttx
+ rescue TTransportException, Thrift::ProtocolException => ttx
#print ttx,"\n"
ensure
trans.close()
@@ -73,7 +73,7 @@
while (true)
@processor.process(p, p)
end
- rescue TTransportException, TProtocolException => e
+ rescue TTransportException, Thrift::ProtocolException => e
ensure
t.close()
end
@@ -119,7 +119,7 @@
while (true)
@processor.process(prot, prot)
end
- rescue TTransportException, TProtocolException => e
+ rescue TTransportException, Thrift::ProtocolException => e
ensure
trans.close()
end
diff --git a/lib/rb/lib/thrift/struct.rb b/lib/rb/lib/thrift/struct.rb
index 04b581f..35b3415 100644
--- a/lib/rb/lib/thrift/struct.rb
+++ b/lib/rb/lib/thrift/struct.rb
@@ -20,7 +20,7 @@
iprot.readStructBegin()
loop do
fname, ftype, fid = iprot.readFieldBegin()
- break if (ftype === TType::STOP)
+ break if (ftype === Types::STOP)
handle_message(iprot, fid, ftype)
iprot.readFieldEnd()
end
@@ -57,10 +57,10 @@
end
def read_field(iprot, field = {})
- if field[:type] == TType::STRUCT
+ if field[:type] == Types::STRUCT
value = field[:class].new
value.read(iprot)
- elsif field[:type] == TType::MAP
+ elsif field[:type] == Types::MAP
key_type, val_type, size = iprot.readMapBegin
value = {}
size.times do
@@ -69,13 +69,13 @@
value[k] = v
end
iprot.readMapEnd
- elsif field[:type] == TType::LIST
+ elsif field[:type] == Types::LIST
e_type, size = iprot.readListBegin
value = Array.new(size) do |n|
read_field(iprot, field_info(field[:element]))
end
iprot.readListEnd
- elsif field[:type] == TType::SET
+ elsif field[:type] == Types::SET
e_type, size = iprot.readSetBegin
value = {}
size.times do
@@ -98,20 +98,20 @@
end
def write_container(oprot, value, field = {})
- if field[:type] == TType::MAP
+ if field[:type] == Types::MAP
oprot.writeMapBegin(field[:key][:type], field[:value][:type], value.size)
value.each do |k, v|
write_data(oprot, k, field[:key])
write_data(oprot, v, field[:value])
end
oprot.writeMapEnd
- elsif field[:type] == TType::LIST
+ elsif field[:type] == Types::LIST
oprot.writeListBegin(field[:element][:type], value.size)
value.each do |elem|
write_data(oprot, elem, field[:element])
end
oprot.writeListEnd
- elsif field[:type] == TType::SET
+ elsif field[:type] == Types::SET
oprot.writeSetBegin(field[:element][:type], value.size)
value.each do |k, v|
write_data(oprot, k, field[:element])
@@ -123,7 +123,7 @@
end
def is_container?(type)
- [TType::LIST, TType::MAP, TType::SET].include? type
+ [Types::LIST, Types::MAP, Types::SET].include? type
end
def field_info(field)
diff --git a/lib/rb/lib/thrift/transport/ttransport.rb b/lib/rb/lib/thrift/transport/ttransport.rb
index a5f6a81..9702ac2 100644
--- a/lib/rb/lib/thrift/transport/ttransport.rb
+++ b/lib/rb/lib/thrift/transport/ttransport.rb
@@ -11,7 +11,7 @@
require 'thrift/thrift'
-class TTransportException < TException
+class TTransportException < Thrift::Exception
UNKNOWN = 0
NOT_OPEN = 1
diff --git a/test/rb/core/test_backwards_compatability.rb b/test/rb/core/test_backwards_compatability.rb
new file mode 100644
index 0000000..a826f20
--- /dev/null
+++ b/test/rb/core/test_backwards_compatability.rb
@@ -0,0 +1,32 @@
+require File.join(File.dirname(__FILE__), '../test_helper')
+
+require 'thrift/thrift'
+
+class TestTException < Test::Unit::TestCase
+ def test_has_accessible_message
+ msg = "hi there thrift"
+ assert_equal msg, TException.new(msg).message
+ end
+
+end
+
+class TestConstRemapping < Test::Unit::TestCase
+ def test_remappings
+ maps = {
+ TException => Thrift::Exception,
+ TApplicationException => Thrift::ApplicationException,
+ TType => Thrift::Types,
+ TMessageType => Thrift::MessageTypes,
+ TProcessor => Thrift::Processor,
+ ThriftClient => Thrift::Client,
+ ThriftStruct => Thrift::Struct,
+ TProtocol => Thrift::Protocol,
+ TProtocolException => Thrift::ProtocolException
+ }
+
+ maps.each do |k, v|
+ assert_equal k, v
+ end
+ end
+end
+
diff --git a/test/rb/core/test_exceptions.rb b/test/rb/core/test_exceptions.rb
index eb63963..88aacb0 100644
--- a/test/rb/core/test_exceptions.rb
+++ b/test/rb/core/test_exceptions.rb
@@ -2,10 +2,10 @@
require 'thrift/thrift'
-class TestTException < Test::Unit::TestCase
+class TestException < Test::Unit::TestCase
def test_has_accessible_message
msg = "hi there thrift"
- assert_equal msg, TException.new(msg).message
+ assert_equal msg, Thrift::Exception.new(msg).message
end
end
diff --git a/tutorial/rb/RubyClient.rb b/tutorial/rb/RubyClient.rb
index d40ff49..1b5d5db 100755
--- a/tutorial/rb/RubyClient.rb
+++ b/tutorial/rb/RubyClient.rb
@@ -2,8 +2,8 @@
$:.push('../gen-rb')
-require 'thrift/transport/tsocket.rb'
-require 'thrift/protocol/tbinaryprotocol.rb'
+require 'thrift/transport/tsocket'
+require 'thrift/protocol/tbinaryprotocol'
require 'Calculator'
@@ -51,6 +51,6 @@
transport.close()
-rescue TException => tx
- print 'TException: ', tx.message, "\n"
+rescue Thrift::Exception => tx
+ print 'Thrift::Exception: ', tx.message, "\n"
end