Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,30 +89,21 @@ private static void addToBuilder(
}
break;
case INT_ARRAY:
List<Long> longList = checkListOfType(value, Long.class);
if (longList != null) {
builder.put(AttributeKey.longArrayKey(name), longList);
return;
}
List<Integer> intList = checkListOfType(value, Integer.class);
if (intList != null) {
List<Number> intList = checkListOfType(value, Number.class);
if (intList != null
&& intList.stream().allMatch(i -> (i instanceof Integer) || (i instanceof Long))) {
builder.put(
AttributeKey.longArrayKey(name),
intList.stream().map(i -> (long) i).collect(toList()));
intList.stream().map(Number::longValue).collect(toList()));
return;
}
break;
case DOUBLE_ARRAY:
List<Double> doubleList = checkListOfType(value, Double.class);
List<Number> doubleList = checkListOfType(value, Number.class);
if (doubleList != null) {
builder.put(AttributeKey.doubleArrayKey(name), doubleList);
return;
}
List<Float> floatList = checkListOfType(value, Float.class);
if (floatList != null) {
builder.put(
AttributeKey.doubleArrayKey(name),
floatList.stream().map(i -> (double) i).collect(toList()));
doubleList.stream().map(Number::doubleValue).collect(toList()));
return;
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,51 @@ private static Stream<Arguments> invalidAttributes() {
.setName("key")
.setType(AttributeTypeModel.INT)
.setValue(true)),
"Error processing attribute with name \"key\": value did not match type INT"));
"Error processing attribute with name \"key\": value did not match type INT"),
Arguments.argumentSet(
"wrong type int array with decimal",
Collections.singletonList(
new AttributeNameValueModel()
.setName("key")
.setType(AttributeTypeModel.INT_ARRAY)
.setValue(Arrays.asList(1, 2.5))),
"Error processing attribute with name \"key\": value did not match type INT_ARRAY"),
Arguments.argumentSet(
"wrong type double array with string",
Collections.singletonList(
new AttributeNameValueModel()
.setName("key")
.setType(AttributeTypeModel.DOUBLE_ARRAY)
.setValue(Collections.singletonList("a"))),
"Error processing attribute with name \"key\": value did not match type DOUBLE_ARRAY"));
}

@Test
void create_NumericArraysWithMixedElementTypes() {
// The YAML parser returns Integer, Long or Double for each list element depending on its value
assertThat(
AttributeListFactory.getInstance()
.create(
Arrays.asList(
new AttributeNameValueModel()
.setName("doubleArrFromInts")
.setValue(Arrays.asList(1, 2))
.setType(AttributeTypeModel.DOUBLE_ARRAY),
new AttributeNameValueModel()
.setName("doubleArrMixed")
.setValue(Arrays.asList(0.5, 1))
.setType(AttributeTypeModel.DOUBLE_ARRAY),
new AttributeNameValueModel()
.setName("intArrMixed")
.setValue(Arrays.asList(1, 3000000000L))
.setType(AttributeTypeModel.INT_ARRAY)),
mock(DeclarativeConfigContext.class)))
.isEqualTo(
Attributes.builder()
.put("doubleArrFromInts", 1.0d, 2.0d)
.put("doubleArrMixed", 0.5d, 1.0d)
.put("intArrMixed", 1L, 3000000000L)
.build());
}

@Test
Expand Down
Loading