davsclaus opened a new pull request, #27585: URL: https://github.com/apache/camel/pull/27585
## Summary Fixes [CAMEL-25470](https://issues.apache.org/jira/browse/CAMEL-25470). Follow-up to CAMEL-25324 and CAMEL-25323. The DataWeave `$` lambda shorthand was only converted correctly when `$` was the **whole** lambda body. Anything else was emitted without a TODO, so the new fail-fast check in `DatasonnetLanguage` did not catch it. Output on `main` before this change: | DataWeave | Emitted (todo=0) | |---|---| | `payload.items map $.@id` | `std.map(function(x) x., body.items)` (invalid Jsonnet) | | `payload.items map $.*Item` | `std.map(function(x) x., body.items) * Item` | | `payload.items reduce ($$ + $.*price)` | `acc + item * price` (wrong result) | | `payload.items map { id: $.id, name: $.name }` | `std.map({id: function(x) x.id, ...}, ...)` | | `payload.items map upper($.name)` | `std.map(std.asciiUpper(function(x) x.name), ...)` | | `payload.items orderBy -$.price` | `c.sortBy(body.items, -function(x) x.price)` | | `payload.items distinctBy $.id` | `c.distinct(body.items)` (key dropped) | Also, `payload.items filter $.price?` applied `?` to the whole filter, `reduce ($$ + $.@qty)` failed to parse, and reduce shorthand bodies with `if`/`else`, `default` or function calls were emitted as TODO. ## Changes - **Parser** (`parseDollarShorthand`): `.@attr`, `.*field`, `?` and `[index]` after `$` now bind to `$`. Before, the `.` was consumed and the rest was dropped or bound to the map/filter result. - **Converter**: a shorthand lambda is now emitted as `function(x) <body>`, with `$` resolving to `x` anywhere in the body (objects, function calls, selectors, if/else, default, ...). - `$$` is the accumulator in `reduce` and the index in `map` (emitted as `std.mapWithIndex`). - It stays a TODO elsewhere (for example `filter`), where it is not supported. - A nested shorthand lambda gets its own scope, so in `payload.orders map { skus: $.lines map $.sku }` the inner `$` is the line. - This replaces the reduce-only `emitReduceShorthandBody`. `containsShorthand` now walks all record components instead of a hand-maintained list of node types. - `distinctBy` with a shorthand key now uses `c.distinctBy`. - Docs: "Supported Constructs" mentions the shorthand (catalog copy synced). After the change: ``` payload.items map { id: $.id, name: upper($.name) } -> std.map(function(x) {id: x.id, name: std.asciiUpper(x.name)}, body.items) payload.items map { line: $$, sku: $.@sku } -> std.mapWithIndex(function(i, x) {line: i, sku: x["@sku"]}, body.items) payload.items orderBy -$.price -> c.sortBy(body.items, function(x) -x.price) payload.items filter $.a.b? -> std.filter(function(x) std.objectHas(x.a, "b"), body.items) ``` ## Tests - `DataWeaveConverterTest`: 14 new tests, one per case above plus nested scopes and `$$` in `filter` staying a TODO. - `DataWeaveExecutedCorpusTest`: new `shorthand.dwl` executed through a real DataSonnet route, checking the JSON output (map with object + function call, `$$` index, `orderBy -$.price`, `filter $.x?`, `distinctBy $.@sku`, `reduce ($$ + $)`). - camel-dataweave 131/131, camel-datasonnet 80/80. ## Not in this PR Found while probing; to be handled separately: - `payload map (item) -> item.a` (lambda without outer parentheses) becomes `std.map((item), body)`, silently dropping the rest. - `payload.items filter $.price > 10` (no parentheses): `> 10` is applied to the filter result. This depends on DataWeave's precedence of `filter` vs `>`, which needs checking. _Claude Code on behalf of davsclaus_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- 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]
