Skip to content

Commit

Permalink
feat: 🚀 Support boolean parsing in Datatables
Browse files Browse the repository at this point in the history
  • Loading branch information
gaeljw committed Sep 14, 2022
1 parent 823c380 commit a4b5224
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public DataTableTypeRegistry(Locale locale) {
TableCellTransformer<Double> doubleTableCellTransformer = applyIfPresent(numberParser::parseDouble);
defineDataTableType(new DataTableType(Double.class, doubleTableCellTransformer));
defineDataTableType(new DataTableType(double.class, doubleTableCellTransformer));

TableCellTransformer<Boolean> booleanTableCellTransformer = applyIfPresent(Boolean::parseBoolean);
defineDataTableType(new DataTableType(Boolean.class, booleanTableCellTransformer, true));
defineDataTableType(new DataTableType(boolean.class, booleanTableCellTransformer, true));
}

private static <R> TableCellTransformer<R> applyIfPresent(Function<String, R> f) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class DataTableTypeRegistryTest {
private static final Type LIST_OF_LIST_OF_FLOAT = aListOf(aListOf(Float.class));
private static final Type LIST_OF_LIST_OF_DOUBLE = aListOf(aListOf(Double.class));
private static final Type LIST_OF_LIST_OF_STRING = aListOf(aListOf(String.class));
private static final Type LIST_OF_LIST_OF_BOOLEAN = aListOf(aListOf(Boolean.class));
private static final Type LIST_OF_LIST_OF_OBJECT = aListOf(aListOf(Object.class));

private static final TableCellByTypeTransformer PLACE_TABLE_CELL_TRANSFORMER = (value,
Expand Down Expand Up @@ -244,4 +245,28 @@ void object_transformer_is_replaceable() {
singletonList(singletonList("")),
dataTableType.transform(singletonList(singletonList("[blank]"))));
}

@Test
void parse_boolean() {
DataTableTypeRegistry registry = new DataTableTypeRegistry(Locale.ENGLISH);
DataTableType dataTableType = registry.lookupTableTypeByType(LIST_OF_LIST_OF_BOOLEAN);
assertEquals(
singletonList(singletonList(Boolean.TRUE)),
dataTableType.transform(singletonList(singletonList("true"))));
assertEquals(
singletonList(singletonList(Boolean.FALSE)),
dataTableType.transform(singletonList(singletonList("false"))));
}

@Test
void boolean_transformer_is_replaceable() {
DataTableTypeRegistry registry = new DataTableTypeRegistry(Locale.ENGLISH);
registry.defineDataTableType(
new DataTableType(Boolean.class, (String cell) -> "yes".equals(cell)));
DataTableType dataTableType = registry.lookupTableTypeByType(LIST_OF_LIST_OF_BOOLEAN);
assertEquals(
singletonList(singletonList(Boolean.TRUE)),
dataTableType.transform(singletonList(singletonList("yes"))));
}

}

0 comments on commit a4b5224

Please sign in to comment.