use of org.apache.nifi.serialization.record.type.ChoiceDataType in project nifi by apache.
the class StandardSchemaValidator method getCanonicalDataType.
private DataType getCanonicalDataType(final DataType dataType, final Object rawValue, final StandardSchemaValidationResult result, final String fieldPrefix, final RecordField field) {
final RecordFieldType fieldType = dataType.getFieldType();
final DataType canonicalDataType;
if (fieldType == RecordFieldType.CHOICE) {
canonicalDataType = DataTypeUtils.chooseDataType(rawValue, (ChoiceDataType) dataType);
if (canonicalDataType == null) {
result.addValidationError(new StandardValidationError(concat(fieldPrefix, field), rawValue, ValidationErrorType.INVALID_FIELD, "Value is of type " + rawValue.getClass().getName() + " but was expected to be of type " + dataType));
return null;
}
} else {
canonicalDataType = dataType;
}
return canonicalDataType;
}
use of org.apache.nifi.serialization.record.type.ChoiceDataType in project nifi by apache.
the class StandardSchemaValidator method isTypeCorrect.
private boolean isTypeCorrect(final Object value, final DataType dataType) {
switch(dataType.getFieldType()) {
case ARRAY:
if (!(value instanceof Object[])) {
return false;
}
final ArrayDataType arrayDataType = (ArrayDataType) dataType;
final DataType elementType = arrayDataType.getElementType();
final Object[] array = (Object[]) value;
for (final Object arrayVal : array) {
if (!isTypeCorrect(arrayVal, elementType)) {
return false;
}
}
return true;
case MAP:
if (!(value instanceof Map)) {
return false;
}
final MapDataType mapDataType = (MapDataType) dataType;
final DataType valueDataType = mapDataType.getValueType();
final Map<?, ?> map = (Map<?, ?>) value;
for (final Object mapValue : map.values()) {
if (!isTypeCorrect(mapValue, valueDataType)) {
return false;
}
}
return true;
case RECORD:
return value instanceof Record;
case CHOICE:
final ChoiceDataType choiceDataType = (ChoiceDataType) dataType;
for (final DataType choice : choiceDataType.getPossibleSubTypes()) {
if (isTypeCorrect(value, choice)) {
return true;
}
}
return false;
case BIGINT:
return value instanceof BigInteger;
case BOOLEAN:
return value instanceof Boolean;
case BYTE:
return value instanceof Byte;
case CHAR:
return value instanceof Character;
case DATE:
return value instanceof java.sql.Date;
case DOUBLE:
return value instanceof Double;
case FLOAT:
// Same goes for Short/Integer
return value instanceof Float;
case INT:
return value instanceof Integer;
case LONG:
return value instanceof Long;
case SHORT:
return value instanceof Short;
case STRING:
return value instanceof String;
case TIME:
return value instanceof java.sql.Time;
case TIMESTAMP:
return value instanceof java.sql.Timestamp;
}
return false;
}
use of org.apache.nifi.serialization.record.type.ChoiceDataType in project nifi by apache.
the class DataTypeUtils method convertType.
public static Object convertType(final Object value, final DataType dataType, final Supplier<DateFormat> dateFormat, final Supplier<DateFormat> timeFormat, final Supplier<DateFormat> timestampFormat, final String fieldName) {
if (value == null) {
return null;
}
switch(dataType.getFieldType()) {
case BIGINT:
return toBigInt(value, fieldName);
case BOOLEAN:
return toBoolean(value, fieldName);
case BYTE:
return toByte(value, fieldName);
case CHAR:
return toCharacter(value, fieldName);
case DATE:
return toDate(value, dateFormat, fieldName);
case DOUBLE:
return toDouble(value, fieldName);
case FLOAT:
return toFloat(value, fieldName);
case INT:
return toInteger(value, fieldName);
case LONG:
return toLong(value, fieldName);
case SHORT:
return toShort(value, fieldName);
case STRING:
return toString(value, () -> getDateFormat(dataType.getFieldType(), dateFormat, timeFormat, timestampFormat));
case TIME:
return toTime(value, timeFormat, fieldName);
case TIMESTAMP:
return toTimestamp(value, timestampFormat, fieldName);
case ARRAY:
return toArray(value, fieldName);
case MAP:
return toMap(value, fieldName);
case RECORD:
final RecordDataType recordType = (RecordDataType) dataType;
final RecordSchema childSchema = recordType.getChildSchema();
return toRecord(value, childSchema, fieldName);
case CHOICE:
{
final ChoiceDataType choiceDataType = (ChoiceDataType) dataType;
final DataType chosenDataType = chooseDataType(value, choiceDataType);
if (chosenDataType == null) {
throw new IllegalTypeConversionException("Cannot convert value [" + value + "] of type " + value.getClass() + " for field " + fieldName + " to any of the following available Sub-Types for a Choice: " + choiceDataType.getPossibleSubTypes());
}
return convertType(value, chosenDataType, fieldName);
}
}
return null;
}
Aggregations