Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Thrift, Java: Object Data Types

Tags:

java

types

thrift

i'm stuck with Thrift about data types.

Now when i map and Integer value to a thrift generated bean, i'm using i32 type in the idl definition.

class MyBean {
  Integer i = null;
}

struct TMyBean {
  1: i32 i;
}

The problem is that in TMyBean generated bean, the i var is an int primitive type, than it's putting 0 as the default value, and for me 0 it's a valid value.

I've tried to put the optional keyword in the idl file, but things are not changing, it's always int.

How i've to handle this situation? i need i to accept a null value in TMyBean i var.

Thanks, Phaedra..

like image 532
Seby Avatar asked Feb 11 '26 03:02

Seby


1 Answers

The optional keyword was the right choice.

To test, whether or not a particular optional field is set or not, use the isset flags:

struct MyBean {
  1: i32 IntValue
}

gives

public class MyBean implements org.apache.thrift.TBase<MyBean, MyBean._Fields>, java.io.Serializable, Cloneable, Comparable<MyBean> {

  // ... lots of other code omitted ...

  // isset id assignments
  private static final int __INTVALUE_ISSET_ID = 0;
  private byte __isset_bitfield = 0;

  // ... lots of other code omitted ...

  /** Returns true if field IntValue is set (has been assigned a value) and false otherwise */
  public boolean isSetIntValue() {
    return EncodingUtils.testBit(__isset_bitfield, __INTVALUE_ISSET_ID);
  }

  public void setIntValueIsSet(boolean value) {
    __isset_bitfield = EncodingUtils.setBit(__isset_bitfield, __INTVALUE_ISSET_ID, value);
  }

  // ... even more code omitted ...

}
like image 157
JensG Avatar answered Feb 12 '26 22:02

JensG