Thrift/Ruby: Allow some thrift exceptions to use standard Ruby syntax.
Submitted by William Morgan.
Approved by Kevin Clark.
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665445 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 56690e8..171e623 100644
--- a/compiler/cpp/src/generate/t_rb_generator.cc
+++ b/compiler/cpp/src/generate/t_rb_generator.cc
@@ -285,6 +285,10 @@
indent_up();
indent(out) << "include ThriftStruct" << endl;
+ if (is_exception) {
+ generate_rb_simple_exception_constructor(out, tstruct);
+ }
+
generate_accessors(out, tstruct);
generate_field_defns(out, tstruct);
@@ -292,6 +296,23 @@
indent(out) << "end" << endl << endl;
}
+void t_rb_generator::generate_rb_simple_exception_constructor(std::ofstream& out, t_struct* tstruct) {
+ const vector<t_field*>& members = tstruct->get_members();
+
+ if (members.size() == 1) {
+ vector<t_field*>::const_iterator m_iter = members.begin();
+
+ if ((*m_iter)->get_type()->is_string()) {
+ indent(out) << "def initialize(message)" << endl;
+ indent_up();
+ indent(out) << "super(message)" << endl;
+ indent(out) << "self." << (*m_iter)->get_name() << " = message" << endl;
+ indent_down();
+ indent(out) << "end" << endl << endl;
+ }
+ }
+}
+
void t_rb_generator::generate_accessors(std::ofstream& out, t_struct* tstruct) {
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
diff --git a/compiler/cpp/src/generate/t_rb_generator.h b/compiler/cpp/src/generate/t_rb_generator.h
index dc8d4a0..0cee5e4 100644
--- a/compiler/cpp/src/generate/t_rb_generator.h
+++ b/compiler/cpp/src/generate/t_rb_generator.h
@@ -56,6 +56,7 @@
void generate_rb_struct_reader(std::ofstream& out, t_struct* tstruct);
void generate_rb_struct_writer(std::ofstream& out, t_struct* tstruct);
void generate_rb_function_helpers(t_function* tfunction);
+ void generate_rb_simple_exception_constructor(std::ofstream& out, t_struct* tstruct);
void generate_accessors (std::ofstream& out, t_struct* tstruct);
void generate_field_defns (std::ofstream& out, t_struct* tstruct);
void generate_field_data (std::ofstream& out, t_type* field_type, const std::string& field_name, t_const_value* field_value);