kl0u commented on a change in pull request #9976: [FLINK-14493][core] Introduce
data types to ConfigOptions.
URL: https://github.com/apache/flink/pull/9976#discussion_r338461856
##########
File path:
flink-core/src/main/java/org/apache/flink/configuration/Configuration.java
##########
@@ -850,98 +856,172 @@ private void loggingFallback(FallbackKey fallbackKey,
ConfigOption<?> configOpti
// Type conversion
//
--------------------------------------------------------------------------------------------
- private int convertToInt(Object o, int defaultValue) {
+ @SuppressWarnings("unchecked")
+ private <T> T convertValue(Object rawValue, Class clazz) {
+ if (Integer.class.equals(clazz)) {
+ return (T) convertToInt(rawValue);
+ } else if (Long.class.equals(clazz)) {
+ return (T) convertToLong(rawValue);
+ } else if (Boolean.class.equals(clazz)) {
+ return (T) convertToBoolean(rawValue);
+ } else if (Float.class.equals(clazz)) {
+ return (T) convertToFloat(rawValue);
+ } else if (Double.class.equals(clazz)) {
+ return (T) convertToDouble(rawValue);
+ } else if (String.class.equals(clazz)) {
+ return (T) convertToString(rawValue);
+ } else if (clazz.isEnum()) {
+ return (T) convertToEnum(rawValue, clazz);
+ } else if (clazz == Duration.class) {
+ return (T) convertToDuration(rawValue);
+ } else if (clazz == MemorySize.class) {
+ return (T) convertToMemorySize(rawValue);
+ } else if (clazz == Map.class) {
+ return (T) convertToProperties(rawValue);
+ }
+
+ throw new IllegalArgumentException("Unsupported type: " +
clazz);
+ }
+
+ @SuppressWarnings("unchecked")
+ private <T> T convertToList(Object rawValue, Class atomicClass) {
+ if (rawValue instanceof List) {
+ return (T) rawValue;
+ } else {
+ return (T)
StructuredOptionsSplitter.splitEscaped(rawValue.toString(), ';').stream()
+ .map(s -> convertValue(s, atomicClass))
+ .collect(Collectors.toList());
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private Map<String, String> convertToProperties(Object o) {
+ if (o instanceof Map) {
+ return (Map<String, String>) o;
+ } else {
+ List<String> listOfRawProperties =
StructuredOptionsSplitter.splitEscaped(o.toString(), ',');
+ return listOfRawProperties.stream()
+ .map(s ->
StructuredOptionsSplitter.splitEscaped(s, ':'))
+ .map(pair -> {
+ if (pair.size() != 2) {
+ throw new
IllegalArgumentException("Could not parse pair in the map " + pair);
+ } else {
+ return pair;
+ }
+ })
+ .collect(Collectors.toMap(
+ a -> a.get(0),
+ a -> a.get(1)
+ ));
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private <E extends Enum<E>> E convertToEnum(Object o, Class<E> clazz) {
+ if (o.getClass().equals(clazz)) {
+ return (E) o;
+ } else {
+ return Enum.valueOf(clazz,
o.toString().toUpperCase(Locale.ROOT));
+ }
+ }
+
+ private Duration convertToDuration(Object o) {
+ if (o.getClass() == Duration.class) {
+ return (Duration) o;
+ } else {
+ return TimeUtils.parseDuration(o.toString());
+ }
+ }
+
+ private MemorySize convertToMemorySize(Object o) {
+ if (o.getClass() == MemorySize.class) {
+ return (MemorySize) o;
+ } else {
+ return MemorySize.parse(o.toString());
+ }
+ }
+
+ private String convertToString(Object o) {
+ if (o.getClass() == String.class) {
+ return (String) o;
+ } else if (o.getClass() == Duration.class) {
+ Duration duration = (Duration) o;
+ return String.format("%d ns", duration.toNanos());
+ } else {
+ return o.toString();
+ }
+ }
+
+ private Integer convertToInt(Object o) {
if (o.getClass() == Integer.class) {
return (Integer) o;
- }
- else if (o.getClass() == Long.class) {
+ } else if (o.getClass() == Long.class) {
long value = (Long) o;
if (value <= Integer.MAX_VALUE && value >=
Integer.MIN_VALUE) {
return (int) value;
} else {
- LOG.warn("Configuration value {}
overflows/underflows the integer type.", value);
- return defaultValue;
- }
- }
- else {
- try {
- return Integer.parseInt(o.toString());
- }
- catch (NumberFormatException e) {
- LOG.warn("Configuration cannot evaluate value
{} as an integer number", o);
- return defaultValue;
+ throw new
IllegalArgumentException(String.format(
+ "Configuration value %s
overflows/underflows the integer type.",
+ value));
}
+ } else {
+ return Integer.parseInt(o.toString());
}
}
- private long convertToLong(Object o, long defaultValue) {
+ private Long convertToLong(Object o) {
if (o.getClass() == Long.class) {
return (Long) o;
- }
- else if (o.getClass() == Integer.class) {
+ } else if (o.getClass() == Integer.class) {
return ((Integer) o).longValue();
- }
- else {
- try {
- return Long.parseLong(o.toString());
- }
- catch (NumberFormatException e) {
- LOG.warn("Configuration cannot evaluate value "
+ o + " as a long integer number");
- return defaultValue;
- }
+ } else {
+ return Long.parseLong(o.toString());
}
}
- private boolean convertToBoolean(Object o) {
+ private Boolean convertToBoolean(Object o) {
if (o.getClass() == Boolean.class) {
return (Boolean) o;
- }
- else {
- return Boolean.parseBoolean(o.toString());
+ } else {
+ switch (o.toString().toUpperCase()) {
+ case "TRUE":
+ return true;
+ case "FALSE":
+ return false;
+ default:
+ throw new
IllegalArgumentException(String.format(
+ "Unrecognized option for
boolean: %s. Expected either true or false(case insensitive)",
+ o));
+ }
}
}
- private float convertToFloat(Object o, float defaultValue) {
+ private Float convertToFloat(Object o) {
if (o.getClass() == Float.class) {
return (Float) o;
- }
- else if (o.getClass() == Double.class) {
+ } else if (o.getClass() == Double.class) {
double value = ((Double) o);
if (value == 0.0
- || (value >= Float.MIN_VALUE && value
<= Float.MAX_VALUE)
- || (value >= -Float.MAX_VALUE && value
<= -Float.MIN_VALUE)) {
+ || (value >= Float.MIN_VALUE && value <=
Float.MAX_VALUE)
Review comment:
I feel like the previous indentation was clearer as it allowed to visually
separate the check from the body of the `if`, but feel free to leave it if you
prefer it this way.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services