gnodet-bot commented on code in PR #30588:
URL: https://github.com/apache/camel/pull/30588#discussion_r4657484017
##########
components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvMarshaller.java:
##########
@@ -213,5 +216,90 @@ protected Iterable<?> getMapRecordValues(Map<?, ?> map) {
return createMapRecordValues().apply(map);
}
}
+
+ /**
+ * This marshaller writes a header line with the keys of the first map
row, and these keys are the columns of all
+ * the rows, as with a configured header. A body whose first row is not a
map, or is an empty map, is written as by
+ * the marshaller without a header.
+ */
+ private static final class MapKeysHeaderMarshaller extends CsvMarshaller {
+ private final CSVFormat format;
Review Comment:
💡 **Shadowed field**: `MapKeysHeaderMarshaller` declares its own `private
final CSVFormat format` which shadows the `format` field in the parent class
`CsvMarshaller`. Since both are `private`, the parent's field is inaccessible
here and the separate field is necessary in practice. No action required; just
noting the redundancy.
##########
components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvMarshaller.java:
##########
@@ -213,5 +216,90 @@ protected Iterable<?> getMapRecordValues(Map<?, ?> map) {
return createMapRecordValues().apply(map);
}
}
+
+ /**
+ * This marshaller writes a header line with the keys of the first map
row, and these keys are the columns of all
+ * the rows, as with a configured header. A body whose first row is not a
map, or is an empty map, is written as by
+ * the marshaller without a header.
+ */
+ private static final class MapKeysHeaderMarshaller extends CsvMarshaller {
+ private final CSVFormat format;
+
+ private MapKeysHeaderMarshaller(CSVFormat format) {
+ super(format);
+ this.format = format;
+ }
+
+ @Override
+ public void marshal(Exchange exchange, Object object, OutputStream
outputStream)
+ throws NoTypeConversionAvailableException, IOException {
+ Object body = object;
+ Object first = object;
+ if (!(object instanceof Map)) {
+ // take the first row, and put it back in front of the others
(the body can be an iterator)
+ Iterator<?> it = ObjectHelper.createIterator(object);
+ first = it.hasNext() ? it.next() : null;
+ body = first != null ? prepend(first, it) : object;
+ }
+ Map<?, ?> map = first != null
+ ?
exchange.getContext().getTypeConverter().tryConvertTo(Map.class, exchange,
first) : null;
+ if (map == null || map.isEmpty()) {
+ new DynamicColumnsMarshaller(format).marshal(exchange, body,
outputStream);
+ return;
+ }
+ List<Object> columns = new ArrayList<>(map.keySet());
+ String[] header =
columns.stream().map(String::valueOf).toArray(String[]::new);
+ // the printer writes the header line of the format first
+ new KeyColumnsMarshaller(format.builder().setHeader(header).get(),
columns)
+ .marshal(exchange, body, outputStream);
+ }
+
+ private static Iterator<Object> prepend(Object first, Iterator<?>
rest) {
+ return new Iterator<>() {
+ private boolean firstTaken;
+
+ @Override
+ public boolean hasNext() {
+ return !firstTaken || rest.hasNext();
+ }
+
+ @Override
+ public Object next() {
+ if (!firstTaken) {
+ firstTaken = true;
+ return first;
+ }
+ return rest.next();
+ }
+ };
+ }
+
+ @Override
+ protected Iterable<?> getMapRecordValues(Map<?, ?> map) {
+ // a map on its own: its keys are the columns
+ return new ArrayList<>(map.values());
+ }
Review Comment:
💡 **Dead code body (compiler-mandated override)**: This method body is
unreachable. `MapKeysHeaderMarshaller.marshal()` always delegates to
`DynamicColumnsMarshaller` or `KeyColumnsMarshaller` and never falls through to
the parent's `marshal()` → `createMapRecordValues()` → `getMapRecordValues()`
chain. The override is required because `getMapRecordValues` is `abstract`, but
the return value is never used. Consider adding a comment like `// never
called; override required because the method is abstract in the parent` for
clarity.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]