use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class TupleCodec method format.
@NonNull
@Override
public String format(@Nullable TupleValue value) {
if (value == null) {
return "NULL";
}
if (!value.getType().equals(cqlType)) {
throw new IllegalArgumentException(String.format("Invalid tuple type, expected %s but got %s", cqlType, value.getType()));
}
CodecRegistry registry = cqlType.getAttachmentPoint().getCodecRegistry();
StringBuilder sb = new StringBuilder("(");
boolean first = true;
for (int i = 0; i < value.size(); i++) {
if (first) {
first = false;
} else {
sb.append(",");
}
DataType elementType = cqlType.getComponentTypes().get(i);
TypeCodec<Object> codec = registry.codecFor(elementType);
sb.append(codec.format(value.get(i, codec)));
}
sb.append(")");
return sb.toString();
}
use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class TupleCodec method parse.
@Nullable
@Override
public TupleValue parse(@Nullable String value) {
if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) {
return null;
}
TupleValue tuple = cqlType.newValue();
int length = value.length();
int position = ParseUtils.skipSpaces(value, 0);
if (value.charAt(position) != '(') {
throw new IllegalArgumentException(String.format("Cannot parse tuple value from \"%s\", at character %d expecting '(' but got '%c'", value, position, value.charAt(position)));
}
position++;
position = ParseUtils.skipSpaces(value, position);
CodecRegistry registry = cqlType.getAttachmentPoint().getCodecRegistry();
int field = 0;
while (position < length) {
if (value.charAt(position) == ')') {
position = ParseUtils.skipSpaces(value, position + 1);
if (position == length) {
return tuple;
}
throw new IllegalArgumentException(String.format("Cannot parse tuple value from \"%s\", at character %d expecting EOF or blank, but got \"%s\"", value, position, value.substring(position)));
}
int n;
try {
n = ParseUtils.skipCQLValue(value, position);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(String.format("Cannot parse tuple value from \"%s\", invalid CQL value at field %d (character %d)", value, field, position), e);
}
String fieldValue = value.substring(position, n);
DataType elementType = cqlType.getComponentTypes().get(field);
TypeCodec<Object> codec = registry.codecFor(elementType);
Object parsed;
try {
parsed = codec.parse(fieldValue);
} catch (Exception e) {
throw new IllegalArgumentException(String.format("Cannot parse tuple value from \"%s\", invalid CQL value at field %d (character %d): %s", value, field, position, e.getMessage()), e);
}
tuple = tuple.set(field, parsed, codec);
position = n;
position = ParseUtils.skipSpaces(value, position);
if (position == length) {
throw new IllegalArgumentException(String.format("Cannot parse tuple value from \"%s\", at field %d (character %d) expecting ',' or ')', but got EOF", value, field, position));
}
if (value.charAt(position) == ')') {
continue;
}
if (value.charAt(position) != ',') {
throw new IllegalArgumentException(String.format("Cannot parse tuple value from \"%s\", at field %d (character %d) expecting ',' but got '%c'", value, field, position, value.charAt(position)));
}
// skip ','
++position;
position = ParseUtils.skipSpaces(value, position);
field += 1;
}
throw new IllegalArgumentException(String.format("Cannot parse tuple value from \"%s\", at field %d (character %d) expecting CQL value or ')', got EOF", value, field, position));
}
use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class DataTypeCqlNameParserTest method should_parse_native_types.
@Test
public void should_parse_native_types() {
for (Map.Entry<String, DataType> entry : DataTypeCqlNameParser.NATIVE_TYPES_BY_NAME.entrySet()) {
String className = entry.getKey();
DataType expectedType = entry.getValue();
assertThat(parse(className)).isEqualTo(expectedType);
}
}
use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class DataTypeClassNameParserTest method should_parse_native_types.
@Test
@UseDataProvider(location = TestDataProviders.class, value = "locales")
public void should_parse_native_types(Locale locale) {
Locale def = Locale.getDefault();
try {
Locale.setDefault(locale);
for (Map.Entry<String, DataType> entry : DataTypeClassNameParser.NATIVE_TYPES_BY_CLASS_NAME.entrySet()) {
String className = entry.getKey();
DataType expectedType = entry.getValue();
assertThat(parse(className)).isEqualTo(expectedType);
}
} finally {
Locale.setDefault(def);
}
}
use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.
the class DataTypeClassNameParserTest method should_parse_user_type_when_definition_not_already_available.
@Test
@UseDataProvider(location = TestDataProviders.class, value = "locales")
public void should_parse_user_type_when_definition_not_already_available(Locale locale) {
Locale def = Locale.getDefault();
try {
Locale.setDefault(locale);
UserDefinedType addressType = (UserDefinedType) parse("org.apache.cassandra.db.marshal.UserType(" + "foo,61646472657373," + ("737472656574:org.apache.cassandra.db.marshal.UTF8Type," + "7a6970636f6465:org.apache.cassandra.db.marshal.Int32Type," + ("70686f6e6573:org.apache.cassandra.db.marshal.SetType(" + "org.apache.cassandra.db.marshal.UserType(foo,70686f6e65," + "6e616d65:org.apache.cassandra.db.marshal.UTF8Type," + "6e756d626572:org.apache.cassandra.db.marshal.UTF8Type)") + "))"));
assertThat(addressType.getKeyspace().asInternal()).isEqualTo("foo");
assertThat(addressType.getName().asInternal()).isEqualTo("address");
assertThat(addressType.isFrozen()).isTrue();
assertThat(addressType.getFieldNames().size()).isEqualTo(3);
assertThat(addressType.getFieldNames().get(0).asInternal()).isEqualTo("street");
assertThat(addressType.getFieldTypes().get(0)).isEqualTo(DataTypes.TEXT);
assertThat(addressType.getFieldNames().get(1).asInternal()).isEqualTo("zipcode");
assertThat(addressType.getFieldTypes().get(1)).isEqualTo(DataTypes.INT);
assertThat(addressType.getFieldNames().get(2).asInternal()).isEqualTo("phones");
DataType phonesType = addressType.getFieldTypes().get(2);
assertThat(phonesType).isInstanceOf(SetType.class);
UserDefinedType phoneType = ((UserDefinedType) ((SetType) phonesType).getElementType());
assertThat(phoneType.getKeyspace().asInternal()).isEqualTo("foo");
assertThat(phoneType.getName().asInternal()).isEqualTo("phone");
assertThat(phoneType.isFrozen()).isTrue();
assertThat(phoneType.getFieldNames().size()).isEqualTo(2);
assertThat(phoneType.getFieldNames().get(0).asInternal()).isEqualTo("name");
assertThat(phoneType.getFieldTypes().get(0)).isEqualTo(DataTypes.TEXT);
assertThat(phoneType.getFieldNames().get(1).asInternal()).isEqualTo("number");
assertThat(phoneType.getFieldTypes().get(1)).isEqualTo(DataTypes.TEXT);
} finally {
Locale.setDefault(def);
}
}
Aggregations