compiler/cpp/src/generate/t_go_generator.cc
/**
 * Generates a process function definition.
 *
 * @param tfunction The function to write a dispatcher for
 */
Generates a process function definition.
@param tfunction The function to write a dispatcher for
Generates a process function definition . @param tfunction The function to write a dispatcher for
void t_go_generator::generate_process_function(t_service* tservice,
                                               t_function* tfunction) {
  string processorName = privatize(tservice->get_name()) + "Processor" + publicize(tfunction->get_name());
  string argsname = publicize(tfunction->get_name()) + "Args";
  string resultname = publicize(tfunction->get_name()) + "Result";
  vector<t_field*>::const_iterator x_iter;
  f_service_ <<
    indent() << "type " << processorName << " struct {" << endl <<
    indent() << "  handler I" << publicize(tservice->get_name()) << endl <<
    indent() << "}" << endl << endl <<
    indent() << "func (p *" << processorName << ") Process(seqId int32, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) {" << endl;
  indent_up();
  f_service_ <<
    indent() << "args := New" << argsname << "()" << endl <<
    indent() << "if err = args.Read(iprot); err != nil {" << endl <<
    indent() << "  iprot.ReadMessageEnd()" << endl <<
    indent() << "  x := thrift.NewTApplicationException(thrift.PROTOCOL_ERROR, err.String())" << endl <<
    indent() << "  oprot.WriteMessageBegin(\"" << escape_string(tfunction->get_name()) << "\", thrift.EXCEPTION, seqId)" << endl <<
    indent() << "  x.Write(oprot)" << endl <<
    indent() << "  oprot.WriteMessageEnd()" << endl <<
    indent() << "  oprot.Transport().Flush()" << endl <<
    indent() << "  return" << endl <<
    indent() << "}" << endl <<
    indent() << "iprot.ReadMessageEnd()" << endl <<
    indent() << "result := New" << resultname << "()" << endl <<
    indent() << "if ";
  if (!tfunction->is_oneway()) {
    if(!tfunction->get_returntype()->is_void()) {
      f_service_ << "result.Success, ";
    }
    t_struct* exceptions = tfunction->get_xceptions();
    const vector<t_field*>& fields = exceptions->get_members();
    vector<t_field*>::const_iterator f_iter;
    for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
      f_service_ << "result." << publicize(variable_name_to_go_name((*f_iter)->get_name())) << ", ";
    }
  }
  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_ <<
    "err = p.handler." << publicize(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." << publicize(variable_name_to_go_name((*f_iter)->get_name()));
  }
  f_service_ << "); err != nil {" << endl <<
    indent() << "  x := thrift.NewTApplicationException(thrift.INTERNAL_ERROR, \"Internal error processing " << escape_string(tfunction->get_name()) << ": \" + err.String())" << endl <<
    indent() << "  oprot.WriteMessageBegin(\"" << escape_string(tfunction->get_name()) << "\", thrift.EXCEPTION, seqId)" << endl <<
    indent() << "  x.Write(oprot)" << endl <<
    indent() << "  oprot.WriteMessageEnd()" << endl <<
    indent() << "  oprot.Transport().Flush()" << endl <<
    indent() << "  return" << endl <<
    indent() << "}" << endl <<
    indent() << "if err2 := oprot.WriteMessageBegin(\"" << escape_string(tfunction->get_name()) << "\", thrift.REPLY, seqId); err2 != nil {" << endl <<
    indent() << "  err = err2" << endl <<
    indent() << "}" << endl <<
    indent() << "if err2 := result.Write(oprot); err == nil && err2 != nil {" << endl <<
    indent() << "  err = err2" << endl <<
    indent() << "}" << endl <<
    indent() << "if err2 := oprot.WriteMessageEnd(); err == nil && err2 != nil {" << endl <<
    indent() << "  err = err2" << endl <<
    indent() << "}" << endl <<
    indent() << "if err2 := oprot.Transport().Flush(); err == nil && err2 != nil {" << endl <<
    indent() << "  err = err2" << endl <<
    indent() << "}" << endl <<
    indent() << "if err != nil {" << endl <<
    indent() << "  return" << endl <<
    indent() << "}" << endl <<
    indent() << "return true, err" << endl;
  indent_down();
  f_service_ <<
    indent() << "}" << endl << endl;
}
void t_go_generator :: generate_process_function ( t_service * tservice , t_function * tfunction ) { string processorName = privatize ( tservice -> get_name ( ) ) + " " + publicize ( tfunction -> get_name ( ) ) ; string argsname = publicize ( tfunction -> get_name ( ) ) + " " ; string resultname = publicize ( tfunction -> get_name ( ) ) + " " ; vector < t_field * > :: const_iterator x_iter ; f_service_ << indent ( ) << " " << processorName << " " << endl << indent ( ) << " " << publicize ( tservice -> get_name ( ) ) << endl << indent ( ) << " " << endl << endl << indent ( ) << " " << processorName << " " << endl ; indent_up ( ) ; f_service_ << indent ( ) << " " << argsname << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " \" " << escape_string ( tfunction -> get_name ( ) ) << " \" " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << resultname << " " << endl << indent ( ) << " " ; if ( ! tfunction -> is_oneway ( ) ) { if ( ! tfunction -> get_returntype ( ) -> is_void ( ) ) { f_service_ << " " ; } t_struct * exceptions = tfunction -> get_xceptions ( ) ; const vector < t_field * > & fields = exceptions -> get_members ( ) ; vector < t_field * > :: const_iterator f_iter ; for ( f_iter = fields . begin ( ) ; f_iter != fields . end ( ) ; ++ f_iter ) { f_service_ << " " << publicize ( variable_name_to_go_name ( ( * f_iter ) -> get_name ( ) ) ) << " " ; } } 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_ << " " << publicize ( 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_ << " " << publicize ( variable_name_to_go_name ( ( * f_iter ) -> get_name ( ) ) ) ; } f_service_ << " " << endl << indent ( ) << " \" " << escape_string ( tfunction -> get_name ( ) ) << " \" " << endl << indent ( ) << " \" " << escape_string ( tfunction -> get_name ( ) ) << " \" " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " \" " << escape_string ( tfunction -> get_name ( ) ) << " \" " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl ; indent_down ( ) ; f_service_ << indent ( ) << " " << endl << endl ; }
Generates a process function definition.
Generates a process function definition .
// Open function //t_struct* xs = tfunction->get_xceptions(); //const std::vector<t_field*>& xceptions = xs->get_members(); // Generate the function call /*
  indent(f_service_) <<
      "func (p *" << publicize(tservice->get_name()) << "Client) WriteResultsSuccess" << publicize(tfunction->get_name()) <<
      "(success bool, result " << publicize(tfunction->get_name()) << "Result, seqid int32, oprot thrift.TProtocol) (err os.Error) {" << endl;
  indent_up();
  f_service_ <<
    indent() << "result.Success = success" << endl <<
    indent() << "oprot.WriteMessageBegin(\"" << escape_string(tfunction->get_name()) << "\", thrift.REPLY, seqid)" << endl <<
    indent() << "result.Write(oprot)" << endl <<
    indent() << "oprot.WriteMessageEnd()" << endl <<
    indent() << "oprot.Transport().Flush()" << endl <<
    indent() << "return" << endl;
  indent_down();
  f_service_ << 
    indent() << "}" << endl << endl;
  */ // Try block for a function with exceptions /*
  if (!tfunction->is_oneway() && xceptions.size() > 0) {
    indent(f_service_) <<
      "func (p *" << publicize(tservice->get_name()) << "Client) WriteResultsException" << publicize(tfunction->get_name()) <<
      "(error *" << publicize(tfunction->get_name()) << ", result *, seqid, oprot) (err os.Error) {" << endl;
    indent_up();

    // Kinda absurd
    for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
      f_service_ <<
        indent() << "except " << type_name((*x_iter)->get_type()) << ", " << (*x_iter)->get_name() << ":" << endl;
      if (!tfunction->is_oneway()) {
        indent_up();
        f_service_ <<
          indent() << "result." << (*x_iter)->get_name() << " = " << (*x_iter)->get_name() << endl;
        indent_down();
      } else {
        f_service_ <<
          indent() << "pass" << endl;
      }
    }
    f_service_ <<
      indent() << "err = oprot.WriteMessageBegin(\"" << escape_string(tfunction->get_name()) << "\", thrift.REPLY, seqid)" << endl <<
      indent() << "if err != nil { return err }" << endl <<
      indent() << "err = result.Write(oprot)" << endl <<
      indent() << "if err != nil { return err }" << endl <<
      indent() << "err = oprot.WriteMessageEnd()" << endl <<
      indent() << "if err != nil { return err }" << endl <<
      indent() << "err = oprot.Transport().Flush()" << endl <<
      indent() << "if err != nil { return err }" << endl;
    indent_down();
    f_service_ << "}" << endl << endl;
  }
  */