use of org.apache.thrift.meta_data.FieldValueMetaData in project elephant-bird by twitter.
the class ThriftToProto method convert.
@SuppressWarnings("unchecked")
public P convert(T thriftObj) {
Descriptor protoDesc = protoObj_.getDescriptorForType();
Map<? extends org.apache.thrift.TFieldIdEnum, FieldMetaData> fieldMap = FieldMetaData.getStructMetaDataMap(thriftObj.getClass());
for (Map.Entry<? extends TFieldIdEnum, FieldMetaData> e : fieldMap.entrySet()) {
final TFieldIdEnum tFieldIdEnum = e.getKey();
final FieldValueMetaData thriftMetadata = e.getValue().valueMetaData;
FieldDescriptor protoFieldDesc = protoDesc.findFieldByName(tFieldIdEnum.getFieldName());
if (protoFieldDesc == null) {
throw new RuntimeException("Field " + tFieldIdEnum.getFieldName() + " not found in " + protoObj_.getClass().getCanonicalName());
} else if (!typesMatch(protoFieldDesc, thriftMetadata)) {
throw new RuntimeException("Field " + tFieldIdEnum.getFieldName() + " type does not match: " + "thrift " + thriftMetadata.type + "vs " + protoFieldDesc.getType());
}
Object fieldValue = thriftObj.getFieldValue(tFieldIdEnum);
if (protoFieldDesc.getType() == Type.BYTES) {
protoBuilder_.setField(protoFieldDesc, (ByteString.copyFrom((byte[]) fieldValue)));
} else {
protoBuilder_.setField(protoFieldDesc, fieldValue);
}
}
return (P) protoBuilder_.build();
}
use of org.apache.thrift.meta_data.FieldValueMetaData in project elephant-bird by twitter.
the class ThriftToProto method typesMatch.
private boolean typesMatch(FieldDescriptor protoFieldDesc, FieldValueMetaData thriftMetadata) {
if (thriftMetadata.isStruct()) {
// TODO: Handle structs
return false;
}
byte thriftType = thriftMetadata.type;
// TODO: Handle Lists that are more than 1 level deep. Be all pro-style with the recursion.
if (thriftMetadata.type == TType.LIST && protoFieldDesc.isRepeated()) {
FieldValueMetaData listMeta = ((ListMetaData) thriftMetadata).elemMetaData;
if (listMeta.isStruct() || listMeta.isContainer()) {
return false;
}
thriftType = listMeta.type;
}
return (protoFieldDesc.getType().equals(thriftTypeToProtoType(thriftType)) || thriftBinSucks(protoFieldDesc.getType(), thriftType));
}
use of org.apache.thrift.meta_data.FieldValueMetaData in project beam by apache.
the class ThriftSchema method beamType.
private <EnumT extends Enum<EnumT> & TEnum> FieldType beamType(FieldValueMetaData metadata) {
if (metadata.isTypedef()) {
final FieldType beamType = typedefs.get(metadata.getTypedefName());
if (beamType != null) {
return beamType;
}
}
switch(metadata.type) {
case TType.BOOL:
return FieldType.BOOLEAN;
case TType.BYTE:
return FieldType.BYTE;
case TType.I16:
return FieldType.INT16;
case TType.I32:
return FieldType.INT32;
case TType.I64:
return FieldType.INT64;
case TType.DOUBLE:
return FieldType.DOUBLE;
case TType.STRING:
return metadata.isBinary() ? FieldType.BYTES : FieldType.STRING;
case TType.SET:
final FieldValueMetaData setElemMetadata = ((SetMetaData) metadata).elemMetaData;
return FieldType.iterable(beamType(setElemMetadata));
case TType.LIST:
final FieldValueMetaData listElemMetadata = ((ListMetaData) metadata).elemMetaData;
return FieldType.array(beamType(listElemMetadata));
case TType.MAP:
final MapMetaData mapMetadata = ((MapMetaData) metadata);
return FieldType.map(beamType(mapMetadata.keyMetaData), beamType(mapMetadata.valueMetaData));
case TType.STRUCT:
final StructMetaData structMetadata = ((StructMetaData) metadata);
return FieldType.row(schemaFor(structMetadata.structClass));
case TType.ENUM:
@SuppressWarnings("unchecked") final Class<EnumT> enumClass = (Class<EnumT>) ((EnumMetaData) metadata).enumClass;
// it's either "nullness" or "unsafe", apparently
@SuppressWarnings("nullness") final EnumT @NonNull [] enumConstants = enumClass.getEnumConstants();
final String[] enumValues = Stream.of(enumConstants).map(EnumT::name).toArray(String[]::new);
return FieldType.logicalType(EnumerationType.create(enumValues));
default:
throw new IllegalArgumentException("Unsupported thrift type code: " + metadata.type);
}
}
Aggregations