You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@thrift.apache.org by "Lehmann, Thomas" <th...@adtech.com> on 2015/01/20 09:01:35 UTC

union with thrift

Hi,

take a definition like here (using thrift 0.9.0):

enum parameter_type {
    STRING = 0,
    INTEGER = 1
}

union request_parameter_value {
    1: string string_value,
    2: i64 integer_value
}

struct request_parameter {
    1: parameter_type type = 0,
    2: request_parameter_value value
}

I'm wondering ... maybe I misunderstand the generated code ...
It look like that ALL values inside the union are written;
In C/C++ I expected that one of these value is written only (union).

Right?

uint32_t request_parameter_value::write(::apache::thrift::protocol::TProtocol* oprot) const {
  uint32_t xfer = 0;
  xfer += oprot->writeStructBegin("request_parameter_value");

  xfer += oprot->writeFieldBegin("string_value", ::apache::thrift::protocol::T_STRING, 1);
  xfer += oprot->writeString(this->string_value);
  xfer += oprot->writeFieldEnd();

  xfer += oprot->writeFieldBegin("integer_value", ::apache::thrift::protocol::T_I64, 2);
  xfer += oprot->writeI64(this->integer_value);
  xfer += oprot->writeFieldEnd();

  xfer += oprot->writeFieldStop();
  xfer += oprot->writeStructEnd();
  return xfer;
}

Regards,
Thomas