compiler/cpp/src/generate/t_go_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_go_generator::generate_serialize_field(ofstream &out,
                                               t_field* tfield,
                                               string prefix,
                                               string err) {
  t_type* type = get_true_type(tfield->get_type());
  string name(prefix + publicize(variable_name_to_go_name(tfield->get_name())));
  if (type->is_void()) {
    throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + name;
  }
  if (type->is_struct() || type->is_xception()) {
    generate_serialize_struct(out,
                              (t_struct*)type,
                              name,
                              err);
  } else if (type->is_container()) {
    generate_serialize_container(out,
                                 type,
                                 name,
                                 err);
  } else if (type->is_base_type() || type->is_enum()) {
    indent(out) <<
      err << " = oprot.";
    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 << "WriteString(string(" << name << "))";
        break;
      case t_base_type::TYPE_BOOL:
        out << "WriteBool(bool(" << name << "))";
        break;
      case t_base_type::TYPE_BYTE:
        out << "WriteByte(byte(" << name << "))";
        break;
      case t_base_type::TYPE_I16:
        out << "WriteI16(int16(" << name << "))";
        break;
      case t_base_type::TYPE_I32:
        out << "WriteI32(int32(" << name << "))";
        break;
      case t_base_type::TYPE_I64:
        out << "WriteI64(int64(" << name << "))";
        break;
      case t_base_type::TYPE_DOUBLE:
        out << "WriteDouble(float64(" << name << "))";
        break;
      default:
        throw "compiler error: no PHP name for base type " + t_base_type::t_base_name(tbase);
      }
    } else if (type->is_enum()) {
      out << "WriteI32(int32(" << name << "))";
    }
    string structName = (prefix.size()) ? prefix + "ThriftName()" : "\"\"";
    out << endl <<
      indent() << "if " << err << " != nil { return thrift.NewTProtocolExceptionWriteField(" 
                        << tfield->get_key()
                        << ", \"" << escape_string(tfield->get_name())
                        << "\", " << structName << ", " << err << "); }\n";
  } else {
    throw "INVALID TYPE IN generate_serialize_field '" + type->get_name() + "' for field '" + name + "'";
  }
}
void t_go_generator :: generate_serialize_field ( ofstream & out , t_field * tfield , string prefix , string err ) { t_type * type = get_true_type ( tfield -> get_type ( ) ) ; string name ( prefix + publicize ( variable_name_to_go_name ( tfield -> get_name ( ) ) ) ) ; if ( type -> is_void ( ) ) { throw " " + name ; } if ( type -> is_struct ( ) || type -> is_xception ( ) ) { generate_serialize_struct ( out , ( t_struct * ) type , name , err ) ; } else if ( type -> is_container ( ) ) { generate_serialize_container ( out , type , name , err ) ; } else if ( type -> is_base_type ( ) || type -> is_enum ( ) ) { indent ( out ) << err << " " ; 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 << " " << 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 ( ) ) { out << " " << name << " " ; } string structName = ( prefix . size ( ) ) ? prefix + " " : " \" \" " ; out << endl << indent ( ) << " " << err << " " << tfield -> get_key ( ) << " \" " << escape_string ( tfield -> get_name ( ) ) << " \" " << structName << " " << err << " \n " ; } else { throw " " + type -> get_name ( ) + " " + name + " " ; } }
Serializes a field of any type.
Serializes a field of any type .
// Do nothing for void types