use of com.datastax.oss.dsbulk.codecs.text.json.dse.JsonNodeToDateRangeCodec in project dsbulk by datastax.
the class JsonNodeConvertingCodecProvider method createJsonNodeConvertingCodec.
@Nullable
private ConvertingCodec<JsonNode, ?> createJsonNodeConvertingCodec(@NonNull DataType cqlType, @NonNull ConvertingCodecFactory codecFactory, boolean rootCodec) {
CodecRegistry codecRegistry = codecFactory.getCodecRegistry();
ConversionContext context = codecFactory.getContext();
// Don't apply null strings for non-root codecs
List<String> nullStrings = rootCodec ? context.getAttribute(NULL_STRINGS) : ImmutableList.of();
int cqlTypeCode = cqlType.getProtocolCode();
switch(cqlTypeCode) {
case ASCII:
case VARCHAR:
TypeCodec<String> typeCodec = codecRegistry.codecFor(cqlType);
return new JsonNodeToStringCodec(typeCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
case BOOLEAN:
return new JsonNodeToBooleanCodec(context.getAttribute(BOOLEAN_INPUT_WORDS), nullStrings);
case TINYINT:
return new JsonNodeToByteCodec(context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
case SMALLINT:
return new JsonNodeToShortCodec(context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
case INT:
return new JsonNodeToIntegerCodec(context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
case BIGINT:
return new JsonNodeToLongCodec(TypeCodecs.BIGINT, context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
case COUNTER:
return new JsonNodeToLongCodec(TypeCodecs.COUNTER, context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
case FLOAT:
return new JsonNodeToFloatCodec(context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
case DOUBLE:
return new JsonNodeToDoubleCodec(context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
case VARINT:
return new JsonNodeToBigIntegerCodec(context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
case DECIMAL:
return new JsonNodeToBigDecimalCodec(context.getAttribute(NUMBER_FORMAT), context.getAttribute(OVERFLOW_STRATEGY), context.getAttribute(ROUNDING_MODE), context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(TIME_UNIT), context.getAttribute(EPOCH), context.getAttribute(BOOLEAN_INPUT_WORDS), context.getAttribute(BOOLEAN_NUMBERS), nullStrings);
case DATE:
return new JsonNodeToLocalDateCodec(context.getAttribute(LOCAL_DATE_FORMAT), context.getAttribute(TIME_ZONE), nullStrings);
case TIME:
return new JsonNodeToLocalTimeCodec(context.getAttribute(LOCAL_TIME_FORMAT), context.getAttribute(TIME_ZONE), nullStrings);
case TIMESTAMP:
return new JsonNodeToInstantCodec(context.getAttribute(TIMESTAMP_FORMAT), context.getAttribute(TIME_ZONE), context.getAttribute(EPOCH), nullStrings);
case INET:
return new JsonNodeToInetAddressCodec(nullStrings);
case UUID:
{
ConvertingCodec<String, Instant> instantCodec = codecFactory.createConvertingCodec(DataTypes.TIMESTAMP, GenericType.STRING, false);
return new JsonNodeToUUIDCodec(TypeCodecs.UUID, instantCodec, context.getAttribute(TIME_UUID_GENERATOR), nullStrings);
}
case TIMEUUID:
{
ConvertingCodec<String, Instant> instantCodec = codecFactory.createConvertingCodec(DataTypes.TIMESTAMP, GenericType.STRING, false);
return new JsonNodeToUUIDCodec(TypeCodecs.TIMEUUID, instantCodec, context.getAttribute(TIME_UUID_GENERATOR), nullStrings);
}
case BLOB:
return new JsonNodeToBlobCodec(context.getAttribute(BINARY_FORMAT), nullStrings);
case DURATION:
return new JsonNodeToDurationCodec(nullStrings);
case LIST:
{
DataType elementType = ((ListType) cqlType).getElementType();
TypeCodec<List<Object>> collectionCodec = codecRegistry.codecFor(cqlType);
@SuppressWarnings("unchecked") ConvertingCodec<JsonNode, Object> eltCodec = (ConvertingCodec<JsonNode, Object>) createJsonNodeConvertingCodec(elementType, codecFactory, false);
return new JsonNodeToListCodec<>(collectionCodec, eltCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
}
case SET:
{
DataType elementType = ((SetType) cqlType).getElementType();
TypeCodec<Set<Object>> collectionCodec = codecRegistry.codecFor(cqlType);
@SuppressWarnings("unchecked") ConvertingCodec<JsonNode, Object> eltCodec = (ConvertingCodec<JsonNode, Object>) createJsonNodeConvertingCodec(elementType, codecFactory, false);
return new JsonNodeToSetCodec<>(collectionCodec, eltCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
}
case MAP:
{
DataType keyType = ((MapType) cqlType).getKeyType();
DataType valueType = ((MapType) cqlType).getValueType();
TypeCodec<Map<Object, Object>> mapCodec = codecRegistry.codecFor(cqlType);
ConvertingCodec<String, Object> keyCodec = codecFactory.createConvertingCodec(keyType, GenericType.STRING, false);
ConvertingCodec<JsonNode, Object> valueCodec = codecFactory.createConvertingCodec(valueType, JSON_NODE_TYPE, false);
return new JsonNodeToMapCodec<>(mapCodec, keyCodec, valueCodec, context.getAttribute(OBJECT_MAPPER), nullStrings);
}
case TUPLE:
{
TypeCodec<TupleValue> tupleCodec = codecRegistry.codecFor(cqlType);
ImmutableList.Builder<ConvertingCodec<JsonNode, Object>> eltCodecs = new ImmutableList.Builder<>();
for (DataType eltType : ((TupleType) cqlType).getComponentTypes()) {
ConvertingCodec<JsonNode, Object> eltCodec = codecFactory.createConvertingCodec(eltType, JSON_NODE_TYPE, false);
eltCodecs.add(Objects.requireNonNull(eltCodec));
}
return new JsonNodeToTupleCodec(tupleCodec, eltCodecs.build(), context.getAttribute(OBJECT_MAPPER), nullStrings, context.getAttribute(ALLOW_EXTRA_FIELDS), context.getAttribute(ALLOW_MISSING_FIELDS));
}
case UDT:
{
TypeCodec<UdtValue> udtCodec = codecRegistry.codecFor(cqlType);
ImmutableMap.Builder<CqlIdentifier, ConvertingCodec<JsonNode, Object>> fieldCodecs = new ImmutableMap.Builder<>();
List<CqlIdentifier> fieldNames = ((UserDefinedType) cqlType).getFieldNames();
List<DataType> fieldTypes = ((UserDefinedType) cqlType).getFieldTypes();
assert (fieldNames.size() == fieldTypes.size());
for (int idx = 0; idx < fieldNames.size(); idx++) {
CqlIdentifier fieldName = fieldNames.get(idx);
DataType fieldType = fieldTypes.get(idx);
ConvertingCodec<JsonNode, Object> fieldCodec = codecFactory.createConvertingCodec(fieldType, JSON_NODE_TYPE, false);
fieldCodecs.put(fieldName, Objects.requireNonNull(fieldCodec));
}
return new JsonNodeToUDTCodec(udtCodec, fieldCodecs.build(), context.getAttribute(OBJECT_MAPPER), nullStrings, context.getAttribute(ALLOW_EXTRA_FIELDS), context.getAttribute(ALLOW_MISSING_FIELDS));
}
case CUSTOM:
{
CustomType customType = (CustomType) cqlType;
switch(customType.getClassName()) {
case POINT_CLASS_NAME:
return new JsonNodeToPointCodec(context.getAttribute(OBJECT_MAPPER), context.getAttribute(GEO_FORMAT), nullStrings);
case LINE_STRING_CLASS_NAME:
return new JsonNodeToLineStringCodec(context.getAttribute(OBJECT_MAPPER), context.getAttribute(GEO_FORMAT), nullStrings);
case POLYGON_CLASS_NAME:
return new JsonNodeToPolygonCodec(context.getAttribute(OBJECT_MAPPER), context.getAttribute(GEO_FORMAT), nullStrings);
case DATE_RANGE_CLASS_NAME:
return new JsonNodeToDateRangeCodec(nullStrings);
}
// fall through
}
default:
try {
TypeCodec<?> innerCodec = codecRegistry.codecFor(cqlType);
LOGGER.warn(String.format("CQL type %s is not officially supported by this version of DSBulk; " + "JSON literals will be parsed and formatted using registered codec %s", cqlType, innerCodec.getClass().getSimpleName()));
return new JsonNodeToUnknownTypeCodec<>(innerCodec, nullStrings);
} catch (CodecNotFoundException ignored) {
}
return null;
}
}
Aggregations