use of com.twitter.elephantbird.thrift.TStructDescriptor.Field in project elephant-bird by twitter.
the class ThriftToDynamicProto method doSchemaMapping.
private int doSchemaMapping(DescriptorProtos.DescriptorProto.Builder desBuilder, TStructDescriptor fieldDesc) throws DescriptorValidationException {
int maxThriftId = 0;
for (Field tField : fieldDesc.getFields()) {
maxThriftId = Math.max(tField.getFieldId(), maxThriftId);
if (supportNestedObjects && tField.isMap()) {
String typeName = mapProtoMessageType(fieldDesc, tField);
if (descriptorBuilderMap.get(typeName) == null) {
DescriptorProtos.DescriptorProto.Builder mapBuilder = mapDescriptorProtoBuilder(tField, typeName);
descriptorBuilderMap.put(typeName, mapBuilder);
addProtoField(desBuilder, tField.getName(), tField.getFieldId() + 1, typeName, true);
}
} else {
Field field = resolveField(tField);
Type protoType = thriftTypeToProtoType(field);
boolean isContainer = isContainer(tField);
if (supportNestedObjects && protoType == Type.TYPE_MESSAGE) {
String typeName = resolveMessageTypeName(field.gettStructDescriptor());
// Protobuf field ids start at 1. Thrift starts at 0.
addProtoField(desBuilder, tField.getName(), tField.getFieldId() + 1, typeName, isContainer);
} else if (protoType != null) {
if (supportNestedObjects || (!supportNestedObjects && !hasNestedObject(tField))) {
addProtoField(desBuilder, tField.getName(), tField.getFieldId() + 1, protoType, isContainer);
}
}
}
}
return maxThriftId;
}
use of com.twitter.elephantbird.thrift.TStructDescriptor.Field 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();
}
use of com.twitter.elephantbird.thrift.TStructDescriptor.Field in project elephant-bird by twitter.
the class ThriftUtils method readFieldNoTag.
/**
* Deserializes a thrift field that was serilized with
* {@link #writeFieldNoTag(TProtocol, Field, Object)}.
*
* @throws TException in case of any Thrift errors.
*/
public static Object readFieldNoTag(TProtocol proto, Field field) throws TException {
Collection<Object> coll = null;
Field innerField = null;
switch(field.getType()) {
case TType.LIST:
innerField = field.getListElemField();
coll = Lists.newArrayList();
break;
case TType.SET:
innerField = field.getSetElemField();
coll = Sets.newHashSet();
break;
case TType.MAP:
innerField = field.getMapKeyField();
break;
default:
return readSingleFieldNoTag(proto, field);
}
if (field.getType() == TType.MAP) {
proto.readByte();
proto.readByte();
int nEntries = proto.readI32();
Map<Object, Object> map = Maps.newHashMap();
Field valueField = field.getMapValueField();
for (int i = 0; i < nEntries; i++) {
map.put(readFieldNoTag(proto, innerField), readFieldNoTag(proto, valueField));
}
return map;
} else {
// SET or LIST
proto.readByte();
int nEntries = proto.readI32();
for (int i = 0; i < nEntries; i++) {
coll.add(readFieldNoTag(proto, innerField));
}
return coll;
}
}
use of com.twitter.elephantbird.thrift.TStructDescriptor.Field in project elephant-bird by twitter.
the class PigToThrift method toThrift.
/**
* Construct a Thrift object from the tuple.
*/
@SuppressWarnings("unchecked")
private static TBase<?, ?> toThrift(TStructDescriptor tDesc, Tuple tuple) {
int size = tDesc.getFields().size();
int tupleSize = tuple.size();
@SuppressWarnings("rawtypes") TBase tObj = newTInstance(tDesc.getThriftClass());
for (int i = 0; i < size && i < tupleSize; i++) {
Object pObj;
try {
pObj = tuple.get(i);
} catch (ExecException e) {
throw new RuntimeException(e);
}
if (pObj != null) {
Field field = tDesc.getFieldAt(i);
try {
tObj.setFieldValue(field.getFieldIdEnum(), toThriftValue(field, pObj));
} catch (Exception e) {
String value = String.valueOf(tObj);
final int max_length = 100;
if (max_length < value.length()) {
value = value.substring(0, max_length - 3) + "...";
}
String type = tObj == null ? "unknown" : tObj.getClass().getName();
throw new RuntimeException(String.format("Failed to set field '%s' using tuple value '%s' of type '%s' at index %d", field.getName(), value, type, i), e);
}
}
// if tDesc is a union, at least one field needs to be non-null.
// user is responsible for ensuring that.
}
return tObj;
}
use of com.twitter.elephantbird.thrift.TStructDescriptor.Field in project elephant-bird by twitter.
the class ProjectedThriftTupleFactory method newTuple.
public Tuple newTuple(T tObject) throws ExecException {
int size = requiredFields.length;
List<Field> tFields = tStructDesc.getFields();
Tuple tuple = tf.newTuple(size);
for (int i = 0; i < size; i++) {
int idx = requiredFields[i];
Object value = tStructDesc.getFieldValue(idx, tObject);
tuple.set(i, ThriftToPig.toPigObject(tFields.get(idx), value, true));
}
return tuple;
}
Aggregations