Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datatables: Fix priority of default converters #514

Merged
merged 2 commits into from
Oct 26, 2018
Merged
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
4 changes: 3 additions & 1 deletion datatable/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Removed

### Fixed

* Fix priority of default converters
([#514](https://github.com/cucumber/cucumber/pull/514)
[mpkorstanje])
## [1.1.3] - 2018-07-27

### Added
Expand Down
4 changes: 4 additions & 0 deletions datatable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,10 @@ private class JacksonDataTableTransformer implements TableEntryByTypeTransformer
}
```

The`TableEntryByTypeTransformer` and `TableCellByTypeTransformer` are used when there is no table entry or table cell
defined for a given type. Note that when installing both `TableEntryByTypeTransformer` and `TableCellByTypeTransformer`
it becomes impossible to disambiguate between table entries and table cells. By default table entries are assumed over
table cells. This ambiguity can be resolved by adding a header.

## Diffing

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,34 +118,32 @@ public void defineDataTableType(DataTableType dataTableType) {

public DataTableType lookupTableTypeByType(final Type tableType) {
JavaType targetType = constructType(tableType);
DataTableType dataTableType = tableTypeByType.get(targetType);
return tableTypeByType.get(targetType);
}

if(dataTableType != null){
return dataTableType;
public DataTableType getDefaultTableCellTransformer(final Type tableType) {
if (defaultDataTableCellTransformer == null) {
return null;
}

if(!targetType.isCollectionLikeType()){
return null;
}
JavaType targetType = constructType(tableType);
return DataTableType.defaultCell(
targetType.getRawClass(),
defaultDataTableCellTransformer
);
}

JavaType contentType = targetType.getContentType();
if(contentType.isCollectionLikeType()) {
if(defaultDataTableCellTransformer != null){
return DataTableType.defaultCell(
contentType.getContentType().getRawClass(),
defaultDataTableCellTransformer
);
}
public DataTableType getDefaultTableEntryTransformer(final Type tableType) {
if (defaultDataTableEntryTransformer == null) {
return null;
}
if (defaultDataTableEntryTransformer != null){
return DataTableType.defaultEntry(
contentType.getRawClass(),
defaultDataTableEntryTransformer,
tableCellByTypeTransformer
);
}
return null;

JavaType targetType = constructType(tableType);
return DataTableType.defaultEntry(
targetType.getRawClass(),
defaultDataTableEntryTransformer,
tableCellByTypeTransformer
);
}

public void setDefaultDataTableEntryTransformer(TableEntryByTypeTransformer defaultDataTableEntryTransformer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,19 @@ private <T> List<T> toListOrNull(List<List<String>> cells, Type itemType) {
if (cellValueType != null) {
return unpack((List<List<T>>) cellValueType.transform(cells));
}

if (cells.size() > 1) {
DataTableType defaultTableType = registry.getDefaultTableEntryTransformer(itemType);
if (defaultTableType != null) {
return (List<T>) defaultTableType.transform(cells);
}
}

DataTableType defaultCellValueType = registry.getDefaultTableCellTransformer(itemType);
if (defaultCellValueType != null) {
return unpack((List<List<T>>) defaultCellValueType.transform(cells));
}

return null;
}

Expand All @@ -163,6 +176,9 @@ public <T> List<List<T>> toLists(DataTable dataTable, Type itemType) {
}

DataTableType tableType = registry.lookupTableTypeByType(aListOf(aListOf(itemType)));
if (tableType == null) {
tableType = registry.getDefaultTableCellTransformer(itemType);
}
if (tableType != null) {
return unmodifiableList((List<List<T>>) tableType.transform(dataTable.cells()));
}
Expand Down Expand Up @@ -220,6 +236,9 @@ private <K> List<K> convertEntryKeys(Type keyType, List<List<String>> keyColumn,
if (firstHeaderCellIsBlank) {
DataTableType keyConverter;
keyConverter = registry.lookupTableTypeByType(aListOf(aListOf(keyType)));
if (keyConverter == null) {
keyConverter = registry.getDefaultTableCellTransformer(keyType);
}
if (keyConverter == null) {
throw mapNoConverterDefined(keyType, valueType, "TableCellTransformer", keyType);
}
Expand Down Expand Up @@ -261,6 +280,20 @@ private <V> List<V> convertEntryValues(DataTable dataTable, Type keyType, Type v
//
// So instead we unroll these steps here. This keeps the error handling and messages sane.

// Handle case #1.
Type listItemType = listItemType(valueType);
if (listItemType != null) {
// Table cell types take priority over default converters
DataTableType cellValueConverter = registry.lookupTableTypeByType(aListOf(aListOf(listItemType)));
if (cellValueConverter == null) {
cellValueConverter = registry.getDefaultTableCellTransformer(listItemType);
}
if (cellValueConverter == null) {
throw mapNoConverterDefined(keyType, valueType, "TableCellTransformer", valueType);
}
return (List<V>) cellValueConverter.transform(dataTable.cells());
}

// Handle case #2
Type valueMapKeyType = mapKeyType(valueType);
if (valueMapKeyType != null) {
Expand All @@ -270,21 +303,32 @@ private <V> List<V> convertEntryValues(DataTable dataTable, Type keyType, Type v
return (List<V>) toMaps(dataTable, String.class, String.class);
}

// Try to handle case #3. We are required to check the most specific solution first.
// Try to handle case #3.
// We check this regardless of the keys. They may not imply that this is a table entry.
// But this type was registered as such.
DataTableType entryValueConverter = registry.lookupTableTypeByType(aListOf(valueType));
if (entryValueConverter != null) {
return (List<V>) entryValueConverter.transform(dataTable.cells());
}

if (keysImplyTableEntryTransformer) {
// There is no way around it though. This is probably a table entry.
DataTableType defaultEntryValueConverter = registry.getDefaultTableEntryTransformer(valueType);
if (defaultEntryValueConverter != null) {
return (List<V>) defaultEntryValueConverter.transform(dataTable.cells());
}
throw keysImplyTableEntryTransformer(keyType, valueType);
}

// Try to handle case #1. This may result in multiple values per key if the table is too wide.
// This may result in multiple values per key if the table is too wide.
DataTableType cellValueConverter = registry.lookupTableTypeByType(aListOf(aListOf(valueType)));
if (cellValueConverter != null) {
return unpack((List<List<V>>) cellValueConverter.transform(dataTable.cells()));
}
DataTableType defaultCellValueConverter = registry.getDefaultTableCellTransformer(valueType);
if (defaultCellValueConverter != null) {
return unpack((List<List<V>>) defaultCellValueConverter.transform(dataTable.cells()));
}

throw mapNoConverterDefined(keyType, valueType, "TableEntryTransformer or TableCellTransformer", valueType);
}
Expand Down
Loading