compiler/cpp/src/generate/t_go_generator.cc
/**
 * Serialize a container by writing out the header followed by
 * data and then a footer.
 */
Serialize a container by writing out the header followed by
data and then a footer.
Serialize a container by writing out the header followed by data and then a footer .
void t_go_generator::generate_deserialize_container(ofstream &out,
                                                    t_type* ttype,
                                                    bool   declare,
                                                    string prefix,
                                                    string err) {
  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);
  string eq(" = ");
  if(declare)
    eq = " := ";
  if (ttype->is_map()) {
    out <<
      indent() << ktype << ", " << vtype << ", " << size << ", " << err << " := iprot.ReadMapBegin()" << endl <<
      indent() << "if " << err << " != nil {" << endl <<
      indent() << "  return thrift.NewTProtocolExceptionReadField(" <<
                           -1 << ", \"" <<
                           escape_string(prefix) << "\", \"\", " << 
                           err << ")" << endl <<
      indent() << "}" << endl <<
      indent() << prefix << eq << "thrift.NewTMap(" << ktype << ", " << vtype << ", " << size << ")" << endl;
  } else if (ttype->is_set()) {
    out <<
      indent() << etype << ", " << size << ", " << err << " := iprot.ReadSetBegin()" << endl <<
      indent() << "if " << err << " != nil {" << endl <<
      indent() <<"  return thrift.NewTProtocolExceptionReadField(" <<
                           -1 << ", \"" <<
                           escape_string(prefix) << "\", \"\", " << 
                           err << "); }" << endl <<
      indent() << "}" << endl <<
      indent() << prefix << eq << "thrift.NewTSet(" << etype << ", " << size << ")" << endl;
  } else if (ttype->is_list()) {
    out <<
      indent() << etype << ", " << size << ", " << err << " := iprot.ReadListBegin()" << endl <<
      indent() << "if " << err << " != nil {" << endl <<
      indent() <<"  return thrift.NewTProtocolExceptionReadField(" <<
                           -1 << ", \"" <<
                           escape_string(prefix) << "\", \"\", " << 
                           err << ")" << endl <<
      indent() << "}" << endl <<
      indent() << prefix << eq << "thrift.NewTList(" << etype << ", " << size << ")" << endl;
  } else {
    throw "INVALID TYPE IN generate_deserialize_container '" + ttype->get_name() + "' for prefix '" + prefix + "'";
  }
  string i = tmp("_i");
  out <<
    indent() << "for " << i << ":= 0; " << i << " < " << size << "; " << i << "++ {" << endl;
  indent_up();
  if (ttype->is_map()) {
    generate_deserialize_map_element(out, (t_map*)ttype, declare, prefix);
  } else if (ttype->is_set()) {
    generate_deserialize_set_element(out, (t_set*)ttype, declare, prefix);
  } else if (ttype->is_list()) {
    generate_deserialize_list_element(out, (t_list*)ttype, declare, prefix);
  }
  indent_down();
  out <<
    indent() << "}" << endl;
  if (ttype->is_map()) {
    out <<
      indent() << err << " = iprot.ReadMapEnd()" << endl <<
      indent() << "if " << err << " != nil { return thrift.NewTProtocolExceptionReadField(" 
                        << -1
                        << ", \"" << escape_string(((t_map*)ttype)->get_cpp_name())
                        << "\", " << "\"map\", " << err << "); }" << endl;
  } else if (ttype->is_set()) {
    out <<
      indent() << err << " = iprot.ReadSetEnd()" << endl <<
      indent() << "if " << err << " != nil { return thrift.NewTProtocolExceptionReadField(" 
                        << -1
                        << ", \"" << escape_string(((t_set*)ttype)->get_cpp_name())
                        << "\", " << "\"set\", " << err << "); }" << endl;
  } else if (ttype->is_list()) {
    out <<
      indent() << err << " = iprot.ReadListEnd()" << endl <<
      indent() << "if " << err << " != nil { return thrift.NewTProtocolExceptionReadField(" 
                        << -1
                        << ", \"" << escape_string(((t_list*)ttype)->get_cpp_name())
                        << "\", " << "\"list\"," << err << "); }" << endl;
  }
}
void t_go_generator :: generate_deserialize_container ( ofstream & out , t_type * ttype , bool declare , string prefix , string err ) { string size = tmp ( " " ) ; string ktype = tmp ( " " ) ; string vtype = tmp ( " " ) ; string etype = tmp ( " " ) ; 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 ) ; string eq ( " " ) ; if ( declare ) eq = " " ; if ( ttype -> is_map ( ) ) { out << indent ( ) << ktype << " " << vtype << " " << size << " " << err << " " << endl << indent ( ) << " " << err << " " << endl << indent ( ) << " " << -1 << " \" " << escape_string ( prefix ) << " \" \" \" " << err << " " << endl << indent ( ) << " " << endl << indent ( ) << prefix << eq << " " << ktype << " " << vtype << " " << size << " " << endl ; } else if ( ttype -> is_set ( ) ) { out << indent ( ) << etype << " " << size << " " << err << " " << endl << indent ( ) << " " << err << " " << endl << indent ( ) << " " << -1 << " \" " << escape_string ( prefix ) << " \" \" \" " << err << " " << endl << indent ( ) << " " << endl << indent ( ) << prefix << eq << " " << etype << " " << size << " " << endl ; } else if ( ttype -> is_list ( ) ) { out << indent ( ) << etype << " " << size << " " << err << " " << endl << indent ( ) << " " << err << " " << endl << indent ( ) << " " << -1 << " \" " << escape_string ( prefix ) << " \" \" \" " << err << " " << endl << indent ( ) << " " << endl << indent ( ) << prefix << eq << " " << etype << " " << size << " " << endl ; } else { throw " " + ttype -> get_name ( ) + " " + prefix + " " ; } string i = tmp ( " " ) ; out << indent ( ) << " " << i << " " << i << " " << size << " " << i << " " << endl ; indent_up ( ) ; if ( ttype -> is_map ( ) ) { generate_deserialize_map_element ( out , ( t_map * ) ttype , declare , prefix ) ; } else if ( ttype -> is_set ( ) ) { generate_deserialize_set_element ( out , ( t_set * ) ttype , declare , prefix ) ; } else if ( ttype -> is_list ( ) ) { generate_deserialize_list_element ( out , ( t_list * ) ttype , declare , prefix ) ; } indent_down ( ) ; out << indent ( ) << " " << endl ; if ( ttype -> is_map ( ) ) { out << indent ( ) << err << " " << endl << indent ( ) << " " << err << " " << -1 << " \" " << escape_string ( ( ( t_map * ) ttype ) -> get_cpp_name ( ) ) << " \" " << " \" \" " << err << " " << endl ; } else if ( ttype -> is_set ( ) ) { out << indent ( ) << err << " " << endl << indent ( ) << " " << err << " " << -1 << " \" " << escape_string ( ( ( t_set * ) ttype ) -> get_cpp_name ( ) ) << " \" " << " \" \" " << err << " " << endl ; } else if ( ttype -> is_list ( ) ) { out << indent ( ) << err << " " << endl << indent ( ) << " " << err << " " << -1 << " \" " << escape_string ( ( ( t_list * ) ttype ) -> get_cpp_name ( ) ) << " \" " << " \" \" " << err << " " << endl ; } }
Serialize a container by writing out the header followed by
 data and then a footer.
Serialize a container by writing out the header followed by data and then a footer .
// Declare variables, read header // For loop iterates over elements // Read container end