@Override
public T read(TProtocol protocol)
throws Exception
{
TProtocolReader reader = new TProtocolReader(protocol);
reader.readStructBegin();
Map.Entry<Short, Object> data = null;
Short fieldId = null;
while (reader.nextField()) {
checkState(fieldId == null, "Received Union with more than one value (seen id %s, now id %s)", fieldId, reader.getFieldId());
fieldId = reader.getFieldId();
// do we have a codec for this field
ThriftCodec<?> codec = fields.get(fieldId);
if (codec == null) {
reader.skipFieldData();
}
else {
// is this field readable
ThriftFieldMetadata field = metadata.getField(fieldId);
if (field.isWriteOnly() || field.getType() != THRIFT_FIELD) {
reader.skipFieldData();
continue;
}
// read the value
Object value = reader.readField(codec);
if (value == null) {
continue;
}
data = Maps.immutableEntry(fieldId, value);
}
}
reader.readStructEnd();
// build the struct
return constructStruct(data);
}