compiler/cpp/src/generate/t_go_generator.cc
/**
 * Generates the read method for a struct
 */
Generates the read method for a struct
Generates the read method for a struct
void t_go_generator::generate_go_struct_reader(ofstream& out,
                                                t_struct* tstruct,
                                                const string& tstruct_name,
                                                bool is_result) {
  (void) is_result;
  const vector<t_field*>& fields = tstruct->get_members();
  vector<t_field*>::const_iterator f_iter;
  string escaped_tstruct_name(escape_string(tstruct->get_name()));
  out <<
    indent() << "func (p *" << tstruct_name << ") Read(iprot thrift.TProtocol) (err thrift.TProtocolException) {" << endl;
  indent_up();
  out <<
    indent() << "_, err = iprot.ReadStructBegin()" << endl <<
    indent() << "if err != nil { return thrift.NewTProtocolExceptionReadStruct(p.ThriftName(), err); }" << endl;
  indent(out) << "for {" << endl;
  indent_up();
  out <<
    indent() << "fieldName, fieldTypeId, fieldId, err := iprot.ReadFieldBegin()" << endl <<
    indent() << "if fieldId < 0 {" << endl <<
    indent() << "  fieldId = int16(p.FieldIdFromFieldName(fieldName))" << endl <<
    indent() << "} else if fieldName == \"\" {" << endl <<
    indent() << "  fieldName = p.FieldNameFromFieldId(int(fieldId))" << endl <<
    indent() << "}" << endl <<
    indent() << "if fieldTypeId == thrift.GENERIC {" << endl <<
    indent() << "  fieldTypeId = p.FieldFromFieldId(int(fieldId)).TypeId()" << endl <<
    indent() << "}" << endl <<
    indent() << "if err != nil {" << endl <<
    indent() << "  return thrift.NewTProtocolExceptionReadField(int(fieldId), fieldName, p.ThriftName(), err)" << endl <<
    indent() << "}" << endl;
  out <<
    indent() << "if fieldTypeId == thrift.STOP { break; }" << endl;
  bool first = true;
  int32_t field_id = -1;
  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
    field_id = (*f_iter)->get_key();
    if (first) {
      first = false;
      indent(out);
    } else {
      indent(out) << "} else ";
    }
    out << "if fieldId == " << field_id << " || fieldName == \"" << escape_string((*f_iter)->get_name()) << "\" {" << endl;
    indent_up();
    out <<
      indent() << "if fieldTypeId == " << type_to_enum((*f_iter)->get_type()) << " {" << endl <<
      indent() << "  err = p.ReadField" << field_id << "(iprot)" << endl <<
      indent() << "  if err != nil { return thrift.NewTProtocolExceptionReadField(int(fieldId), fieldName, p.ThriftName(), err); }" << endl <<
      indent() << "} else if fieldTypeId == thrift.VOID {" << endl <<
      indent() << "  err = iprot.Skip(fieldTypeId)" << endl <<
      indent() << "  if err != nil { return thrift.NewTProtocolExceptionReadField(int(fieldId), fieldName, p.ThriftName(), err); }" << endl <<
      indent() << "} else {" << endl <<
      indent() << "  err = p.ReadField" << field_id << "(iprot)" << endl <<
      indent() << "  if err != nil { return thrift.NewTProtocolExceptionReadField(int(fieldId), fieldName, p.ThriftName(), err); }" << endl <<
      indent() << "}" << endl;
    indent_down();
  }
  if (first) {
    out <<
      indent() << "err = iprot.Skip(fieldTypeId)" << endl <<
      indent() << "if err != nil { return thrift.NewTProtocolExceptionReadField(int(fieldId), fieldName, p.ThriftName(), err); }" << endl;
  } else {
    out <<
      indent() << "} else {" << endl <<
      indent() << "  err = iprot.Skip(fieldTypeId)" << endl <<
      indent() << "  if err != nil { return thrift.NewTProtocolExceptionReadField(int(fieldId), fieldName, p.ThriftName(), err); }" << endl <<
      indent() << "}" << endl;
  }
  out <<
    indent() << "err = iprot.ReadFieldEnd()" << endl <<
    indent() << "if err != nil { return thrift.NewTProtocolExceptionReadField(int(fieldId), fieldName, p.ThriftName(), err); }" << endl;
  indent_down();
  out <<
    indent() << "}" << endl <<
    indent() << "err = iprot.ReadStructEnd()" << endl <<
    indent() << "if err != nil { return thrift.NewTProtocolExceptionReadStruct(p.ThriftName(), err); }" << endl <<
    indent() << "return err" << endl;
  indent_down();
  out <<
    indent() << "}" << endl << endl;
  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
    string field_type_name(publicize((*f_iter)->get_type()->get_name()));
    string field_name(publicize((*f_iter)->get_name()));
    int32_t field_id = (*f_iter)->get_key();
    out <<
      indent() << "func (p *" << tstruct_name << ") ReadField" << field_id << "(iprot thrift.TProtocol) (err thrift.TProtocolException) {" << endl;
    indent_up();
    generate_deserialize_field(out, *f_iter, false, "p.");
    indent_down();
    out <<
      indent() << "  return err" << endl <<
      indent() << "}" << endl << endl <<
      indent() << "func (p *" << tstruct_name << ") ReadField" << field_name << "(iprot thrift.TProtocol) (thrift.TProtocolException) {" << endl <<
      indent() << "  return p.ReadField" << field_id << "(iprot)" << endl <<
      indent() << "}" << endl << endl;
  }
}
void t_go_generator :: generate_go_struct_reader ( ofstream & out , t_struct * tstruct , const string & tstruct_name , bool is_result ) { ( void ) is_result ; const vector < t_field * > & fields = tstruct -> get_members ( ) ; vector < t_field * > :: const_iterator f_iter ; string escaped_tstruct_name ( escape_string ( tstruct -> get_name ( ) ) ) ; out << indent ( ) << " " << tstruct_name << " " << endl ; indent_up ( ) ; out << indent ( ) << " " << endl << indent ( ) << " " << endl ; indent ( out ) << " " << endl ; indent_up ( ) ; out << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " \" \" " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl ; out << indent ( ) << " " << endl ; bool first = true ; int32_t field_id = -1 ; for ( f_iter = fields . begin ( ) ; f_iter != fields . end ( ) ; ++ f_iter ) { field_id = ( * f_iter ) -> get_key ( ) ; if ( first ) { first = false ; indent ( out ) ; } else { indent ( out ) << " " ; } out << " " << field_id << " \" " << escape_string ( ( * f_iter ) -> get_name ( ) ) << " \" " << endl ; indent_up ( ) ; out << indent ( ) << " " << type_to_enum ( ( * f_iter ) -> get_type ( ) ) << " " << endl << indent ( ) << " " << field_id << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << field_id << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl ; indent_down ( ) ; } if ( first ) { out << indent ( ) << " " << endl << indent ( ) << " " << endl ; } else { out << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl ; } out << indent ( ) << " " << endl << indent ( ) << " " << endl ; indent_down ( ) ; out << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl << indent ( ) << " " << endl ; indent_down ( ) ; out << indent ( ) << " " << endl << endl ; for ( f_iter = fields . begin ( ) ; f_iter != fields . end ( ) ; ++ f_iter ) { string field_type_name ( publicize ( ( * f_iter ) -> get_type ( ) -> get_name ( ) ) ) ; string field_name ( publicize ( ( * f_iter ) -> get_name ( ) ) ) ; int32_t field_id = ( * f_iter ) -> get_key ( ) ; out << indent ( ) << " " << tstruct_name << " " << field_id << " " << endl ; indent_up ( ) ; generate_deserialize_field ( out , * f_iter , false , " " ) ; indent_down ( ) ; out << indent ( ) << " " << endl << indent ( ) << " " << endl << endl << indent ( ) << " " << tstruct_name << " " << field_name << " " << endl << indent ( ) << " " << field_id << " " << endl << indent ( ) << " " << endl << endl ; } }
Generates the read method for a struct
Generates the read method for a struct
// Loop over reading in fields // Read beginning field marker // Check for field STOP marker and break // Switch statement on the field we are reading // Generate deserialization code for known cases // In the default case we skip the field // Read field end marker