Ruby support for Thrift
Summary: Just client support so far.
Reviewed By: tbr-doug
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664953 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
new file mode 100644
index 0000000..b9f12ec
--- /dev/null
+++ b/compiler/cpp/src/generate/t_rb_generator.cc
@@ -0,0 +1,1573 @@
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sstream>
+#include "t_rb_generator.h"
+using namespace std;
+
+/**
+ * Prepares for file generation by opening up the necessary file output
+ * streams.
+ *
+ * @param tprogram The program to generate
+ */
+void t_rb_generator::init_generator() {
+ // Make output directory
+ mkdir(T_RB_DIR, S_IREAD | S_IWRITE | S_IEXEC);
+
+ // Make output file
+ string f_types_name = string(T_RB_DIR)+"/"+program_name_+"_types.rb";
+ f_types_.open(f_types_name.c_str());
+
+ string f_consts_name = string(T_RB_DIR)+"/"+program_name_+"_constants.rb";
+ f_consts_.open(f_consts_name.c_str());
+
+ // Print header
+ f_types_ <<
+ rb_autogen_comment() << endl <<
+ rb_imports() << endl <<
+ render_includes() << endl;
+
+ f_consts_ <<
+ rb_autogen_comment() << endl <<
+ rb_imports() << endl <<
+ "require '" << program_name_ << "_types'" << endl <<
+ endl;
+}
+
+/**
+ * Renders all the imports necessary for including another Thrift program
+ */
+string t_rb_generator::render_includes() {
+ const vector<t_program*>& includes = program_->get_includes();
+ string result = "";
+ for (size_t i = 0; i < includes.size(); ++i) {
+ result += "require '" + includes[i]->get_name() + "_types'\n";
+ }
+ if (includes.size() > 0) {
+ result += "\n";
+ }
+ return result;
+}
+
+/**
+ * Autogen'd comment
+ */
+string t_rb_generator::rb_autogen_comment() {
+ return
+ std::string("#\n") +
+ "# Autogenerated by Thrift\n" +
+ "#\n" +
+ "# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" +
+ "#\n";
+}
+
+/**
+ * Prints standard thrift imports
+ */
+string t_rb_generator::rb_imports() {
+ return
+ string("require 'thrift/protocol/tprotocol'");
+}
+
+/**
+ * Closes the type files
+ */
+void t_rb_generator::close_generator() {
+ // Close types file
+ f_types_.close();
+ f_consts_.close();
+}
+
+/**
+ * Generates a typedef. This is not done in Ruby, types are all implicit.
+ *
+ * @param ttypedef The type definition
+ */
+void t_rb_generator::generate_typedef(t_typedef* ttypedef) {}
+
+/**
+ * Generates code for an enumerated type. Done using a class to scope
+ * the values.
+ *
+ * @param tenum The enumeration
+ */
+void t_rb_generator::generate_enum(t_enum* tenum) {
+ f_types_ <<
+ "module " << tenum->get_name() << endl;
+ indent_up();
+
+ vector<t_enum_value*> constants = tenum->get_constants();
+ vector<t_enum_value*>::iterator c_iter;
+ int value = -1;
+ for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
+ if ((*c_iter)->has_value()) {
+ value = (*c_iter)->get_value();
+ } else {
+ ++value;
+ }
+
+ // Ruby class constants have to be capitalized... omg i am so on the fence
+ // about languages strictly enforcing capitalization why can't we just all
+ // agree and play nice.
+ string name = capitalize((*c_iter)->get_name());
+
+ f_types_ <<
+ indent() << name << " = " << value << endl;
+ }
+
+ indent_down();
+ f_types_ <<
+ "end" << endl <<
+ endl;
+}
+
+/**
+ * Generate a constant value
+ */
+void t_rb_generator::generate_const(t_const* tconst) {
+ t_type* type = tconst->get_type();
+ string name = tconst->get_name();
+ t_const_value* value = tconst->get_value();
+
+ name[0] = toupper(name[0]);
+
+ indent(f_consts_) << name << " = ";
+ print_const_value(type, value);
+ f_consts_ << endl << endl;
+}
+
+/**
+ * Prints the value of a constant with the given type. Note that type checking
+ * is NOT performed in this function as it is always run beforehand using the
+ * validate_types method in main.cc
+ */
+void t_rb_generator::print_const_value(t_type* type, t_const_value* value) {
+ if (type->is_base_type()) {
+ t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ switch (tbase) {
+ case t_base_type::TYPE_STRING:
+ f_consts_ << "'" << value->get_string() << "'";
+ break;
+ case t_base_type::TYPE_BOOL:
+ f_consts_ << (value->get_integer() > 0 ? "true" : "false");
+ break;
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ f_consts_ << value->get_integer();
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ if (value->get_type() == t_const_value::CV_INTEGER) {
+ f_consts_ << value->get_integer();
+ } else {
+ f_consts_ << value->get_double();
+ }
+ break;
+ default:
+ throw "compiler error: no const of base type " + tbase;
+ }
+ } else if (type->is_enum()) {
+ indent(f_consts_) << value->get_integer();
+ } else if (type->is_struct() || type->is_xception()) {
+ f_consts_ << type->get_name() << "({" << endl;
+ indent_up();
+ const vector<t_field*>& fields = ((t_struct*)type)->get_members();
+ vector<t_field*>::const_iterator f_iter;
+ const map<t_const_value*, t_const_value*>& val = value->get_map();
+ map<t_const_value*, t_const_value*>::const_iterator v_iter;
+ for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
+ t_type* field_type = NULL;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ if ((*f_iter)->get_name() == v_iter->first->get_string()) {
+ field_type = (*f_iter)->get_type();
+ }
+ }
+ if (field_type == NULL) {
+ throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
+ }
+ f_consts_ << indent();
+ print_const_value(g_type_string, v_iter->first);
+ f_consts_ << " => ";
+ print_const_value(field_type, v_iter->second);
+ f_consts_ << "," << endl;
+ }
+ indent_down();
+ indent(f_consts_) << "})";
+ } else if (type->is_map()) {
+ t_type* ktype = ((t_map*)type)->get_key_type();
+ t_type* vtype = ((t_map*)type)->get_val_type();
+ f_consts_ << "{" << endl;
+ indent_up();
+ const map<t_const_value*, t_const_value*>& val = value->get_map();
+ map<t_const_value*, t_const_value*>::const_iterator v_iter;
+ for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
+ f_consts_ << indent();
+ print_const_value(ktype, v_iter->first);
+ f_consts_ << " =>x ";
+ print_const_value(vtype, v_iter->second);
+ f_consts_ << "," << endl;
+ }
+ indent_down();
+ indent(f_consts_) << "}";
+ } else if (type->is_list() || type->is_set()) {
+ t_type* etype;
+ if (type->is_list()) {
+ etype = ((t_list*)type)->get_elem_type();
+ } else {
+ etype = ((t_set*)type)->get_elem_type();
+ }
+ if (type->is_set()) {
+ f_consts_ << "{";
+ } else {
+ f_consts_ << "[" << endl;
+ }
+ indent_up();
+ const vector<t_const_value*>& val = value->get_list();
+ vector<t_const_value*>::const_iterator v_iter;
+ for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
+ f_consts_ << indent();
+ print_const_value(etype, *v_iter);
+ if (type->is_set()) {
+ f_consts_ << " => true";
+ }
+ f_consts_ << "," << endl;
+ }
+ indent_down();
+ if (type->is_set()) {
+ indent(f_consts_) << "}";
+ } else {
+ indent(f_consts_) << "]";
+ }
+ }
+}
+
+/**
+ * Generates a ruby struct
+ */
+void t_rb_generator::generate_struct(t_struct* tstruct) {
+ generate_rb_struct(tstruct, false);
+}
+
+/**
+ * Generates a struct definition for a thrift exception. Basically the same
+ * as a struct but extends the Exception class.
+ *
+ * @param txception The struct definition
+ */
+void t_rb_generator::generate_xception(t_struct* txception) {
+ generate_rb_struct(txception, true);
+}
+
+/**
+ * Generates a ruby struct
+ */
+void t_rb_generator::generate_rb_struct(t_struct* tstruct,
+ bool is_exception) {
+ generate_rb_struct_definition(f_types_, tstruct, is_exception);
+}
+
+/**
+ * Generates a struct definition for a thrift data type. This is nothing in PHP
+ * where the objects are all just associative arrays (unless of course we
+ * decide to start using objects for them...)
+ *
+ * @param tstruct The struct definition
+ */
+void t_rb_generator::generate_rb_struct_definition(ofstream& out,
+ t_struct* tstruct,
+ bool is_exception,
+ bool is_result) {
+ const vector<t_field*>& members = tstruct->get_members();
+ vector<t_field*>::const_iterator m_iter;
+
+ indent(out) <<
+ "class " << type_name(tstruct);
+ if (is_exception) {
+ out << " < StandardError";
+ }
+ out << endl;
+ indent_up();
+
+ out << endl;
+
+ if (members.size() > 0) {
+ indent(out) << "attr_writer ";
+ bool first = true;
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ if (first) {
+ first = false;
+ } else {
+ out << ", ";
+ }
+ out << ":" << (*m_iter)->get_name();
+ }
+ out << endl;
+ indent(out) << "attr_reader ";
+ first = true;
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ if (first) {
+ first = false;
+ } else {
+ out << ", ";
+ }
+ out << ":" << (*m_iter)->get_name();
+ }
+ out << endl;
+ out << endl;
+ }
+
+ out <<
+ indent() << "def initialize(d=nil)" << endl;
+ indent_up();
+
+ if (members.size() > 0) {
+ indent(out) <<
+ "if (d != nil)" << endl;
+ indent_up();
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ out <<
+ indent() << "if (d.has_key?('" << (*m_iter)->get_name() << "'))" << endl <<
+ indent() << " @" << (*m_iter)->get_name() << " = d['" << (*m_iter)->get_name() << "']" << endl <<
+ indent() << "end" << endl;
+ }
+ indent_down();
+ indent(out) << "end" << endl;
+ }
+
+ indent_down();
+ indent(out) << "end" << endl;
+
+ out << endl;
+
+ generate_rb_struct_reader(out, tstruct);
+ generate_rb_struct_writer(out, tstruct);
+
+ indent_down();
+ indent(out) << "end" << endl << endl;
+}
+
+/**
+ * Generates the read method for a struct
+ */
+void t_rb_generator::generate_rb_struct_reader(ofstream& out,
+ t_struct* tstruct) {
+ const vector<t_field*>& fields = tstruct->get_members();
+ vector<t_field*>::const_iterator f_iter;
+
+ indent(out) <<
+ "def read(iprot)" << endl;
+ indent_up();
+
+ indent(out) <<
+ "iprot.readStructBegin()" << endl;
+
+ // Loop over reading in fields
+ indent(out) <<
+ "while true" << endl;
+ indent_up();
+
+ // Read beginning field marker
+ indent(out) <<
+ "fname, ftype, fid = iprot.readFieldBegin()" << endl;
+
+ // Check for field STOP marker and break
+ indent(out) <<
+ "if (ftype === TType::STOP)" << endl;
+ indent_up();
+ indent(out) <<
+ "break" << endl;
+ indent_down();
+ if (fields.size() > 0) {
+ indent(out) <<
+ "end" << endl;
+ }
+
+ // Switch statement on the field we are reading
+ bool first = true;
+
+ // Generate deserialization code for known cases
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ if (first) {
+ first = false;
+ out <<
+ indent() << "if ";
+ } else {
+ out <<
+ indent() << "elsif ";
+ }
+ out << "(fid == " << (*f_iter)->get_key() << ")" << endl;
+ indent_up();
+ generate_deserialize_field(out, *f_iter, "@");
+ indent_down();
+ }
+
+ // In the default case we skip the field
+ out <<
+ indent() << "else" << endl <<
+ indent() << " iprot.skip(ftype)" << endl <<
+ indent() << "end" << endl;
+
+ // Read field end marker
+ indent(out) <<
+ "iprot.readFieldEnd()" << endl;
+
+ indent_down();
+ indent(out) << "end" << endl;
+
+ indent(out) <<
+ "iprot.readStructEnd()" << endl;
+
+ indent_down();
+ indent(out) << "end" << endl;
+ out << endl;
+}
+
+void t_rb_generator::generate_rb_struct_writer(ofstream& out,
+ t_struct* tstruct) {
+ string name = tstruct->get_name();
+ const vector<t_field*>& fields = tstruct->get_members();
+ vector<t_field*>::const_iterator f_iter;
+
+ indent(out) <<
+ "def write(oprot)" << endl;
+ indent_up();
+
+ indent(out) <<
+ "oprot.writeStructBegin('" << name << "')" << endl;
+
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ // Write field header
+ indent(out) <<
+ "if (@" << (*f_iter)->get_name() << " != nil)" << endl;
+ indent_up();
+ indent(out) <<
+ "oprot.writeFieldBegin(" <<
+ "'" << (*f_iter)->get_name() << "', " <<
+ type_to_enum((*f_iter)->get_type()) << ", " <<
+ (*f_iter)->get_key() << ")" << endl;
+
+ // Write field contents
+ generate_serialize_field(out, *f_iter, "@");
+
+ // Write field closer
+ indent(out) <<
+ "oprot.writeFieldEnd()" << endl;
+
+ indent_down();
+ indent(out) << "end" << endl;
+ }
+
+ // Write the struct map
+ out <<
+ indent() << "oprot.writeFieldStop()" << endl <<
+ indent() << "oprot.writeStructEnd()" << endl;
+
+ indent_down();
+ indent(out) << "end" << endl;
+
+ out <<
+ endl;
+}
+
+/**
+ * Generates a thrift service.
+ *
+ * @param tservice The service definition
+ */
+void t_rb_generator::generate_service(t_service* tservice) {
+ string f_service_name = string(T_RB_DIR)+"/"+service_name_+".rb";
+ f_service_.open(f_service_name.c_str());
+
+ f_service_ <<
+ rb_autogen_comment() << endl <<
+ rb_imports() << endl;
+
+ if (tservice->get_extends() != NULL) {
+ f_service_ <<
+ "require '" << tservice->get_extends()->get_name() << "'" << endl;
+ }
+
+ f_service_ <<
+ "require 'thrift/thrift'" << endl <<
+ "require '" << program_name_ << "_types'" << endl <<
+ endl;
+
+ f_service_ << "module " << tservice->get_name() << endl;
+ indent_up();
+
+ // Generate the three main parts of the service (well, two for now in PHP)
+ generate_service_interface(tservice);
+ generate_service_client(tservice);
+ generate_service_server(tservice);
+ generate_service_helpers(tservice);
+ generate_service_remote(tservice);
+
+ indent_down();
+ f_service_ << "end" << endl <<
+ endl;
+
+ // Close service file
+ f_service_.close();
+}
+
+/**
+ * Generates helper functions for a service.
+ *
+ * @param tservice The service to generate a header definition for
+ */
+void t_rb_generator::generate_service_helpers(t_service* tservice) {
+ vector<t_function*> functions = tservice->get_functions();
+ vector<t_function*>::iterator f_iter;
+
+ indent(f_service_) <<
+ "# HELPER FUNCTIONS AND STRUCTURES" << endl << endl;
+
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ t_struct* ts = (*f_iter)->get_arglist();
+ generate_rb_struct_definition(f_service_, ts, false);
+ generate_rb_function_helpers(*f_iter);
+ }
+}
+
+/**
+ * Generates a struct and helpers for a function.
+ *
+ * @param tfunction The function
+ */
+void t_rb_generator::generate_rb_function_helpers(t_function* tfunction) {
+ t_struct result(program_, tfunction->get_name() + "_result");
+ t_field success(tfunction->get_returntype(), "success", 0);
+ if (!tfunction->get_returntype()->is_void()) {
+ result.append(&success);
+ }
+
+ t_struct* xs = tfunction->get_xceptions();
+ const vector<t_field*>& fields = xs->get_members();
+ vector<t_field*>::const_iterator f_iter;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ result.append(*f_iter);
+ }
+ generate_rb_struct_definition(f_service_, &result, false, true);
+}
+
+/**
+ * Generates a service interface definition.
+ *
+ * @param tservice The service to generate a header definition for
+ */
+void t_rb_generator::generate_service_interface(t_service* tservice) {
+ f_service_ <<
+ indent() << "module Iface" << endl;
+ indent_up();
+
+ if (tservice->get_extends() != NULL) {
+ string extends = type_name(tservice->get_extends());
+ indent(f_service_) << "include " << extends << ".Iface" << endl;
+ }
+
+ vector<t_function*> functions = tservice->get_functions();
+ vector<t_function*>::iterator f_iter;
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ f_service_ <<
+ indent() << "def " << function_signature(*f_iter) << "" << endl <<
+ indent() << "end" << endl << endl;
+ }
+ indent_down();
+ indent(f_service_) << "end" << endl << endl;
+}
+
+/**
+ * Generates a service client definition.
+ *
+ * @param tservice The service to generate a server for.
+ */
+void t_rb_generator::generate_service_client(t_service* tservice) {
+ string extends = "";
+ string extends_client = "";
+ if (tservice->get_extends() != NULL) {
+ extends = type_name(tservice->get_extends());
+ extends_client = " < " + extends + ".Client, ";
+ }
+
+ indent(f_service_) <<
+ "class Client" << extends_client << endl;
+ indent_up();
+
+ indent(f_service_) <<
+ "include Iface" << endl << endl;
+
+ // Constructor function
+ f_service_ <<
+ indent() << "def initialize(iprot, oprot=nil)" << endl;
+ if (extends.empty()) {
+ f_service_ <<
+ indent() << " @iprot = @oprot = iprot" << endl <<
+ indent() << " if (oprot != nil)" << endl <<
+ indent() << " @oprot = oprot" << endl <<
+ indent() << " end" << endl <<
+ indent() << " @seqid = 0" << endl;
+ }
+ indent(f_service_) << "end" << endl << endl;
+
+ // Generate client method implementations
+ vector<t_function*> functions = tservice->get_functions();
+ vector<t_function*>::const_iterator f_iter;
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ t_struct* arg_struct = (*f_iter)->get_arglist();
+ const vector<t_field*>& fields = arg_struct->get_members();
+ vector<t_field*>::const_iterator fld_iter;
+ string funname = (*f_iter)->get_name();
+
+ // Open function
+ indent(f_service_) <<
+ "def " << function_signature(*f_iter) << endl;
+ indent_up();
+ indent(f_service_) <<
+ "send_" << funname << "(";
+
+ bool first = true;
+ for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
+ if (first) {
+ first = false;
+ } else {
+ f_service_ << ", ";
+ }
+ f_service_ << (*fld_iter)->get_name();
+ }
+ f_service_ << ")" << endl;
+
+ if (!(*f_iter)->is_async()) {
+ f_service_ << indent();
+ if (!(*f_iter)->get_returntype()->is_void()) {
+ f_service_ << "return ";
+ }
+ f_service_ <<
+ "recv_" << funname << "()" << endl;
+ }
+ indent_down();
+ indent(f_service_) << "end" << endl;
+ f_service_ << endl;
+
+ indent(f_service_) <<
+ "def send_" << function_signature(*f_iter) << endl;
+ indent_up();
+
+ std::string argsname = capitalize((*f_iter)->get_name() + "_args");
+
+ // Serialize the request header
+ f_service_ <<
+ indent() << "@oprot.writeMessageBegin('" << (*f_iter)->get_name() << "', TMessageType::CALL, @seqid)" << endl;
+
+ f_service_ <<
+ indent() << "args = " << argsname << ".new()" << endl;
+
+ for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
+ f_service_ <<
+ indent() << "args." << (*fld_iter)->get_name() << " = " << (*fld_iter)->get_name() << endl;
+ }
+
+ // Write to the stream
+ f_service_ <<
+ indent() << "args.write(@oprot)" << endl <<
+ indent() << "@oprot.writeMessageEnd()" << endl <<
+ indent() << "@oprot.trans.flush()" << endl;
+
+ indent_down();
+ indent(f_service_) << "end" << endl;
+
+ if (!(*f_iter)->is_async()) {
+ std::string resultname = capitalize((*f_iter)->get_name() + "_result");
+ t_struct noargs(program_);
+
+ t_function recv_function((*f_iter)->get_returntype(),
+ string("recv_") + (*f_iter)->get_name(),
+ &noargs);
+ // Open function
+ f_service_ <<
+ endl <<
+ indent() << "def " << function_signature(&recv_function) << endl;
+ indent_up();
+
+ f_service_ <<
+ indent() << "fname, mtype, rseqid = @iprot.readMessageBegin()" << endl;
+
+ // TODO(mcslee): Validate message reply here, seq ids etc.
+
+ f_service_ <<
+ indent() << "result = " << resultname << ".new()" << endl <<
+ indent() << "result.read(@iprot)" << endl <<
+ indent() << "@iprot.readMessageEnd()" << endl;
+
+ // Careful, only return _result if not a void function
+ if (!(*f_iter)->get_returntype()->is_void()) {
+ f_service_ <<
+ indent() << "if result.success != nil" << endl <<
+ indent() << " return result.success" << endl <<
+ indent() << "end" << endl;
+ }
+
+ t_struct* xs = (*f_iter)->get_xceptions();
+ const std::vector<t_field*>& xceptions = xs->get_members();
+ vector<t_field*>::const_iterator x_iter;
+ for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
+ f_service_ <<
+ indent() << "if result." << (*x_iter)->get_name() << " != nil" << endl <<
+ indent() << " raise result." << (*x_iter)->get_name() << "" << endl <<
+ indent() << "end" << endl;
+ }
+
+ // Careful, only return _result if not a void function
+ if ((*f_iter)->get_returntype()->is_void()) {
+ indent(f_service_) <<
+ "return" << endl;
+ } else {
+ f_service_ <<
+ indent() << "raise StandardError.new('" << (*f_iter)->get_name() << " failed: unknown result')" << endl;
+ }
+
+ // Close function
+ indent_down();
+ indent(f_service_) << "end" << endl << endl;
+ }
+ }
+
+ indent_down();
+ indent(f_service_) << "end" << endl << endl;
+}
+
+/**
+ * Generates a command line tool for making remote requests
+ *
+ * @param tservice The service to generate a remote for.
+ */
+void t_rb_generator::generate_service_remote(t_service* tservice) {
+ vector<t_function*> functions = tservice->get_functions();
+ vector<t_function*>::iterator f_iter;
+
+ string f_remote_name = string(T_RB_DIR)+"/"+service_name_+"-remote";
+ ofstream f_remote;
+ f_remote.open(f_remote_name.c_str());
+
+ f_remote <<
+ "#!/usr/bin/ruby" << endl <<
+ rb_autogen_comment() << endl <<
+ "import sys" << endl <<
+ "import pprint" << endl <<
+ "from thrift.transport import TTransport" << endl <<
+ "from thrift.transport import TSocket" << endl <<
+ "from thrift.protocol import TBinaryProtocol" << endl <<
+ endl;
+
+ f_remote <<
+ "import " << service_name_ << endl <<
+ "from " << program_name_ << "_types import *" << endl <<
+ endl;
+
+ f_remote <<
+ "if len(sys.argv) <= 1 or sys.argv[1] == '--help':" << endl <<
+ " print ''" << endl <<
+ " print 'Usage: ' + sys.argv[0] + ' [-h host:port] [-f[ramed]] function [arg1,[arg2...]]'" << endl <<
+ " print ''" << endl <<
+ " print 'Functions:'" << endl;
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ f_remote <<
+ " print ' " << (*f_iter)->get_returntype()->get_name() << " " << (*f_iter)->get_name() << "(";
+ t_struct* arg_struct = (*f_iter)->get_arglist();
+ const std::vector<t_field*>& args = arg_struct->get_members();
+ vector<t_field*>::const_iterator a_iter;
+ int num_args = args.size();
+ bool first = true;
+ for (int i = 0; i < num_args; ++i) {
+ if (first) {
+ first = false;
+ } else {
+ f_remote << ", ";
+ }
+ f_remote <<
+ args[i]->get_type()->get_name() << " " << args[i]->get_name();
+ }
+ f_remote << ")'" << endl;
+ }
+ f_remote <<
+ " print ''" << endl <<
+ " sys.exit(0)" << endl <<
+ endl;
+
+ f_remote <<
+ "pp = pprint.PrettyPrinter(indent = 2)" << endl <<
+ "host = 'localhost'" << endl <<
+ "port = 9090" << endl <<
+ "framed = False" << endl <<
+ "argi = 1" << endl <<
+ endl <<
+ "if sys.argv[1] == '-h':" << endl <<
+ " parts = sys.argv[2].split(':') " << endl <<
+ " host = parts[0]" << endl <<
+ " port = int(parts[1])" << endl <<
+ " argi = 3" << endl <<
+ endl <<
+ "if sys.argv[argi] == '-f' or sys.argv[argi] == '-framed':" << endl <<
+ " framed = True" << endl <<
+ " argi += 1" << endl <<
+ endl <<
+ "cmd = sys.argv[argi]" << endl <<
+ "args = sys.argv[argi+1:]" << endl <<
+ endl <<
+ "socket = TSocket.TSocket(host, port)" << endl <<
+ "if framed:" << endl <<
+ " transport = TTransport.TFramedTransport(socket)" << endl <<
+ "else:" << endl <<
+ " transport = TTransport.TBufferedTransport(socket)" << endl <<
+ "protocol = TBinaryProtocol.TBinaryProtocol(transport)" << endl <<
+ "client = " << service_name_ << ".Client(protocol)" << endl <<
+ "transport.open()" << endl <<
+ endl;
+
+ // Generate the dispatch methods
+ bool first = true;
+
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ if (first) {
+ first = false;
+ } else {
+ f_remote << "el";
+ }
+
+ t_struct* arg_struct = (*f_iter)->get_arglist();
+ const std::vector<t_field*>& args = arg_struct->get_members();
+ vector<t_field*>::const_iterator a_iter;
+ int num_args = args.size();
+
+ f_remote <<
+ "if cmd == '" << (*f_iter)->get_name() << "':" << endl <<
+ " if len(args) != " << num_args << ":" << endl <<
+ " print '" << (*f_iter)->get_name() << " requires " << num_args << " args'" << endl <<
+ " sys.exit(1)" << endl <<
+ " pp.pprint(client." << (*f_iter)->get_name() << "(";
+ for (int i = 0; i < num_args; ++i) {
+ if (args[i]->get_type()->is_string()) {
+ f_remote << "args[" << i << "],";
+ } else {
+ f_remote << "eval(args[" << i << "]),";
+ }
+ }
+ f_remote << "))" << endl;
+
+ f_remote << endl;
+ }
+
+ f_remote << "transport.close()" << endl;
+
+ // Close service file
+ f_remote.close();
+
+ // Make file executable, love that bitwise OR action
+ chmod(f_remote_name.c_str(),
+ S_IRUSR |
+ S_IWUSR |
+ S_IXUSR |
+ S_IRGRP |
+ S_IXGRP |
+ S_IROTH |
+ S_IXOTH);
+}
+
+/**
+ * Generates a service server definition.
+ *
+ * @param tservice The service to generate a server for.
+ */
+void t_rb_generator::generate_service_server(t_service* tservice) {
+ // Generate the dispatch methods
+ vector<t_function*> functions = tservice->get_functions();
+ vector<t_function*>::iterator f_iter;
+
+ string extends = "";
+ string extends_processor = "";
+ if (tservice->get_extends() != NULL) {
+ extends = type_name(tservice->get_extends());
+ extends_processor = " < " + extends + ".Processor, ";
+ }
+
+ // Generate the header portion
+ indent(f_service_) <<
+ "class Processor" << extends_processor << endl;
+ indent_up();
+
+ f_service_ <<
+ indent() << "include Iface" << endl <<
+ indent() << "include TProcessor" << endl <<
+ endl;
+
+ indent(f_service_) <<
+ "def initialize(handler)" << endl;
+ indent_up();
+ if (extends.empty()) {
+ f_service_ <<
+ indent() << "@handler = handler" << endl <<
+ indent() << "@processMap = {}" << endl;
+ }
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ f_service_ <<
+ indent() << "@processMap[\"" << (*f_iter)->get_name() << "\"] = Processor.process_" << (*f_iter)->get_name() << endl;
+ }
+ indent_down();
+ indent(f_service_) << "end" << endl << endl;
+
+ // Generate the server implementation
+ indent(f_service_) <<
+ "def process(iprot, oprot)" << endl;
+ indent_up();
+
+ f_service_ <<
+ indent() << "name, type, seqid = iprot.readMessageBegin()" << endl;
+
+ // TODO(mcslee): validate message
+
+ // HOT: dictionary function lookup
+ f_service_ <<
+ indent() << "if (@processMap.has_key?(name))" << endl <<
+ indent() << " @processMap[name].call(seqid, iprot, oprot)" << endl <<
+ indent() << "else" << endl <<
+ indent() << " print 'Unknown function %s' % (name)" << endl <<
+ indent() << "end" << endl;
+
+ // Read end of args field, the T_STOP, and the struct close
+ f_service_ <<
+ indent() << "return true" << endl;
+
+ indent_down();
+ indent(f_service_) << "end" << endl << endl;
+
+ // Generate the process subfunctions
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ generate_process_function(tservice, *f_iter);
+ }
+
+ indent_down();
+ indent(f_service_) << "end" << endl << endl;
+}
+
+/**
+ * Generates a process function definition.
+ *
+ * @param tfunction The function to write a dispatcher for
+ */
+void t_rb_generator::generate_process_function(t_service* tservice,
+ t_function* tfunction) {
+ // Open function
+ indent(f_service_) <<
+ "def process_" << tfunction->get_name() <<
+ "(seqid, iprot, oprot)" << endl;
+ indent_up();
+
+ string argsname = tfunction->get_name() + "_args";
+ string resultname = tfunction->get_name() + "_result";
+
+ f_service_ <<
+ indent() << "args = " << argsname << "()" << endl <<
+ indent() << "args.read(iprot)" << endl <<
+ indent() << "iprot.readMessageEnd()" << endl;
+
+ t_struct* xs = tfunction->get_xceptions();
+ const std::vector<t_field*>& xceptions = xs->get_members();
+ vector<t_field*>::const_iterator x_iter;
+
+ // Declare result for non async function
+ if (!tfunction->is_async()) {
+ f_service_ <<
+ indent() << "result = " << resultname << "()" << endl;
+ }
+
+ // Try block for a function with exceptions
+ if (xceptions.size() > 0) {
+ f_service_ <<
+ indent() << "begin" << endl;
+ indent_up();
+ }
+
+ // Generate the function call
+ t_struct* arg_struct = tfunction->get_arglist();
+ const std::vector<t_field*>& fields = arg_struct->get_members();
+ vector<t_field*>::const_iterator f_iter;
+
+ f_service_ << indent();
+ if (!tfunction->is_async() && !tfunction->get_returntype()->is_void()) {
+ f_service_ << "result.success = ";
+ }
+ f_service_ <<
+ "@handler." << tfunction->get_name() << "(";
+ bool first = true;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ if (first) {
+ first = false;
+ } else {
+ f_service_ << ", ";
+ }
+ f_service_ << "args." << (*f_iter)->get_name();
+ }
+ f_service_ << ")" << endl;
+
+ if (!tfunction->is_async() && xceptions.size() > 0) {
+ indent_down();
+ for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
+ f_service_ <<
+ indent() << "rescue " << (*x_iter)->get_type()->get_name() << " => " << (*x_iter)->get_name() << endl;
+ if (!tfunction->is_async()) {
+ indent_up();
+ f_service_ <<
+ indent() << "result." << (*x_iter)->get_name() << " = " << (*x_iter)->get_name() << endl;
+ indent_down();
+ }
+ }
+ indent(f_service_) << "end" << endl;
+ }
+
+ // Shortcut out here for async functions
+ if (tfunction->is_async()) {
+ f_service_ <<
+ indent() << "return" << endl;
+ indent_down();
+ indent(f_service_) << "end" << endl << endl;
+ return;
+ }
+
+ f_service_ <<
+ indent() << "oprot.writeMessageBegin('" << tfunction->get_name() << "', TMessageType::REPLY, seqid)" << endl <<
+ indent() << "result.write(oprot)" << endl <<
+ indent() << "oprot.writeMessageEnd()" << endl <<
+ indent() << "oprot.trans.flush()" << endl;
+
+ // Close function
+ indent_down();
+ indent(f_service_) << "end" << endl << endl;
+}
+
+/**
+ * Deserializes a field of any type.
+ */
+void t_rb_generator::generate_deserialize_field(ofstream &out,
+ t_field* tfield,
+ string prefix,
+ bool inclass) {
+ t_type* type = tfield->get_type();
+ while (type->is_typedef()) {
+ type = ((t_typedef*)type)->get_type();
+ }
+
+ if (type->is_void()) {
+ throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " +
+ prefix + tfield->get_name();
+ }
+
+ string name = prefix + tfield->get_name();
+
+ if (type->is_struct() || type->is_xception()) {
+ generate_deserialize_struct(out,
+ (t_struct*)type,
+ name);
+ } else if (type->is_container()) {
+ generate_deserialize_container(out, type, name);
+ } else if (type->is_base_type() || type->is_enum()) {
+ indent(out) <<
+ name << " = iprot.";
+
+ if (type->is_base_type()) {
+ t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ switch (tbase) {
+ case t_base_type::TYPE_VOID:
+ throw "compiler error: cannot serialize void field in a struct: " +
+ name;
+ break;
+ case t_base_type::TYPE_STRING:
+ out << "readString();";
+ break;
+ case t_base_type::TYPE_BOOL:
+ out << "readBool();";
+ break;
+ case t_base_type::TYPE_BYTE:
+ out << "readByte();";
+ break;
+ case t_base_type::TYPE_I16:
+ out << "readI16();";
+ break;
+ case t_base_type::TYPE_I32:
+ out << "readI32();";
+ break;
+ case t_base_type::TYPE_I64:
+ out << "readI64();";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ out << "readDouble();";
+ break;
+ default:
+ throw "compiler error: no PHP name for base type " + tbase;
+ }
+ } else if (type->is_enum()) {
+ out << "readI32();";
+ }
+ out << endl;
+
+ } else {
+ printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
+ tfield->get_name().c_str(), type->get_name().c_str());
+ }
+}
+
+/**
+ * Generates an unserializer for a struct, calling read()
+ */
+void t_rb_generator::generate_deserialize_struct(ofstream &out,
+ t_struct* tstruct,
+ string prefix) {
+ out <<
+ indent() << prefix << " = " << type_name(tstruct) << "()" << endl <<
+ indent() << prefix << ".read(iprot)" << endl;
+}
+
+/**
+ * Serialize a container by writing out the header followed by
+ * data and then a footer.
+ */
+void t_rb_generator::generate_deserialize_container(ofstream &out,
+ t_type* ttype,
+ string prefix) {
+ string size = tmp("_size");
+ string ktype = tmp("_ktype");
+ string vtype = tmp("_vtype");
+ string etype = tmp("_etype");
+
+ t_field fsize(g_type_i32, size);
+ t_field fktype(g_type_byte, ktype);
+ t_field fvtype(g_type_byte, vtype);
+ t_field fetype(g_type_byte, etype);
+
+ // Declare variables, read header
+ if (ttype->is_map()) {
+ out <<
+ indent() << prefix << " = {}" << endl <<
+ indent() << "(" << ktype << ", " << vtype << ", " << size << " ) = iprot.readMapBegin() " << endl;
+ } else if (ttype->is_set()) {
+ out <<
+ indent() << prefix << " = {}" << endl <<
+ indent() << "(" << etype << ", " << size << ") = iprot.readSetBegin()" << endl;
+ } else if (ttype->is_list()) {
+ out <<
+ indent() << prefix << " = []" << endl <<
+ indent() << "(" << etype << ", " << size << ") = iprot.readListBegin()" << endl;
+ }
+
+ // For loop iterates over elements
+ string i = tmp("_i");
+ indent(out) <<
+ "for " << i << " in (1.." << size << ")" << endl;
+
+ indent_up();
+
+ if (ttype->is_map()) {
+ generate_deserialize_map_element(out, (t_map*)ttype, prefix);
+ } else if (ttype->is_set()) {
+ generate_deserialize_set_element(out, (t_set*)ttype, prefix);
+ } else if (ttype->is_list()) {
+ generate_deserialize_list_element(out, (t_list*)ttype, prefix);
+ }
+
+ indent_down();
+ indent(out) << "end" << endl;
+
+ // Read container end
+ if (ttype->is_map()) {
+ indent(out) << "iprot.readMapEnd()" << endl;
+ } else if (ttype->is_set()) {
+ indent(out) << "iprot.readSetEnd()" << endl;
+ } else if (ttype->is_list()) {
+ indent(out) << "iprot.readListEnd()" << endl;
+ }
+}
+
+
+/**
+ * Generates code to deserialize a map
+ */
+void t_rb_generator::generate_deserialize_map_element(ofstream &out,
+ t_map* tmap,
+ string prefix) {
+ string key = tmp("_key");
+ string val = tmp("_val");
+ t_field fkey(tmap->get_key_type(), key);
+ t_field fval(tmap->get_val_type(), val);
+
+ generate_deserialize_field(out, &fkey);
+ generate_deserialize_field(out, &fval);
+
+ indent(out) <<
+ prefix << "[" << key << "] = " << val << endl;
+}
+
+/**
+ * Write a set element
+ */
+void t_rb_generator::generate_deserialize_set_element(ofstream &out,
+ t_set* tset,
+ string prefix) {
+ string elem = tmp("_elem");
+ t_field felem(tset->get_elem_type(), elem);
+
+ generate_deserialize_field(out, &felem);
+
+ indent(out) <<
+ prefix << "[" << elem << "] = true" << endl;
+}
+
+/**
+ * Write a list element
+ */
+void t_rb_generator::generate_deserialize_list_element(ofstream &out,
+ t_list* tlist,
+ string prefix) {
+ string elem = tmp("_elem");
+ t_field felem(tlist->get_elem_type(), elem);
+
+ generate_deserialize_field(out, &felem);
+
+ indent(out) <<
+ prefix << ".push(" << elem << ")" << endl;
+}
+
+
+/**
+ * Serializes a field of any type.
+ *
+ * @param tfield The field to serialize
+ * @param prefix Name to prepend to field name
+ */
+void t_rb_generator::generate_serialize_field(ofstream &out,
+ t_field* tfield,
+ string prefix) {
+ t_type* type = tfield->get_type();
+ while (type->is_typedef()) {
+ type = ((t_typedef*)type)->get_type();
+ }
+
+ // Do nothing for void types
+ if (type->is_void()) {
+ throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " +
+ prefix + tfield->get_name();
+ }
+
+ if (type->is_struct() || type->is_xception()) {
+ generate_serialize_struct(out,
+ (t_struct*)type,
+ prefix + tfield->get_name());
+ } else if (type->is_container()) {
+ generate_serialize_container(out,
+ type,
+ prefix + tfield->get_name());
+ } else if (type->is_base_type() || type->is_enum()) {
+
+ string name = prefix + tfield->get_name();
+
+ indent(out) <<
+ "oprot.";
+
+ if (type->is_base_type()) {
+ t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ switch (tbase) {
+ case t_base_type::TYPE_VOID:
+ throw
+ "compiler error: cannot serialize void field in a struct: " + name;
+ break;
+ case t_base_type::TYPE_STRING:
+ out << "writeString(" << name << ")";
+ break;
+ case t_base_type::TYPE_BOOL:
+ out << "writeBool(" << name << ")";
+ break;
+ case t_base_type::TYPE_BYTE:
+ out << "writeByte(" << name << ")";
+ break;
+ case t_base_type::TYPE_I16:
+ out << "writeI16(" << name << ")";
+ break;
+ case t_base_type::TYPE_I32:
+ out << "writeI32(" << name << ")";
+ break;
+ case t_base_type::TYPE_I64:
+ out << "writeI64(" << name << ")";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ out << "writeDouble(" << name << ")";
+ break;
+ default:
+ throw "compiler error: no PHP name for base type " + tbase;
+ }
+ } else if (type->is_enum()) {
+ out << "writeI32(" << name << ")";
+ }
+ out << endl;
+ } else {
+ printf("DO NOT KNOW HOW TO SERIALIZE FIELD '%s%s' TYPE '%s'\n",
+ prefix.c_str(),
+ tfield->get_name().c_str(),
+ type->get_name().c_str());
+ }
+}
+
+/**
+ * Serializes all the members of a struct.
+ *
+ * @param tstruct The struct to serialize
+ * @param prefix String prefix to attach to all fields
+ */
+void t_rb_generator::generate_serialize_struct(ofstream &out,
+ t_struct* tstruct,
+ string prefix) {
+ indent(out) <<
+ prefix << ".write(oprot)" << endl;
+}
+
+void t_rb_generator::generate_serialize_container(ofstream &out,
+ t_type* ttype,
+ string prefix) {
+ if (ttype->is_map()) {
+ indent(out) <<
+ "oprot.writeMapBegin(" <<
+ type_to_enum(((t_map*)ttype)->get_key_type()) << ", " <<
+ type_to_enum(((t_map*)ttype)->get_val_type()) << ", " <<
+ prefix << ".length)" << endl;
+ } else if (ttype->is_set()) {
+ indent(out) <<
+ "oprot.writeSetBegin(" <<
+ type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " <<
+ prefix << ".length)" << endl;
+ } else if (ttype->is_list()) {
+ indent(out) <<
+ "oprot.writeListBegin(" <<
+ type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " <<
+ prefix << ".length)" << endl;
+ }
+
+ if (ttype->is_map()) {
+ string kiter = tmp("kiter");
+ string viter = tmp("viter");
+ indent(out) <<
+ prefix << ".each do |" << kiter << ", " << viter << "|" << endl;
+ indent_up();
+ generate_serialize_map_element(out, (t_map*)ttype, kiter, viter);
+ indent_down();
+ indent(out) << "end" << endl;
+ } else if (ttype->is_set()) {
+ string iter = tmp("iter");
+ string t = tmp("true");
+ indent(out) <<
+ prefix << ".each do |" << iter << ", " << t << "|" << endl;
+ indent_up();
+ generate_serialize_set_element(out, (t_set*)ttype, iter);
+ indent_down();
+ indent(out) << "end" << endl;
+ } else if (ttype->is_list()) {
+ string iter = tmp("iter");
+ indent(out) <<
+ prefix << ".each do |" << iter << "|" << endl;
+ indent_up();
+ generate_serialize_list_element(out, (t_list*)ttype, iter);
+ indent_down();
+ indent(out) << "end" << endl;
+ }
+
+ if (ttype->is_map()) {
+ indent(out) <<
+ "oprot.writeMapEnd()" << endl;
+ } else if (ttype->is_set()) {
+ indent(out) <<
+ "oprot.writeSetEnd()" << endl;
+ } else if (ttype->is_list()) {
+ indent(out) <<
+ "oprot.writeListEnd()" << endl;
+ }
+}
+
+/**
+ * Serializes the members of a map.
+ *
+ */
+void t_rb_generator::generate_serialize_map_element(ofstream &out,
+ t_map* tmap,
+ string kiter,
+ string viter) {
+ t_field kfield(tmap->get_key_type(), kiter);
+ generate_serialize_field(out, &kfield, "");
+
+ t_field vfield(tmap->get_val_type(), viter);
+ generate_serialize_field(out, &vfield, "");
+}
+
+/**
+ * Serializes the members of a set.
+ */
+void t_rb_generator::generate_serialize_set_element(ofstream &out,
+ t_set* tset,
+ string iter) {
+ t_field efield(tset->get_elem_type(), iter);
+ generate_serialize_field(out, &efield, "");
+}
+
+/**
+ * Serializes the members of a list.
+ */
+void t_rb_generator::generate_serialize_list_element(ofstream &out,
+ t_list* tlist,
+ string iter) {
+ t_field efield(tlist->get_elem_type(), iter);
+ generate_serialize_field(out, &efield, "");
+}
+
+/**
+ * Declares a field, which may include initialization as necessary.
+ *
+ * @param ttype The type
+ */
+string t_rb_generator::declare_field(t_field* tfield, bool init, bool obj) {
+ string result = "@" + tfield->get_name();
+ if (init) {
+ t_type* type = tfield->get_type();
+ while (type->is_typedef()) {
+ type = ((t_typedef*)type)->get_type();
+ }
+ if (type->is_base_type()) {
+ t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ switch (tbase) {
+ case t_base_type::TYPE_VOID:
+ break;
+ case t_base_type::TYPE_STRING:
+ result += " = ''";
+ break;
+ case t_base_type::TYPE_BOOL:
+ result += " = false";
+ break;
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ result += " = 0";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ result += " = 0.0";
+ break;
+ default:
+ throw "compiler error: no PHP initializer for base type " + tbase;
+ }
+ } else if (type->is_enum()) {
+ result += " = 0";
+ } else if (type->is_container()) {
+ if (type->is_map() || type->is_set()) {
+ result += " = {}";
+ } else {
+ result += " = []";
+ }
+ } else if (type->is_struct() || type->is_xception()) {
+ if (obj) {
+ result += " = " + type_name((t_struct*)type) + "()";
+ } else {
+ result += " = nil";
+ }
+ }
+ }
+ return result;
+}
+
+/**
+ * Renders a function signature of the form 'type name(args)'
+ *
+ * @param tfunction Function definition
+ * @return String of rendered function definition
+ */
+string t_rb_generator::function_signature(t_function* tfunction,
+ string prefix) {
+ // TODO(mcslee): Nitpicky, no ',' if argument_list is empty
+ return
+ prefix + tfunction->get_name() +
+ "(" + argument_list(tfunction->get_arglist()) + ")";
+}
+
+/**
+ * Renders a field list
+ */
+string t_rb_generator::argument_list(t_struct* tstruct) {
+ string result = "";
+
+ const vector<t_field*>& fields = tstruct->get_members();
+ vector<t_field*>::const_iterator f_iter;
+ bool first = true;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ if (first) {
+ first = false;
+ } else {
+ result += ", ";
+ }
+ result += (*f_iter)->get_name();
+ }
+ return result;
+}
+
+string t_rb_generator::type_name(t_type* ttype) {
+ string prefix = "";
+ t_program* program = ttype->get_program();
+ if (program != NULL && program != program_) {
+ if (!ttype->is_service()) {
+ prefix = program->get_name() + "_types.";
+ }
+ }
+
+ string name = ttype->get_name();
+ if (ttype->is_struct() || ttype->is_xception()) {
+ name = capitalize(ttype->get_name());
+ }
+
+ return prefix + name;
+}
+
+/**
+ * Converts the parse type to a Ruby tyoe
+ */
+string t_rb_generator::type_to_enum(t_type* type) {
+ while (type->is_typedef()) {
+ type = ((t_typedef*)type)->get_type();
+ }
+
+ if (type->is_base_type()) {
+ t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ switch (tbase) {
+ case t_base_type::TYPE_VOID:
+ throw "NO T_VOID CONSTRUCT";
+ case t_base_type::TYPE_STRING:
+ return "TType::STRING";
+ case t_base_type::TYPE_BOOL:
+ return "TType::BOOL";
+ case t_base_type::TYPE_BYTE:
+ return "TType::BYTE";
+ case t_base_type::TYPE_I16:
+ return "TType::I16";
+ case t_base_type::TYPE_I32:
+ return "TType::I32";
+ case t_base_type::TYPE_I64:
+ return "TType::I64";
+ case t_base_type::TYPE_DOUBLE:
+ return "TType::DOUBLE";
+ }
+ } else if (type->is_enum()) {
+ return "TType::I32";
+ } else if (type->is_struct() || type->is_xception()) {
+ return "TType::STRUCT";
+ } else if (type->is_map()) {
+ return "TType::MAP";
+ } else if (type->is_set()) {
+ return "TType::SET";
+ } else if (type->is_list()) {
+ return "TType::LIST";
+ }
+
+ throw "INVALID TYPE IN type_to_enum: " + type->get_name();
+}
diff --git a/compiler/cpp/src/generate/t_rb_generator.h b/compiler/cpp/src/generate/t_rb_generator.h
new file mode 100644
index 0000000..9299f3e
--- /dev/null
+++ b/compiler/cpp/src/generate/t_rb_generator.h
@@ -0,0 +1,148 @@
+#ifndef T_RB_GENERATOR_H
+#define T_RB_GENERATOR_H
+
+#include <string>
+#include <fstream>
+#include <iostream>
+#include <vector>
+
+#include "t_oop_generator.h"
+
+#define T_RB_DIR "gen-rb"
+
+/**
+ * Ruby code generator.
+ *
+ * @author Mark Slee <mcslee@facebook.com>
+ */
+class t_rb_generator : public t_oop_generator {
+ public:
+ t_rb_generator(t_program* program) :
+ t_oop_generator(program) {}
+
+ /**
+ * Init and close methods
+ */
+
+ void init_generator();
+ void close_generator();
+
+ /**
+ * Program-level generation functions
+ */
+
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_const (t_const* tconst);
+ void generate_struct (t_struct* tstruct);
+ void generate_xception (t_struct* txception);
+ void generate_service (t_service* tservice);
+
+ void print_const_value (t_type* type, t_const_value* value);
+
+ /**
+ * Struct generation code
+ */
+
+ void generate_rb_struct(t_struct* tstruct, bool is_exception);
+ void generate_rb_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false, bool is_result=false);
+ 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);
+
+ /**
+ * Service-level generation functions
+ */
+
+ void generate_service_helpers (t_service* tservice);
+ void generate_service_interface (t_service* tservice);
+ void generate_service_client (t_service* tservice);
+ void generate_service_remote (t_service* tservice);
+ void generate_service_server (t_service* tservice);
+ void generate_process_function (t_service* tservice, t_function* tfunction);
+
+ /**
+ * Serialization constructs
+ */
+
+ void generate_deserialize_field (std::ofstream &out,
+ t_field* tfield,
+ std::string prefix="",
+ bool inclass=false);
+
+ void generate_deserialize_struct (std::ofstream &out,
+ t_struct* tstruct,
+ std::string prefix="");
+
+ void generate_deserialize_container (std::ofstream &out,
+ t_type* ttype,
+ std::string prefix="");
+
+ void generate_deserialize_set_element (std::ofstream &out,
+ t_set* tset,
+ std::string prefix="");
+
+ void generate_deserialize_map_element (std::ofstream &out,
+ t_map* tmap,
+ std::string prefix="");
+
+ void generate_deserialize_list_element (std::ofstream &out,
+ t_list* tlist,
+ std::string prefix="");
+
+ void generate_serialize_field (std::ofstream &out,
+ t_field* tfield,
+ std::string prefix="");
+
+ void generate_serialize_struct (std::ofstream &out,
+ t_struct* tstruct,
+ std::string prefix="");
+
+ void generate_serialize_container (std::ofstream &out,
+ t_type* ttype,
+ std::string prefix="");
+
+ void generate_serialize_map_element (std::ofstream &out,
+ t_map* tmap,
+ std::string kiter,
+ std::string viter);
+
+ void generate_serialize_set_element (std::ofstream &out,
+ t_set* tmap,
+ std::string iter);
+
+ void generate_serialize_list_element (std::ofstream &out,
+ t_list* tlist,
+ std::string iter);
+
+ /**
+ * Helper rendering functions
+ */
+
+ std::string rb_autogen_comment();
+ std::string rb_imports();
+ std::string render_includes();
+ std::string declare_field(t_field* tfield, bool init=false, bool obj=false);
+ std::string type_name(t_type* ttype);
+ std::string function_signature(t_function* tfunction, std::string prefix="");
+ std::string argument_list(t_struct* tstruct);
+ std::string type_to_enum(t_type* ttype);
+
+ std::string capitalize(std::string in) {
+ in[0] = toupper(in[0]);
+ return in;
+ }
+
+ private:
+
+ /**
+ * File streams
+ */
+
+ std::ofstream f_types_;
+ std::ofstream f_consts_;
+ std::ofstream f_service_;
+
+};
+
+#endif
diff --git a/compiler/cpp/src/main.cc b/compiler/cpp/src/main.cc
index 48552e6..24dca83 100644
--- a/compiler/cpp/src/main.cc
+++ b/compiler/cpp/src/main.cc
@@ -26,6 +26,7 @@
#include "generate/t_java_generator.h"
#include "generate/t_php_generator.h"
#include "generate/t_py_generator.h"
+#include "generate/t_rb_generator.h"
#include "generate/t_xsd_generator.h"
using namespace std;
@@ -109,6 +110,7 @@
*/
bool gen_cpp = false;
bool gen_java = false;
+bool gen_rb = false;
bool gen_py = false;
bool gen_xsd = false;
bool gen_php = false;
@@ -521,6 +523,13 @@
delete py;
}
+ if (gen_rb) {
+ pverbose("Generating Ruby\n");
+ t_rb_generator* rb = new t_rb_generator(program);
+ rb->generate_program();
+ delete rb;
+ }
+
if (gen_xsd) {
pverbose("Generating XSD\n");
t_xsd_generator* xsd = new t_xsd_generator(program);
@@ -584,6 +593,8 @@
gen_phpi = true;
} else if (strcmp(arg, "-py") == 0) {
gen_py = true;
+ } else if (strcmp(arg, "-rb") == 0) {
+ gen_rb = true;
} else if (strcmp(arg, "-xsd") == 0) {
gen_xsd = true;
} else if (strcmp(arg, "-I") == 0) {
@@ -606,7 +617,7 @@
}
// You gotta generate something!
- if (!gen_cpp && !gen_java && !gen_php && !gen_phpi && !gen_py && !gen_xsd) {
+ if (!gen_cpp && !gen_java && !gen_php && !gen_phpi && !gen_py && !gen_rb && !gen_xsd) {
fprintf(stderr, "!!! No output language(s) specified\n\n");
usage();
}