in maestro-common/src/main/java/com/netflix/maestro/models/parameter/MapParameter.java [105:167]
private Map<String, Object> castValuesWithListToArray(
String name, Map<String, ParamDefinition> mapParamDef, Map<String, Object> mapToCast) {
Map<String, Object> result = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : mapToCast.entrySet()) {
Object value = entry.getValue();
Object newValue = value;
ParamDefinition paramDef = mapParamDef == null ? null : mapParamDef.get(entry.getKey());
if (value instanceof Map) {
if (paramDef != null) {
if (paramDef.getType() == ParamType.MAP) {
newValue =
castValuesWithListToArray(
name, paramDef.asMapParamDef().getValue(), (Map<String, Object>) value);
}
} else {
newValue = castValuesWithListToArray(name, null, (Map<String, Object>) value);
}
} else if (value instanceof List<?>) {
final List<?> listValue = (List<?>) value;
if (!(listValue).isEmpty()) {
Object firstValue = listValue.getFirst();
if (firstValue instanceof Long) {
long[] arr = ((List<Long>) value).stream().mapToLong(Long::longValue).toArray();
newValue = arr;
} else if (firstValue instanceof Double) {
double[] arr =
((List<Double>) value).stream().mapToDouble(Double::doubleValue).toArray();
newValue = arr;
} else if (firstValue instanceof BigDecimal) {
double[] arr =
((List<BigDecimal>) value)
.stream().mapToDouble(BigDecimal::doubleValue).toArray();
newValue = arr;
} else if (firstValue instanceof Boolean) {
boolean[] arr = new boolean[((List<?>) value).size()];
for (int i = 0; i < arr.length; i++) {
arr[i] = ((Boolean) ((List<?>) value).get(i)).booleanValue();
}
newValue = arr;
} else if (firstValue instanceof String) {
String[] arr = ((List<String>) value).stream().toArray(String[]::new);
newValue = arr;
} else {
throw new MaestroInternalError(
"MapParam [%s] param [%s] has an invalid type List as value [%s]",
name, entry.getKey(), value.getClass().getName());
}
} else if (paramDef != null) { // empty list case
if (paramDef.getType() == ParamType.LONG_ARRAY) {
newValue = new long[0];
} else if (paramDef.getType() == ParamType.DOUBLE_ARRAY) {
newValue = new double[0];
} else if (paramDef.getType() == ParamType.BOOLEAN_ARRAY) {
newValue = new boolean[0];
} else if (paramDef.getType() == ParamType.STRING_ARRAY) {
newValue = new String[0];
}
}
}
result.put(entry.getKey(), newValue);
}
return result;
}