use of com.datastax.oss.driver.api.core.type.TupleType in project java-driver by datastax.
the class DataTypeCqlNameParserTest method should_parse_tuple.
@Test
public void should_parse_tuple() {
TupleType tupleType = (TupleType) parse("tuple<int,text,float>");
assertThat(tupleType.getComponentTypes().size()).isEqualTo(3);
assertThat(tupleType.getComponentTypes().get(0)).isEqualTo(DataTypes.INT);
assertThat(tupleType.getComponentTypes().get(1)).isEqualTo(DataTypes.TEXT);
assertThat(tupleType.getComponentTypes().get(2)).isEqualTo(DataTypes.FLOAT);
}
use of com.datastax.oss.driver.api.core.type.TupleType in project java-driver by datastax.
the class DataTypeClassNameParserTest method should_parse_tuple.
@Test
@UseDataProvider(location = TestDataProviders.class, value = "locales")
public void should_parse_tuple(Locale locale) {
Locale def = Locale.getDefault();
try {
Locale.setDefault(locale);
TupleType tupleType = (TupleType) parse("org.apache.cassandra.db.marshal.TupleType(" + "org.apache.cassandra.db.marshal.Int32Type," + "org.apache.cassandra.db.marshal.UTF8Type," + "org.apache.cassandra.db.marshal.FloatType)");
assertThat(tupleType.getComponentTypes().size()).isEqualTo(3);
assertThat(tupleType.getComponentTypes().get(0)).isEqualTo(DataTypes.INT);
assertThat(tupleType.getComponentTypes().get(1)).isEqualTo(DataTypes.TEXT);
assertThat(tupleType.getComponentTypes().get(2)).isEqualTo(DataTypes.FLOAT);
} finally {
Locale.setDefault(def);
}
}
use of com.datastax.oss.driver.api.core.type.TupleType 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.driver.api.core.type.TupleType in project java-driver by datastax.
the class TupleValueSerializer method readDynamicCustomValue.
@Override
public TupleValue readDynamicCustomValue(Buffer buffer, GraphBinaryReader context) throws IOException {
// read the type first
DataType type = ComplexTypeSerializerUtil.decodeTypeDefinition(buffer, driverContext);
assert type instanceof TupleType : "GraphBinary TupleValue deserializer was called on a value that is not encoded as a TupleValue.";
TupleType tupleType = (TupleType) type;
TupleValue value = tupleType.newValue();
// then decode the values from the buffer
return ComplexTypeSerializerUtil.decodeValue(buffer, value, tupleType.getComponentTypes().size());
}
use of com.datastax.oss.driver.api.core.type.TupleType in project java-driver by datastax.
the class DataTypeSerializationTest method should_serialize_and_deserialize.
@Test
public void should_serialize_and_deserialize() {
TupleType tuple = DataTypes.tupleOf(DataTypes.INT, DataTypes.TEXT);
UserDefinedType udt = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("field1"), DataTypes.INT).withField(CqlIdentifier.fromInternal("field2"), DataTypes.TEXT).build();
// Because primitive and custom types never use the codec registry, we consider them always
// attached
should_serialize_and_deserialize(DataTypes.INT, false);
should_serialize_and_deserialize(DataTypes.custom("some.class.name"), false);
should_serialize_and_deserialize(tuple, true);
should_serialize_and_deserialize(udt, true);
should_serialize_and_deserialize(DataTypes.listOf(DataTypes.INT), false);
should_serialize_and_deserialize(DataTypes.listOf(tuple), true);
should_serialize_and_deserialize(DataTypes.setOf(udt), true);
should_serialize_and_deserialize(DataTypes.mapOf(tuple, udt), true);
}
Aggregations