use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class DefaultUdtValueTest method should_support_null_items_when_setting_in_bulk.
@Test
public void should_support_null_items_when_setting_in_bulk() throws UnsupportedEncodingException {
UserDefinedType type = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("field1"), DataTypes.INT).withField(CqlIdentifier.fromInternal("field2"), DataTypes.TEXT).build();
when(codecRegistry.<Integer>codecFor(DataTypes.INT)).thenReturn(TypeCodecs.INT);
when(codecRegistry.codecFor(DataTypes.TEXT, "foo")).thenReturn(TypeCodecs.TEXT);
UdtValue value = type.newValue(null, "foo");
assertThat(value.isNull(0)).isTrue();
assertThat(value.getString(1)).isEqualTo("foo");
}
use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class DefaultUdtValueTest method should_serialize_and_deserialize.
@Test
public void should_serialize_and_deserialize() {
UserDefinedType type = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("field1"), DataTypes.INT).withField(CqlIdentifier.fromInternal("field2"), DataTypes.TEXT).build();
UdtValue in = type.newValue();
in = in.setBytesUnsafe(0, Bytes.fromHexString("0x00000001"));
in = in.setBytesUnsafe(1, Bytes.fromHexString("0x61"));
UdtValue out = SerializationHelper.serializeAndDeserialize(in);
assertThat(out.getType()).isEqualTo(in.getType());
assertThat(out.getType().isDetached()).isTrue();
assertThat(Bytes.toHexString(out.getBytesUnsafe(0))).isEqualTo("0x00000001");
assertThat(Bytes.toHexString(out.getBytesUnsafe(1))).isEqualTo("0x61");
}
use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class DefaultUdtValueTest method newInstance.
@Override
protected UdtValue newInstance(List<DataType> dataTypes, List<Object> values, AttachmentPoint attachmentPoint) {
UserDefinedTypeBuilder builder = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type"));
for (int i = 0; i < dataTypes.size(); i++) {
builder.withField(CqlIdentifier.fromInternal("field" + i), dataTypes.get(i));
}
UserDefinedType userDefinedType = builder.build();
userDefinedType.attach(attachmentPoint);
return userDefinedType.newValue(values.toArray());
}
use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class CachingCodecRegistry method createCodec.
// Try to create a codec when we haven't found it in the cache
@NonNull
protected TypeCodec<?> createCodec(@Nullable DataType cqlType, @Nullable GenericType<?> javaType, boolean isJavaCovariant) {
LOG.trace("[{}] Cache miss, creating codec", logPrefix);
// Either type can be null, but not both.
if (javaType == null) {
assert cqlType != null;
return createCodec(cqlType);
} else if (cqlType == null) {
return createCodec(javaType, isJavaCovariant);
} else {
// Both non-null
TypeToken<?> token = javaType.__getToken();
if (cqlType instanceof ListType && List.class.isAssignableFrom(token.getRawType())) {
DataType elementCqlType = ((ListType) cqlType).getElementType();
TypeCodec<Object> elementCodec;
if (token.getType() instanceof ParameterizedType) {
Type[] typeArguments = ((ParameterizedType) token.getType()).getActualTypeArguments();
GenericType<?> elementJavaType = GenericType.of(typeArguments[0]);
elementCodec = uncheckedCast(codecFor(elementCqlType, elementJavaType, isJavaCovariant));
} else {
elementCodec = codecFor(elementCqlType);
}
return TypeCodecs.listOf(elementCodec);
} else if (cqlType instanceof SetType && Set.class.isAssignableFrom(token.getRawType())) {
DataType elementCqlType = ((SetType) cqlType).getElementType();
TypeCodec<Object> elementCodec;
if (token.getType() instanceof ParameterizedType) {
Type[] typeArguments = ((ParameterizedType) token.getType()).getActualTypeArguments();
GenericType<?> elementJavaType = GenericType.of(typeArguments[0]);
elementCodec = uncheckedCast(codecFor(elementCqlType, elementJavaType, isJavaCovariant));
} else {
elementCodec = codecFor(elementCqlType);
}
return TypeCodecs.setOf(elementCodec);
} else if (cqlType instanceof MapType && Map.class.isAssignableFrom(token.getRawType())) {
DataType keyCqlType = ((MapType) cqlType).getKeyType();
DataType valueCqlType = ((MapType) cqlType).getValueType();
TypeCodec<Object> keyCodec;
TypeCodec<Object> valueCodec;
if (token.getType() instanceof ParameterizedType) {
Type[] typeArguments = ((ParameterizedType) token.getType()).getActualTypeArguments();
GenericType<?> keyJavaType = GenericType.of(typeArguments[0]);
GenericType<?> valueJavaType = GenericType.of(typeArguments[1]);
keyCodec = uncheckedCast(codecFor(keyCqlType, keyJavaType, isJavaCovariant));
valueCodec = uncheckedCast(codecFor(valueCqlType, valueJavaType, isJavaCovariant));
} else {
keyCodec = codecFor(keyCqlType);
valueCodec = codecFor(valueCqlType);
}
return TypeCodecs.mapOf(keyCodec, valueCodec);
} else if (cqlType instanceof TupleType && TupleValue.class.isAssignableFrom(token.getRawType())) {
return TypeCodecs.tupleOf((TupleType) cqlType);
} else if (cqlType instanceof UserDefinedType && UdtValue.class.isAssignableFrom(token.getRawType())) {
return TypeCodecs.udtOf((UserDefinedType) cqlType);
} else if (cqlType instanceof CustomType && ByteBuffer.class.isAssignableFrom(token.getRawType())) {
return TypeCodecs.custom(cqlType);
}
throw new CodecNotFoundException(cqlType, javaType);
}
}
use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class GraphDataTypesTest method complexUdtTests.
@Test
public void complexUdtTests() throws IOException {
UserDefinedType type1 = new UserDefinedTypeBuilder("ks", "udt1").withField("a", INT).withField("b", TEXT).build();
verifySerDeBinary(type1.newValue(1, "2"));
TupleType secondNested = tupleOf(BIGINT, listOf(BIGINT));
TupleType firstNested = tupleOf(TEXT, secondNested);
UserDefinedType type2 = new UserDefinedTypeBuilder("ks", "udt2").withField("a", INT).withField("b", TEXT).withField("c", type1).withField("mylist", listOf(BIGINT)).withField("mytuple_withlist", firstNested).build();
verifySerDeBinary(type2.newValue(1, "2", type1.newValue(3, "4"), ImmutableList.of(5L), firstNested.newValue("6", secondNested.newValue(7L, ImmutableList.of(8L)))));
UserDefinedType type3 = new UserDefinedTypeBuilder("ks", "udt3").withField("a", listOf(INT)).withField("b", setOf(FLOAT)).withField("c", mapOf(TEXT, BIGINT)).withField("d", listOf(listOf(DOUBLE))).withField("e", setOf(setOf(FLOAT))).withField("f", listOf(tupleOf(INT, TEXT))).build();
verifySerDeBinary(type3.newValue(ImmutableList.of(1), ImmutableSet.of(2.1f), ImmutableMap.of("3", 4L), ImmutableList.of(ImmutableList.of(5.1d, 6.1d), ImmutableList.of(7.1d)), ImmutableSet.of(ImmutableSet.of(8.1f), ImmutableSet.of(9.1f)), ImmutableList.of(tupleOf(INT, TEXT).newValue(10, "11"))));
}
Aggregations