Search in sources :

Example 1 with TStructDescriptor

use of com.twitter.elephantbird.thrift.TStructDescriptor in project elephant-bird by twitter.

the class ThriftToDynamicProto method doConvert.

/**
 * conver TBase object to Message object
 * @param thriftObj
 */
@SuppressWarnings("unchecked")
public <F extends TFieldIdEnum> Message doConvert(TBase<?, F> thriftObj) {
    if (thriftObj == null) {
        return null;
    }
    Class<TBase<?, F>> clazz = (Class<TBase<?, F>>) thriftObj.getClass();
    checkState(clazz);
    Message.Builder builder = getBuilder(clazz);
    TStructDescriptor fieldDesc = TStructDescriptor.getInstance(clazz);
    int fieldId = 0;
    for (Field tField : fieldDesc.getFields()) {
        // don't want to carry over default values from unset fields
        if (!thriftObj.isSet((F) tField.getFieldIdEnum()) || (!supportNestedObjects && hasNestedObject(tField))) {
            fieldId++;
            continue;
        }
        // recurse into the object if it's a struct, otherwise just add the field
        if (supportNestedObjects && tField.getType() == TType.STRUCT) {
            TBase<?, ?> fieldValue = (TBase<?, ?>) fieldDesc.getFieldValue(fieldId++, thriftObj);
            Message message = doConvert(fieldValue);
            if (message != null) {
                FieldDescriptor protoFieldDesc = builder.getDescriptorForType().findFieldByName(tField.getName());
                builder.setField(protoFieldDesc, message);
            }
        } else {
            fieldId = convertField(thriftObj, builder, fieldDesc, fieldId, tField);
        }
    }
    return builder.build();
}
Also used : Field(com.twitter.elephantbird.thrift.TStructDescriptor.Field) DynamicMessage(com.google.protobuf.DynamicMessage) Message(com.google.protobuf.Message) TBase(org.apache.thrift.TBase) TStructDescriptor(com.twitter.elephantbird.thrift.TStructDescriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Aggregations

FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 DynamicMessage (com.google.protobuf.DynamicMessage)1 Message (com.google.protobuf.Message)1 TStructDescriptor (com.twitter.elephantbird.thrift.TStructDescriptor)1 Field (com.twitter.elephantbird.thrift.TStructDescriptor.Field)1 TBase (org.apache.thrift.TBase)1