Search in sources :

Example 1 with RawType

use of com.datastax.oss.protocol.internal.response.result.RawType in project java-driver by datastax.

the class ComplexTypeSerializerUtil method toProtocolSpec.

private static RawType toProtocolSpec(DataType dataType) {
    int id = dataType.getProtocolCode();
    RawType type = RawType.PRIMITIVES.get(id);
    if (type != null) {
        return type;
    }
    switch(id) {
        case ProtocolConstants.DataType.CUSTOM:
            CustomType customType = ((CustomType) dataType);
            type = new RawType.RawCustom(customType.getClassName());
            break;
        case ProtocolConstants.DataType.LIST:
            ListType listType = ((ListType) dataType);
            type = new RawType.RawList(toProtocolSpec(listType.getElementType()));
            break;
        case ProtocolConstants.DataType.SET:
            SetType setType = ((SetType) dataType);
            type = new RawType.RawSet(toProtocolSpec(setType.getElementType()));
            break;
        case ProtocolConstants.DataType.MAP:
            MapType mapType = ((MapType) dataType);
            type = new RawType.RawMap(toProtocolSpec(mapType.getKeyType()), toProtocolSpec(mapType.getValueType()));
            break;
        case ProtocolConstants.DataType.TUPLE:
            TupleType tupleType = ((TupleType) dataType);
            ImmutableList.Builder<RawType> subTypesList = ImmutableList.builderWithExpectedSize(tupleType.getComponentTypes().size());
            for (int i = 0; i < tupleType.getComponentTypes().size(); i++) {
                subTypesList.add(toProtocolSpec(tupleType.getComponentTypes().get(i)));
            }
            type = new RawType.RawTuple(subTypesList.build());
            break;
        case ProtocolConstants.DataType.UDT:
            UserDefinedType userDefinedType = ((UserDefinedType) dataType);
            ImmutableMap.Builder<String, RawType> subTypesMap = ImmutableMap.builderWithExpectedSize(userDefinedType.getFieldNames().size());
            for (int i = 0; i < userDefinedType.getFieldTypes().size(); i++) {
                subTypesMap.put(userDefinedType.getFieldNames().get(i).asInternal(), toProtocolSpec(userDefinedType.getFieldTypes().get(i)));
            }
            type = new RawType.RawUdt(Objects.requireNonNull(userDefinedType.getKeyspace()).asInternal(), userDefinedType.getName().asInternal(), subTypesMap.build());
            break;
        default:
            throw new IllegalArgumentException("Unsupported type: " + dataType.asCql(true, true));
    }
    return type;
}
Also used : CustomType(com.datastax.oss.driver.api.core.type.CustomType) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) MapType(com.datastax.oss.driver.api.core.type.MapType) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) SetType(com.datastax.oss.driver.api.core.type.SetType) ListType(com.datastax.oss.driver.api.core.type.ListType) TupleType(com.datastax.oss.driver.api.core.type.TupleType) RawType(com.datastax.oss.protocol.internal.response.result.RawType)

Example 2 with RawType

use of com.datastax.oss.protocol.internal.response.result.RawType in project java-driver by datastax.

the class ComplexTypeSerializerUtil method encodeTypeDefinition.

static void encodeTypeDefinition(DataType type, Buffer buffer, DefaultDriverContext driverContext) {
    RawType protocolType = toProtocolSpec(type);
    protocolType.encode(buffer, codec, driverContext.getProtocolVersion().getCode());
}
Also used : RawType(com.datastax.oss.protocol.internal.response.result.RawType)

Example 3 with RawType

use of com.datastax.oss.protocol.internal.response.result.RawType in project java-driver by datastax.

the class DataTypeHelper method fromProtocolSpec.

public static DataType fromProtocolSpec(RawType rawType, AttachmentPoint attachmentPoint) {
    DataType type = PRIMITIVE_TYPES_BY_CODE.get(rawType.id);
    if (type != null) {
        return type;
    } else {
        switch(rawType.id) {
            case ProtocolConstants.DataType.CUSTOM:
                RawType.RawCustom rawCustom = (RawType.RawCustom) rawType;
                return DataTypes.custom(rawCustom.className);
            case ProtocolConstants.DataType.LIST:
                RawType.RawList rawList = (RawType.RawList) rawType;
                return DataTypes.listOf(fromProtocolSpec(rawList.elementType, attachmentPoint));
            case ProtocolConstants.DataType.SET:
                RawType.RawSet rawSet = (RawType.RawSet) rawType;
                return DataTypes.setOf(fromProtocolSpec(rawSet.elementType, attachmentPoint));
            case ProtocolConstants.DataType.MAP:
                RawType.RawMap rawMap = (RawType.RawMap) rawType;
                return DataTypes.mapOf(fromProtocolSpec(rawMap.keyType, attachmentPoint), fromProtocolSpec(rawMap.valueType, attachmentPoint));
            case ProtocolConstants.DataType.TUPLE:
                RawType.RawTuple rawTuple = (RawType.RawTuple) rawType;
                List<RawType> rawFieldsList = rawTuple.fieldTypes;
                ImmutableList.Builder<DataType> fields = ImmutableList.builder();
                for (RawType rawField : rawFieldsList) {
                    fields.add(fromProtocolSpec(rawField, attachmentPoint));
                }
                return new DefaultTupleType(fields.build(), attachmentPoint);
            case ProtocolConstants.DataType.UDT:
                RawType.RawUdt rawUdt = (RawType.RawUdt) rawType;
                ImmutableList.Builder<CqlIdentifier> fieldNames = ImmutableList.builder();
                ImmutableList.Builder<DataType> fieldTypes = ImmutableList.builder();
                for (Map.Entry<String, RawType> entry : rawUdt.fields.entrySet()) {
                    fieldNames.add(CqlIdentifier.fromInternal(entry.getKey()));
                    fieldTypes.add(fromProtocolSpec(entry.getValue(), attachmentPoint));
                }
                return new DefaultUserDefinedType(CqlIdentifier.fromInternal(rawUdt.keyspace), CqlIdentifier.fromInternal(rawUdt.typeName), false, fieldNames.build(), fieldTypes.build(), attachmentPoint);
            default:
                throw new IllegalArgumentException("Unsupported type: " + rawType.id);
        }
    }
}
Also used : ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) DataType(com.datastax.oss.driver.api.core.type.DataType) RawType(com.datastax.oss.protocol.internal.response.result.RawType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Map(java.util.Map) IntMap(com.datastax.oss.protocol.internal.util.IntMap)

Aggregations

RawType (com.datastax.oss.protocol.internal.response.result.RawType)3 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)2 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)1 CustomType (com.datastax.oss.driver.api.core.type.CustomType)1 DataType (com.datastax.oss.driver.api.core.type.DataType)1 ListType (com.datastax.oss.driver.api.core.type.ListType)1 MapType (com.datastax.oss.driver.api.core.type.MapType)1 SetType (com.datastax.oss.driver.api.core.type.SetType)1 TupleType (com.datastax.oss.driver.api.core.type.TupleType)1 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)1 ImmutableMap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)1 IntMap (com.datastax.oss.protocol.internal.util.IntMap)1 Map (java.util.Map)1