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;
}
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());
}
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);
}
}
}
Aggregations