Yeah, we never got around to implementing optional for the normal
Java generated code.  If you submit a patch for it, it would probably
be accepted.

--David

Bryan Duxbury wrote:
> This is not a "bug" per se. It's a known fact that it does this.
> 
> For anything beyond trivial uses, I think you want to use the beans- 
> style generator. I would personally go so far as to say that we  
> shouldn't even have the default generator, but that's just me.
> 
> -Bryan
> 
> On Jul 1, 2009, at 5:15 PM, Chengdu Huang wrote:
> 
>> I have a simple thrift file:
>>
>> struct UserDataContent {
>>     1: i32 id,
>>     2: optional i32 value,
>> }
>>
>> Java file generated by "thrift --gen java " doesn't seem to respect
>> the "optional" keyword:
>>
>>   public void write(TProtocol oprot) throws TException {
>>     validate();
>>
>>     oprot.writeStructBegin(STRUCT_DESC);
>>     oprot.writeFieldBegin(ID_FIELD_DESC);
>>     oprot.writeI32(this.id);
>>     oprot.writeFieldEnd();
>>     oprot.writeFieldBegin(VALUE_FIELD_DESC);
>>     oprot.writeI32(this.value);
>>     oprot.writeFieldEnd();
>>     oprot.writeFieldStop();
>>     oprot.writeStructEnd();
>>   }
>>
>> However, java file generated using "thrift --gen java:beans" looks
>> correct to me:
>>  public void write(TProtocol oprot) throws TException {
>>     validate();
>>
>>     oprot.writeStructBegin(STRUCT_DESC);
>>     oprot.writeFieldBegin(ID_FIELD_DESC);
>>     oprot.writeI32(this.id);
>>     oprot.writeFieldEnd();
>>     if (isSetValue()) {
>>       oprot.writeFieldBegin(VALUE_FIELD_DESC);
>>       oprot.writeI32(this.value);
>>       oprot.writeFieldEnd();
>>     }
>>     oprot.writeFieldStop();
>>     oprot.writeStructEnd();
>>   }
>>
>> My question is that why "normal" java style files should be different
>> from "beans" style files in terms of handling optional fields?
>>
>> The change is introduced in rev665308 (t_java_generator.cc:1105).
>>
>> Thanks,
>> Chengdu
> 

Reply via email to