compiler/cpp/src/generate/t_hs_generator.cc
/**
 * Serializes a field of any type.
 *
 * @param tfield The field to serialize
 * @param prefix Name to prepend to field name
 */
Serializes a field of any type.
@param tfield The field to serialize
@param prefix Name to prepend to field name
Serializes a field of any type . @param tfield The field to serialize @param prefix Name to prepend to field name
void t_hs_generator::generate_serialize_field(ofstream &out,
                                              t_field* tfield,
                                              string name) {
  t_type* type = get_true_type(tfield->get_type());
  if (type->is_void())
    throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + tfield->get_name();
  if (name.length() == 0)
    name = decapitalize(tfield->get_name());
  if (type->is_struct() || type->is_xception()) {
    generate_serialize_struct(out, (t_struct*)type, name);
  } else if (type->is_container()) {
    generate_serialize_container(out, type, name);
  } else if (type->is_base_type() || type->is_enum()) {
    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 << (((t_base_type*)type)->is_binary() ? "writeBinary" : "writeString") << " oprot " << name;
        break;
      case t_base_type::TYPE_BOOL:
        out << "writeBool oprot " << name;
       break;
      case t_base_type::TYPE_BYTE:
        out << "writeByte oprot " << name;
        break;
      case t_base_type::TYPE_I16:
        out << "writeI16 oprot " << name;
        break;
      case t_base_type::TYPE_I32:
        out << "writeI32 oprot " << name;
        break;
      case t_base_type::TYPE_I64:
        out << "writeI64 oprot " << name;
        break;
      case t_base_type::TYPE_DOUBLE:
        out << "writeDouble oprot " << name;
        break;
      default:
        throw "compiler error: no hs name for base type " + t_base_type::t_base_name(tbase);
      }
    } else if (type->is_enum()) {
      string ename = capitalize(type->get_name());
      out << "writeI32 oprot (fromIntegral $ fromEnum " << name << ")";
    }
  } else {
    printf("DO NOT KNOW HOW TO SERIALIZE FIELD '%s' TYPE '%s'\n",
           tfield->get_name().c_str(),
           type->get_name().c_str());
  }
}
void t_hs_generator :: generate_serialize_field ( ofstream & out , t_field * tfield , string name ) { t_type * type = get_true_type ( tfield -> get_type ( ) ) ; if ( type -> is_void ( ) ) throw " " + tfield -> get_name ( ) ; if ( name . length ( ) == 0 ) name = decapitalize ( tfield -> get_name ( ) ) ; if ( type -> is_struct ( ) || type -> is_xception ( ) ) { generate_serialize_struct ( out , ( t_struct * ) type , name ) ; } else if ( type -> is_container ( ) ) { generate_serialize_container ( out , type , name ) ; } else if ( type -> is_base_type ( ) || type -> is_enum ( ) ) { 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 " " + name ; break ; case t_base_type :: TYPE_STRING : out << ( ( ( t_base_type * ) type ) -> is_binary ( ) ? " " : " " ) << " " << name ; break ; case t_base_type :: TYPE_BOOL : out << " " << name ; break ; case t_base_type :: TYPE_BYTE : out << " " << name ; break ; case t_base_type :: TYPE_I16 : out << " " << name ; break ; case t_base_type :: TYPE_I32 : out << " " << name ; break ; case t_base_type :: TYPE_I64 : out << " " << name ; break ; case t_base_type :: TYPE_DOUBLE : out << " " << name ; break ; default : throw " " + t_base_type :: t_base_name ( tbase ) ; } } else if ( type -> is_enum ( ) ) { string ename = capitalize ( type -> get_name ( ) ) ; out << " " << name << " " ; } } else { printf ( " \n " , tfield -> get_name ( ) . c_str ( ) , type -> get_name ( ) . c_str ( ) ) ; } }
Serializes a field of any type.
Serializes a field of any type .
// Do nothing for void types