2014-01-30 25 views
0

我目前使用的是大象 - 鳥 - 豬圖書館的4.4版本。如果我試圖從一個節儉對象中創建一個元組,我希望未在該對象中設置的字段在元組中被標記爲空。然而,默認值被放入元組中。例如。當沒有設置可選字段時,elephant-bird庫會生成字段的默認值而不是null

struct PropValueUnion { 
    1: optional i32 intValue, 
    2: optional i64 longValue, 
    3: optional string stringValue, 
    4: optional double doubleValue, 
    5: optional bool flag 
} 

以下的輸出應爲(NULL,NULL,ABC,NULL,NULL)

PropValueUnion value = new PropValueUnion(); 
a.setStringValue("abc"); 
System.out.println(ThriftToPig.newInstance(PropvalueUnion.class).getPigTuple(value)); 

實際:(0,0,ABC,0.0,0)

問題在轉換爲元組期間,字段的isset信息正在丟失。是否故意完成,有沒有解決這個問題的方法?

回答

0

我期望未在對象中設置的字段在元組中標記爲null。然而,默認值被放入元組中。 E

沒錯。可以通過檢查isset標誌來檢測是否設置了可選字段。

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

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

    // isset id assignments 
    private static final int __INTVALUE_ISSET_ID = 0; 
    private static final int __LONGVALUE_ISSET_ID = 1; 
    private static final int __DOUBLEVALUE_ISSET_ID = 2; 
    private static final int __FLAG_ISSET_ID = 3; 
    private byte __isset_bitfield = 0; 

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

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

    public void setDoubleValueIsSet(boolean value) { 
    __isset_bitfield = EncodingUtils.setBit(__isset_bitfield, __DOUBLEVALUE_ISSET_ID, value); 
    } 

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

} 
+0

elephant-bird是一個JAVA庫。這個問題與該庫有關,以及圖書館如何從豬元組轉換爲節儉對象,反之亦然,而不是一般的節儉語義。 – hobgoblin

+0

我已經給了你Java的一部分。我現在刪除了C#部分,以免讓你感到困惑。你的問題是關於Thrift'struct's中'optional'字段的行爲。 – JensG

相關問題