use of com.twitter.elephantbird.thrift.TStructDescriptor.Field in project elephant-bird by twitter.
the class TestThriftToPig method thriftToPig.
static <M extends TBase<?, ?>> Tuple thriftToPig(M obj) throws TException {
// it is very inefficient to create one ThriftToPig for each Thrift object,
// but good enough for unit testing.
TypeRef<M> typeRef = new TypeRef<M>(obj.getClass()) {
};
ThriftToPig<M> thriftToPig = ThriftToPig.newInstance(typeRef);
Tuple t = thriftToPig.getPigTuple(obj);
// test projected tuple. project a subset of fields based on field name.
List<Field> tFields = thriftToPig.getTStructDescriptor().getFields();
List<Integer> idxList = Lists.newArrayList();
RequiredFieldList reqFieldList = new RequiredFieldList();
for (int i = 0; i < tFields.size(); i++) {
String name = tFields.get(i).getName();
if (name.hashCode() % 2 == 0) {
RequiredField rf = new RequiredField();
rf.setAlias(name);
rf.setIndex(i);
reqFieldList.add(rf);
idxList.add(i);
}
}
try {
Tuple pt = new ProjectedThriftTupleFactory<M>(typeRef, reqFieldList).newTuple(obj);
int pidx = 0;
for (int idx : idxList) {
if (t.get(idx) != pt.get(pidx)) {
// if both are not nulls
assertEquals(t.get(idx).toString(), pt.get(pidx).toString());
}
pidx++;
}
} catch (ExecException e) {
// not expected
throw new TException(e);
}
// return the full tuple
return t;
}
use of com.twitter.elephantbird.thrift.TStructDescriptor.Field in project elephant-bird by twitter.
the class ThriftToPig method toTuple.
@SuppressWarnings("rawtypes")
private static <T extends TBase> Tuple toTuple(TStructDescriptor tDesc, T tObj) {
int size = tDesc.getFields().size();
Tuple tuple = tupleFactory.newTuple(size);
for (int i = 0; i < size; i++) {
Field field = tDesc.getFieldAt(i);
Object value = tDesc.getFieldValue(i, tObj);
try {
tuple.set(i, toPigObject(field, value, false));
} catch (ExecException e) {
// not expected
throw new RuntimeException(e);
}
}
return tuple;
}
use of com.twitter.elephantbird.thrift.TStructDescriptor.Field in project elephant-bird by twitter.
the class ThriftUtils method writeFieldNoTag.
/**
* Serializes a single field of a thrift struct.
*
* @throws TException
*/
public static void writeFieldNoTag(TProtocol proto, Field field, Object value) throws TException {
if (value == null) {
return;
}
Field innerField = null;
switch(field.getType()) {
case TType.LIST:
innerField = field.getListElemField();
break;
case TType.SET:
innerField = field.getSetElemField();
break;
case TType.MAP:
innerField = field.getMapKeyField();
break;
default:
writeSingleFieldNoTag(proto, field, value);
return;
}
if (field.getType() == TType.MAP) {
Field valueField = field.getMapValueField();
Map<?, ?> map = (Map<?, ?>) value;
proto.writeByte(innerField.getType());
proto.writeByte(valueField.getType());
proto.writeI32(map.size());
for (Entry<?, ?> entry : map.entrySet()) {
writeSingleFieldNoTag(proto, innerField, entry.getKey());
writeSingleFieldNoTag(proto, valueField, entry.getValue());
}
} else {
// SET or LIST
Collection<?> coll = (Collection<?>) value;
proto.writeByte(innerField.getType());
proto.writeI32(coll.size());
for (Object v : coll) {
writeSingleFieldNoTag(proto, innerField, v);
}
}
}
use of com.twitter.elephantbird.thrift.TStructDescriptor.Field in project elephant-bird by twitter.
the class ThriftToDynamicProto method mapDescriptorProtoBuilder.
/**
* Generate a DescriptorProto.Builder for the Message type that will be used
* to represent the entries of the input Map field.
*
* @param field a Map Field (field.isMap() == true)
* @param typeName name of new message type
*/
private DescriptorProtos.DescriptorProto.Builder mapDescriptorProtoBuilder(Field field, String typeName) throws DescriptorValidationException {
DescriptorProtos.DescriptorProto.Builder mapBuilder = DescriptorProtos.DescriptorProto.newBuilder().setName(typeName);
Field keyField = field.getMapKeyField();
Field valueField = field.getMapValueField();
DescriptorProtos.FieldDescriptorProto.Builder keyBuilder = mapKeyProtoBuilder();
DescriptorProtos.FieldDescriptorProto.Builder valueBuilder = mapValueProtoBuilder();
setBuilderTypeFromField(keyField, keyBuilder);
setBuilderTypeFromField(valueField, valueBuilder);
mapBuilder.addField(keyBuilder.build());
mapBuilder.addField(valueBuilder.build());
return mapBuilder;
}
Aggregations