-
-
Notifications
You must be signed in to change notification settings - Fork 687
[datatable] Add ability to register default transformers for table cell and entry #429
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
datatable/java/datatable/src/main/java/io/cucumber/datatable/CamelCaseStringConverter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.cucumber.datatable; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
class CamelCaseStringConverter { | ||
|
||
private static final String WHITESPACE = " "; | ||
private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+"); | ||
|
||
public String map(String string) { | ||
String[] splitted = normalizeSpace(string).split(WHITESPACE); | ||
splitted[0] = uncapitalize(splitted[0]); | ||
for (int i = 1; i < splitted.length; i++) { | ||
splitted[i] = capitalize(splitted[i]); | ||
} | ||
return join(splitted); | ||
} | ||
|
||
private String join(String[] splitted) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (String s : splitted) { | ||
sb.append(s); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private String normalizeSpace(String originalHeaderName) { | ||
return WHITESPACE_PATTERN.matcher(originalHeaderName.trim()).replaceAll(WHITESPACE); | ||
} | ||
|
||
private String capitalize(String string) { | ||
return String.valueOf(Character.toTitleCase(string.charAt(0))) + string.substring(1); | ||
} | ||
|
||
private String uncapitalize(String string) { | ||
if (string.isEmpty()) { | ||
throw new CucumberDataTableException("Field name cannot be empty. Please check the table header."); | ||
} | ||
return String.valueOf(Character.toLowerCase(string.charAt(0))) + string.substring(1); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...le/java/datatable/src/main/java/io/cucumber/datatable/DataTableCellByTypeTransformer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.cucumber.datatable; | ||
|
||
import java.util.List; | ||
|
||
import static io.cucumber.datatable.TypeFactory.aListOf; | ||
import static java.util.Collections.singletonList; | ||
|
||
class DataTableCellByTypeTransformer implements TableCellByTypeTransformer { | ||
|
||
private DataTableTypeRegistry dataTableTypeRegistry; | ||
|
||
DataTableCellByTypeTransformer(DataTableTypeRegistry dataTableTypeRegistry) { | ||
this.dataTableTypeRegistry = dataTableTypeRegistry; | ||
} | ||
|
||
@Override | ||
public <T> T transform(String value, Class<T> cellType) throws Throwable { | ||
//noinspection unchecked | ||
DataTableType typeByType = dataTableTypeRegistry.lookupTableTypeByType(aListOf(aListOf(cellType))); | ||
if (typeByType == null) { | ||
throw new CucumberDataTableException("There is no DataTableType registered for cell type "+ cellType); | ||
} | ||
//noinspection unchecked | ||
return ((List<List<T>>)typeByType.transform(singletonList(singletonList(value)))).get(0).get(0); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...java/datatable/src/main/java/io/cucumber/datatable/SimpleTableEntryByTypeTransformer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package io.cucumber.datatable; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.Map; | ||
|
||
/** | ||
* {@link TableEntryByTypeTransformer} based on reflection. | ||
* <p> | ||
* For example: | ||
* | ||
* <pre> | ||
* | firstName | last name | age | | ||
* | Annie M. G. | Schmidt | 20 | | ||
* </pre> | ||
* can be transformed to class: | ||
* <pre> | ||
* | ||
* public class User { | ||
* | ||
* String firstName; | ||
* String lastName; | ||
* int age; | ||
* | ||
* public User() { | ||
* | ||
* } | ||
* } | ||
* | ||
* </pre> | ||
*/ | ||
public class SimpleTableEntryByTypeTransformer implements TableEntryByTypeTransformer { | ||
|
||
public interface InstanceCreator { | ||
|
||
<T> T createInstance(Class<T> type) throws Throwable; | ||
} | ||
|
||
public static class DefaultInstanceCreator implements InstanceCreator { | ||
|
||
@Override | ||
public <T> T createInstance(Class<T> type) throws Throwable { | ||
return type.newInstance(); | ||
} | ||
} | ||
|
||
private CamelCaseStringConverter fieldNameConverter = new CamelCaseStringConverter(); | ||
private InstanceCreator instanceCreator; | ||
|
||
public SimpleTableEntryByTypeTransformer(InstanceCreator instanceCreator) { | ||
this.instanceCreator = instanceCreator; | ||
} | ||
|
||
@Override | ||
public <T> T transform(Map<String, String> value, Class<T> type, TableCellByTypeTransformer cellTransformer) throws Throwable { | ||
|
||
T instance = instanceCreator.createInstance(type); | ||
Field[] fields = type.getDeclaredFields(); | ||
for (Map.Entry<String, String> entry : value.entrySet()) { | ||
String key = entry.getKey(); | ||
String cell = entry.getValue(); | ||
for (Field field : fields) { | ||
if (fieldNameConverter.map(key).equals(field.getName())) { | ||
field.setAccessible(true); | ||
field.set(instance, cellTransformer.transform(cell, field.getType())); | ||
break; | ||
} | ||
} | ||
} | ||
return instance; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
datatable/java/datatable/src/main/java/io/cucumber/datatable/TableCellByTypeTransformer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.cucumber.datatable; | ||
|
||
/** | ||
* Transformer for single cell. Similar to {@link TableCellTransformer} but additionally it receives expected {@code Class<T>} of cell. | ||
* @see TableCellTransformer | ||
*/ | ||
public interface TableCellByTypeTransformer { | ||
|
||
/** | ||
* Transforms single cell to type {@code T} | ||
* @param value cell | ||
* @param cellType expected cell type | ||
* @param <T> see {@code cellType} | ||
* @return an instance of {@code T} | ||
*/ | ||
<T> T transform(String value, Class<T> cellType) throws Throwable; | ||
} |
21 changes: 21 additions & 0 deletions
21
...table/java/datatable/src/main/java/io/cucumber/datatable/TableEntryByTypeTransformer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.cucumber.datatable; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Default transformer for entries which don't have registered {@link DataTableType} in {@code TypeRegistry}. Similar to {@link TableEntryTransformer} but additionally it receives {@code Class<T>} of expected object and {@link TableCellByTypeTransformer} for transforming individual cells from {@code String} to arbitrary type. | ||
* @see TableEntryTransformer | ||
*/ | ||
public interface TableEntryByTypeTransformer { | ||
|
||
/** | ||
* This method should transform row represented by key-value map to object of type {@code type} | ||
* @param entry table entry, key - column name, value - cell | ||
* @param type type of an expected object to return | ||
* @param cellTransformer cell transformer | ||
* @param <T> see {@code type} | ||
* @return new instance of {@code type} | ||
*/ | ||
<T> T transform(Map<String, String> entry, Class<T> type, TableCellByTypeTransformer cellTransformer) throws Throwable; | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat!