davsclaus commented on code in PR #30588:
URL: https://github.com/apache/camel/pull/30588#discussion_r4657706569
##########
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:
Fixed in 94627b5c611a: the override now throws
`UnsupportedOperationException`, with a comment that it is never called
(`marshal` hands every body to `DynamicColumnsMarshaller` or
`KeyColumnsMarshaller`) and exists only because the method is abstract in the
parent.
_Claude Code on behalf of davsclaus_
##########
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:
Fixed in 94627b5c611a: the private `format` field of
`MapKeysHeaderMarshaller` is gone; `CsvMarshaller` has a protected
`getFormat()` and the nested marshaller uses it.
_Claude Code on behalf of davsclaus_
--
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]