Search in sources :

Example 11 with ListType

use of com.datastax.oss.driver.api.core.type.ListType in project java-driver by datastax.

the class UserDefinedTypeListParserTest method should_resolve_nested_dependency.

@Test
public void should_resolve_nested_dependency() {
    UserDefinedTypeParser parser = new UserDefinedTypeParser(new DataTypeCqlNameParser(), context);
    Map<CqlIdentifier, UserDefinedType> types = parser.parse(KEYSPACE_ID, mockTypeRow("ks", "a", ImmutableList.of("bs"), ImmutableList.of("frozen<tuple<int, frozen<list<frozen<b>>>>>")), mockTypeRow("ks", "b", ImmutableList.of("i"), ImmutableList.of("int")));
    assertThat(types).hasSize(2);
    UserDefinedType aType = types.get(CqlIdentifier.fromInternal("a"));
    UserDefinedType bType = types.get(CqlIdentifier.fromInternal("b"));
    TupleType tupleType = (TupleType) aType.getFieldTypes().get(0);
    ListType listType = (ListType) tupleType.getComponentTypes().get(1);
    assertThat(listType.getElementType()).isEqualTo(bType);
}
Also used : ListType(com.datastax.oss.driver.api.core.type.ListType) TupleType(com.datastax.oss.driver.api.core.type.TupleType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Test(org.junit.Test)

Example 12 with ListType

use of com.datastax.oss.driver.api.core.type.ListType in project java-driver by datastax.

the class DataTypeDetachableTest method list_should_be_attached_if_its_element_is.

@Test
public void list_should_be_attached_if_its_element_is() {
    TupleType tuple = DataTypes.tupleOf(DataTypes.INT);
    ListType list = DataTypes.listOf(tuple);
    assertThat(tuple.isDetached()).isTrue();
    assertThat(list.isDetached()).isTrue();
    tuple.attach(attachmentPoint);
    assertThat(list.isDetached()).isFalse();
}
Also used : ListType(com.datastax.oss.driver.api.core.type.ListType) TupleType(com.datastax.oss.driver.api.core.type.TupleType) Test(org.junit.Test)

Example 13 with ListType

use of com.datastax.oss.driver.api.core.type.ListType in project java-driver by datastax.

the class CachingCodecRegistryTest method should_allow_covariance_for_lookups_by_cql_type_and_value.

@Test
public void should_allow_covariance_for_lookups_by_cql_type_and_value() {
    TestCachingCodecRegistry registry = new TestCachingCodecRegistry(mockCache);
    registry.register(new ACodec());
    InOrder inOrder = inOrder(mockCache);
    inOrder.verify(mockCache).lookup(DataTypes.INT, GenericType.of(A.class), false);
    // covariance allowed
    assertThat(registry.codecFor(DataTypes.INT, new B())).isInstanceOf(ACodec.class);
    // no cache hit since we find the custom codec directly
    inOrder.verifyNoMoreInteractions();
    // note: in Java, type parameters are always invariant, so List<B> is not a subtype of List<A>;
    // but in practice, a codec for List<A> is capable of encoding a List<B>, so we allow it (even
    // if in driver 3.x that was forbidden).
    List<B> list = Lists.newArrayList(new B());
    ListType cqlType = DataTypes.listOf(DataTypes.INT);
    TypeCodec<List<B>> actual = registry.codecFor(cqlType, list);
    assertThat(actual).isInstanceOf(ListCodec.class);
    assertThat(actual.getJavaType()).isEqualTo(GenericType.listOf(A.class));
    assertThat(actual.accepts(list)).isTrue();
    // accepts(GenericType) remains invariant, so it returns false for List<B>
    assertThat(actual.accepts(GenericType.listOf(B.class))).isFalse();
    inOrder.verify(mockCache).lookup(cqlType, GenericType.listOf(B.class), true);
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) ListType(com.datastax.oss.driver.api.core.type.ListType) List(java.util.List) Test(org.junit.Test)

Aggregations

ListType (com.datastax.oss.driver.api.core.type.ListType)13 MapType (com.datastax.oss.driver.api.core.type.MapType)7 SetType (com.datastax.oss.driver.api.core.type.SetType)7 TupleType (com.datastax.oss.driver.api.core.type.TupleType)7 DataType (com.datastax.oss.driver.api.core.type.DataType)6 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)6 Test (org.junit.Test)6 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)4 CustomType (com.datastax.oss.driver.api.core.type.CustomType)3 CodecNotFoundException (com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException)3 NonNull (edu.umd.cs.findbugs.annotations.NonNull)3 TypeCodec (com.datastax.oss.driver.api.core.type.codec.TypeCodec)2 List (java.util.List)2 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)1 ColumnMetadata (com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata)1 TableMetadata (com.datastax.oss.driver.api.core.metadata.schema.TableMetadata)1 ViewMetadata (com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata)1 GenericType (com.datastax.oss.driver.api.core.type.reflect.GenericType)1 ShallowUserDefinedType (com.datastax.oss.driver.internal.core.metadata.schema.ShallowUserDefinedType)1 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)1