compiler/cpp/src/generate/t_go_generator.cc
/**
 * Deserializes a field of any type.
 */
Deserializes a field of any type.
Deserializes a field of any type .
void t_go_generator::generate_deserialize_field(ofstream &out,
                                                t_field* tfield,
                                                bool declare,
                                                string prefix,
                                                string err,
                                                bool inclass,
                                                bool coerceData) {
  (void) inclass;
  (void) coerceData;
  t_type* orig_type = tfield->get_type();
  t_type* type = get_true_type(orig_type);
  string name(prefix + publicize(variable_name_to_go_name(tfield->get_name())));
  if (type->is_void()) {
    throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + name;
  }
  string v = tmp("v");
  string err2 = tmp("err");
  if (type->is_struct() || type->is_xception()) {
    generate_deserialize_struct(out,
                                (t_struct*)type,
                                declare,
                                name,
                                err);
  } else if (type->is_container()) {
    generate_deserialize_container(out, type, declare, name, err);
  } else if (type->is_base_type() || type->is_enum()) {
    indent(out) <<
      v << ", " << err2 << " := iprot.";
    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 << "ReadString()";
        break;
      case t_base_type::TYPE_BOOL:
        out << "ReadBool()";
        break;
      case t_base_type::TYPE_BYTE:
        out << "ReadByte()";
        break;
      case t_base_type::TYPE_I16:
        out << "ReadI16()";
        break;
      case t_base_type::TYPE_I32:
        out << "ReadI32()";
        break;
      case t_base_type::TYPE_I64:
        out << "ReadI64()";
        break;
      case t_base_type::TYPE_DOUBLE:
        out << "ReadDouble()";
        break;
      default:
        throw "compiler error: no Go name for base type " + t_base_type::t_base_name(tbase);
      }
    } else if (type->is_enum()) {
      out << "ReadI32()";
    }
    string structName("\"\"");
    if(!prefix.size() || prefix.find(".") == string::npos) {
      structName = "\"\"";
    } else {
      structName = prefix + "ThriftName()";
    }
    out << endl <<
      indent() << "if " << err2 << " != nil { return thrift.NewTProtocolExceptionReadField(" <<
                           tfield->get_key()  <<
                           ", \"" << escape_string(tfield->get_name()) <<
                           "\", " << structName << ", " << err2 << "); }" << endl;
    if(!prefix.size() || prefix.find(".") == string::npos) {
      if(type->is_enum() || orig_type->is_typedef()) {
        indent(out) << name << " := " << publicize(orig_type->get_name()) << "("<< v << ")" << endl;
      } else {
        indent(out) << name << " := " << v << endl;
      }
    } else {
      if(type->is_enum() || orig_type->is_typedef()) {
        indent(out) << name << " = " << publicize(orig_type->get_name()) << "("<< v << ")" << endl;
      } else {
        indent(out) << name << " = " << v << endl;
      }
    }
  } else {
    throw "INVALID TYPE IN generate_deserialize_field '" + type->get_name() + "' for field '" + tfield->get_name() + "'";
  }
}
void t_go_generator :: generate_deserialize_field ( ofstream & out , t_field * tfield , bool declare , string prefix , string err , bool inclass , bool coerceData ) { ( void ) inclass ; ( void ) coerceData ; t_type * orig_type = tfield -> get_type ( ) ; t_type * type = get_true_type ( orig_type ) ; string name ( prefix + publicize ( variable_name_to_go_name ( tfield -> get_name ( ) ) ) ) ; if ( type -> is_void ( ) ) { throw " " + name ; } string v = tmp ( " " ) ; string err2 = tmp ( " " ) ; if ( type -> is_struct ( ) || type -> is_xception ( ) ) { generate_deserialize_struct ( out , ( t_struct * ) type , declare , name , err ) ; } else if ( type -> is_container ( ) ) { generate_deserialize_container ( out , type , declare , name , err ) ; } else if ( type -> is_base_type ( ) || type -> is_enum ( ) ) { indent ( out ) << v << " " << err2 << " " ; 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 << " " ; break ; case t_base_type :: TYPE_BOOL : out << " " ; break ; case t_base_type :: TYPE_BYTE : out << " " ; break ; case t_base_type :: TYPE_I16 : out << " " ; break ; case t_base_type :: TYPE_I32 : out << " " ; break ; case t_base_type :: TYPE_I64 : out << " " ; break ; case t_base_type :: TYPE_DOUBLE : out << " " ; break ; default : throw " " + t_base_type :: t_base_name ( tbase ) ; } } else if ( type -> is_enum ( ) ) { out << " " ; } string structName ( " \" \" " ) ; if ( ! prefix . size ( ) || prefix . find ( " " ) == string :: npos ) { structName = " \" \" " ; } else { structName = prefix + " " ; } out << endl << indent ( ) << " " << err2 << " " << tfield -> get_key ( ) << " \" " << escape_string ( tfield -> get_name ( ) ) << " \" " << structName << " " << err2 << " " << endl ; if ( ! prefix . size ( ) || prefix . find ( " " ) == string :: npos ) { if ( type -> is_enum ( ) || orig_type -> is_typedef ( ) ) { indent ( out ) << name << " " << publicize ( orig_type -> get_name ( ) ) << " " << v << " " << endl ; } else { indent ( out ) << name << " " << v << endl ; } } else { if ( type -> is_enum ( ) || orig_type -> is_typedef ( ) ) { indent ( out ) << name << " " << publicize ( orig_type -> get_name ( ) ) << " " << v << " " << endl ; } else { indent ( out ) << name << " " << v << endl ; } } } else { throw " " + type -> get_name ( ) + " " + tfield -> get_name ( ) + " " ; } }
Deserializes a field of any type.
Deserializes a field of any type .