Skip to content

Commit

Permalink
Merge pull request #251 from eclipse/thomas/fix-csv-var-path
Browse files Browse the repository at this point in the history
Small fix to ensure header-less CSV paths are used as integers
  • Loading branch information
tcalmant authored Oct 9, 2023
2 parents b49aefe + 70ee7d9 commit 71ea85f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public List<? extends IDeviceMappingRecord> parseRecords(byte[] rawInput, Map<St
final List<CsvRecord> records = new ArrayList<>();
try (CSVParser parser = format.build().parse(new InputStreamReader(input, charset))) {
for (CSVRecord record : parser) {
records.add(new CsvRecord(record));
records.add(new CsvRecord(record, withHeader != null && !withHeader));
}
} catch (IllegalStateException | IOException e) {
throw new ParserException("Error reading CSV content", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,20 @@ public class CsvRecord implements IDeviceMappingRecord {
*/
private final CSVRecord record;

/**
* Flag to indicate the paths must all be a column index
*/
private final boolean columnIndexOnly;

/**
* Sets up the CSV record
*
* @param record Current parsed CSV record
* @param record Current parsed CSV record
* @param columnIndexOnly If true, all paths are considered column indices
*/
public CsvRecord(final CSVRecord record) {
public CsvRecord(final CSVRecord record, final boolean columnIndexOnly) {
this.record = record;
this.columnIndexOnly = columnIndexOnly;
}

/**
Expand All @@ -48,7 +55,7 @@ public CsvRecord(final CSVRecord record) {
*/
private String getValue(final RecordPath path) {
try {
if (path.isInt()) {
if (path.isInt() || columnIndexOnly) {
return this.record.get(path.asInt());
} else {
return this.record.get(path.asString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,27 @@ void testIsolatedValueTyped() throws Exception {
// CSV loads strings by default
assertEquals(37.5f, getResourceValue(provider, "data", "value", Float.class));
}

/**
* Mapping based on variables in multiple places
*/
@Test
void testVariables() throws Exception {
// Excepted resource
final String provider = "provider-vars-" + String.valueOf(new Random().nextInt());
final String service = "svc-vars-" + String.valueOf(new Random().nextInt());
final String resource = "rc-vars-" + String.valueOf(new Random().nextInt());

// Read the configuration
DeviceMappingConfigurationDTO config = readConfiguration("csv/csv-no-header-vars-mapping.json");

// Read the file
byte[] fileContent = readFile("csv/csv-no-header-vars.csv");

// Apply mapping
deviceMapper.handle(config, Map.of("provider", provider, "svc", service, "rc", resource), fileContent);

assertEquals("3.4", getResourceValue(provider, service, "a", String.class));
assertEquals("42", getResourceValue(provider, service, "b", String.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"parser": "csv",
"parser.options": {
"header": false
},
"mapping": {
"@provider": {
"literal": "${context.provider}"
},
"$svcName": "${context.svc}",
"@datetime": 0,
"$rcName": 1,
"$rcValue": {
"path": 2,
"type": "int"
},
"${svcName}/${rcName}": {
"path": "${rcValue}"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2023-10-09T08:14:00.0+02:00,a,4,1.2,3.4,21
2023-10-09T08:17:00.0+02:00,b,5,5.6,7.8,42

0 comments on commit 71ea85f

Please sign in to comment.