Search in sources :

Example 1 with FieldValueMetaData

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();
}
Also used : FieldValueMetaData(org.apache.thrift.meta_data.FieldValueMetaData) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) FieldMetaData(org.apache.thrift.meta_data.FieldMetaData) TFieldIdEnum(org.apache.thrift.TFieldIdEnum) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) Map(java.util.Map)

Example 2 with FieldValueMetaData

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));
}
Also used : ListMetaData(org.apache.thrift.meta_data.ListMetaData) FieldValueMetaData(org.apache.thrift.meta_data.FieldValueMetaData)

Example 3 with FieldValueMetaData

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);
    }
}
Also used : SetMetaData(org.apache.thrift.meta_data.SetMetaData) MapMetaData(org.apache.thrift.meta_data.MapMetaData) ListMetaData(org.apache.thrift.meta_data.ListMetaData) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType) FieldValueMetaData(org.apache.thrift.meta_data.FieldValueMetaData) StructMetaData(org.apache.thrift.meta_data.StructMetaData) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Aggregations

FieldValueMetaData (org.apache.thrift.meta_data.FieldValueMetaData)3 ListMetaData (org.apache.thrift.meta_data.ListMetaData)2 Descriptor (com.google.protobuf.Descriptors.Descriptor)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 Map (java.util.Map)1 FieldType (org.apache.beam.sdk.schemas.Schema.FieldType)1 TFieldIdEnum (org.apache.thrift.TFieldIdEnum)1 FieldMetaData (org.apache.thrift.meta_data.FieldMetaData)1 MapMetaData (org.apache.thrift.meta_data.MapMetaData)1 SetMetaData (org.apache.thrift.meta_data.SetMetaData)1 StructMetaData (org.apache.thrift.meta_data.StructMetaData)1 NonNull (org.checkerframework.checker.nullness.qual.NonNull)1