Search in sources :

Example 1 with ListMetaData

use of org.apache.thrift.meta_data.ListMetaData 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 2 with ListMetaData

use of org.apache.thrift.meta_data.ListMetaData 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)2 ListMetaData (org.apache.thrift.meta_data.ListMetaData)2 FieldType (org.apache.beam.sdk.schemas.Schema.FieldType)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