davsclaus opened a new pull request, #3340:
URL: https://github.com/apache/camel-kamelets/pull/3340
Two new action Kamelets for CSV, built on the Camel `csv` data format
(camel-csv, Apache Commons CSV). With them, the file sources and sinks
(`ftp-*`, `sftp-*`, `aws-s3-*`, …) can work with CSV without any code.
### `csv-deserialize-action`
This action turns CSV text (as text, bytes or a stream, read as UTF-8) into
a `List` of rows.
- With `header` true (the default) or `columns` set, each row is a
`LinkedHashMap` from the column name to the value, in column order. A
`List<Map>` body means `json-serialize-action` can follow directly and gives a
JSON array of objects. Without a header line or column names, each row is a
`List` of the values. All values are text.
- The `Content-Type` header is removed.
- Options: `delimiter` (`,`), `header` (true), `columns` (comma-separated
names; they replace the header line, or name the columns of a file without
one), `quote` (`"`), `skipEmptyLines` (true), `trim` (false).
- `in` default `text` (`text-plain`). `out` has no default, because no
transformer keeps a List of Maps.
### `csv-serialize-action`
This action turns rows into CSV text, as a String, with CRLF line ends and
minimal quoting.
- Input: a `List` of Maps or of Lists, a single `Map`, or parsed JSON (a
Jackson `JsonNode` from `json-deserialize-action`). The template turns parsed
JSON into Maps and Lists with Jackson first.
- `Content-Type` is set to `text/csv`.
- Options: `delimiter` (`,`), `header` (true), `columns` (the column order,
and the names on the header line), `quote` (`"`).
- When `columns` is set, Map rows give the values of those keys in that
order. Other keys are left out, and a missing key is an empty value.
- camel-csv writes a header line only for given columns. When `columns` is
not set, the template writes the keys of the first Map row as the header line
itself: it marshals them into a variable and puts that line before the body. A
key that only a later row has gets a column with no name on the header line.
The description says so.
### Design points
- **Tab delimiter.** A tab cannot be given as a `kamelet:` parameter in a
route. As the last parameter it is trimmed, and anywhere else the URI fails
with `Illegal character`. `%09` is not decoded either, because the kamelet
component takes the parameters raw. Setting `camel.kamelet.<name>.delimiter=\t`
as a property works. The descriptions say so, and the tab test does it this way.
- `delimiter` and `quote` are single characters, because camel-csv binds
them to a `Character`.
- `columns` has a `pattern` that rejects `'`, because the template puts the
option inside a quoted `simple` text when it checks whether it is empty.
- Security: neither template reads or forwards a message header. Message
data is never evaluated: header keys such as `${header.x}` are written as text,
which I checked.
### Tests
The tests are in `tests/camel-kamelets-itest/src/test/resources/csv/`, run
by the new `CsvIT` class. All checks are log checks, with no containers and no
ports.
- `csv-deserialize-action-test`:
- A header file read from bytes. It has a quoted value with the delimiter,
doubled quotes, a line break inside quotes, CRLF line ends, an empty line, a
short row and non-ASCII text.
- A file without a header line.
- The `;` delimiter with given column names and `trim`.
- Column names that replace a header line.
- The `'` quote character with `skipEmptyLines: false`.
- Empty text, and text with only a header line: both give `[]`.
- An unclosed quote fails the message.
- `csv-serialize-action-test`:
- A round trip: the text read and written back is exactly the original,
and the result is a String with `Content-Type: text/csv`.
- A single Map.
- `columns` order with `header: false`.
- List rows with `;` and `'`.
- Parsed JSON: nested object, null, and a key that only the second row has.
- An empty List, and an empty List with `columns` (header line only).
- A body of text fails the message.
- `csv-tab-test`: tab-separated values read and written, with the tab set
through a properties resource.
Local results:
- `./mvnw -o clean install -DskipITs` passes.
- `./mvnw -o verify -pl tests/camel-kamelets-itest -Dit.test=CsvIT`: 3
tests, 3 passed.
Fixes #3328.
### Upgrade note
- New Kamelets `csv-deserialize-action` (CSV text to a List of rows, each a
Map keyed by the column names or a List of values) and `csv-serialize-action`
(a List of Maps or Lists, a Map or parsed JSON to CSV text with a header line,
`Content-Type: text/csv`), built on the Camel `csv` data format.
_Claude Code on behalf of Claus Ibsen_
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_016Y4dDmBDsSWV117kccuNf9
--
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]