compiler/cpp/src/generate/t_hs_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_hs_generator::generate_hs_struct_reader(ofstream& out, t_struct* tstruct) {
  const vector<t_field*>& fields = tstruct->get_members();
  vector<t_field*>::const_iterator f_iter;
  string sname = type_name(tstruct);
  string str = tmp("_str");
  string t = tmp("_t");
  string id = tmp("_id");
  indent(out) << "read_" << sname << "_fields iprot record = do" << endl;
  indent_up();
  indent(out) << "(_," << t << "," << id << ") <- readFieldBegin iprot" << endl;
  indent(out) << "if " << t << " == T_STOP then return record else" << endl;
  indent_up();
  indent(out) << "case " << id << " of " << endl;
  indent_up();
  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
    int32_t key = (*f_iter)->get_key();
    string etype = type_to_enum((*f_iter)->get_type());
    indent(out) << key << " -> " << "if " << t << " == " << etype << " then do" << endl;
    indent_up();
    indent(out) << "s <- ";
    generate_deserialize_field(out, *f_iter,str);
    out << endl;
    string fname = decapitalize((*f_iter)->get_name());
    indent(out) << "read_" << sname << "_fields iprot record{f_" << sname << "_" << fname << "=Just s}" << endl;
    indent(out) << "else do" << endl;
    indent_up();
    indent(out) << "skip iprot " << t << endl;
    indent(out) << "read_" << sname << "_fields iprot record" << endl;
    indent_down();
    indent_down();
  }
  indent(out) << "_ -> do" << endl;
  indent_up();
  indent(out) << "skip iprot " << t << endl;
  indent(out) << "readFieldEnd iprot" << endl;
  indent(out) << "read_" << sname << "_fields iprot record" << endl;
  indent_down();
  indent_down();
  indent_down();
  indent_down();
  indent(out) << "read_" << sname << " iprot = do" << endl;
  indent_up();
  indent(out) << "_ <- readStructBegin iprot" << endl;
  indent(out) << "record <- read_" << sname << "_fields iprot (" << sname << "{";
  bool first = true;
  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
    out << (first ? "" : ",");
    out << "f_" << sname << "_" << decapitalize((*f_iter)->get_name()) << "=Nothing";
    first = false;
  }
  out << "})" << endl;
  indent(out) << "readStructEnd iprot" << endl;
  indent(out) << "return record" << endl;
  indent_down();
}
void t_hs_generator :: generate_hs_struct_reader ( ofstream & out , t_struct * tstruct ) { const vector < t_field * > & fields = tstruct -> get_members ( ) ; vector < t_field * > :: const_iterator f_iter ; string sname = type_name ( tstruct ) ; string str = tmp ( " " ) ; string t = tmp ( " " ) ; string id = tmp ( " " ) ; indent ( out ) << " " << sname << " " << endl ; indent_up ( ) ; indent ( out ) << " " << t << " " << id << " " << endl ; indent ( out ) << " " << t << " " << endl ; indent_up ( ) ; indent ( out ) << " " << id << " " << endl ; indent_up ( ) ; for ( f_iter = fields . begin ( ) ; f_iter != fields . end ( ) ; ++ f_iter ) { int32_t key = ( * f_iter ) -> get_key ( ) ; string etype = type_to_enum ( ( * f_iter ) -> get_type ( ) ) ; indent ( out ) << key << " " << " " << t << " " << etype << " " << endl ; indent_up ( ) ; indent ( out ) << " " ; generate_deserialize_field ( out , * f_iter , str ) ; out << endl ; string fname = decapitalize ( ( * f_iter ) -> get_name ( ) ) ; indent ( out ) << " " << sname << " " << sname << " " << fname << " " << endl ; indent ( out ) << " " << endl ; indent_up ( ) ; indent ( out ) << " " << t << endl ; indent ( out ) << " " << sname << " " << endl ; indent_down ( ) ; indent_down ( ) ; } indent ( out ) << " " << endl ; indent_up ( ) ; indent ( out ) << " " << t << endl ; indent ( out ) << " " << endl ; indent ( out ) << " " << sname << " " << endl ; indent_down ( ) ; indent_down ( ) ; indent_down ( ) ; indent_down ( ) ; indent ( out ) << " " << sname << " " << endl ; indent_up ( ) ; indent ( out ) << " " << endl ; indent ( out ) << " " << sname << " " << sname << " " ; bool first = true ; for ( f_iter = fields . begin ( ) ; f_iter != fields . end ( ) ; ++ f_iter ) { out << ( first ? " " : " " ) ; out << " " << sname << " " << decapitalize ( ( * f_iter ) -> get_name ( ) ) << " " ; first = false ; } out << " " << endl ; indent ( out ) << " " << endl ; indent ( out ) << " " << endl ; indent_down ( ) ; }
Generates the read method for a struct
Generates the read method for a struct
// Read beginning field marker // Check for field STOP marker and break // Generate deserialization code for known cases // In the default case we skip the field // read