gnodet-bot commented on code in PR #26607:
URL: https://github.com/apache/camel/pull/26607#discussion_r4051507443
##########
dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/TuiToolRegistry.java:
##########
@@ -1708,39 +1708,59 @@ private String callListExamples(Map<String, Object>
args) {
String filter = args.get("filter") instanceof String v ? v : null;
String level = args.get("level") instanceof String v ? v : null;
+ int limit = args.get("limit") instanceof Number n && n.intValue() > 0
? n.intValue() : 50;
List<JsonObject> filtered = catalog;
if (filter != null && !filter.isEmpty()) {
filtered = ExampleHelper.filterExamples(filtered, filter);
}
- if (level != null && !level.isEmpty()) {
- String lowerLevel = level.toLowerCase();
- filtered = filtered.stream()
- .filter(e ->
lowerLevel.equals(e.getStringOrDefault("level", "")))
- .toList();
- }
+ JsonArray groups = new JsonArray();
JsonArray examples = new JsonArray();
- for (JsonObject entry : filtered) {
- JsonObject ex = new JsonObject();
- ex.put("name", entry.getStringOrDefault("name", ""));
- ex.put("title", entry.getStringOrDefault("title", ""));
- ex.put("description", entry.getStringOrDefault("description", ""));
- ex.put("level", entry.getStringOrDefault("level", ""));
- ex.put("category", ExampleHelper.getCategory(entry));
- ex.put("tags", toJsonArray(
- entry.get("tags") instanceof java.util.Collection<?> c
- ? c.stream().map(Object::toString).toList()
- : List.of()));
- ex.put("bundled", ExampleHelper.isBundled(entry));
- ex.put("requiresDocker", ExampleHelper.requiresDocker(entry));
- ex.put("infraServices",
toJsonArray(ExampleHelper.getInfraServices(entry)));
- examples.add(ex);
+ int total = 0;
+ for (Map.Entry<String, List<JsonObject>> group :
ExampleHelper.groupByLevel(filtered).entrySet()) {
+ if (level != null && !level.isEmpty() &&
!group.getKey().equalsIgnoreCase(level)) {
+ continue;
+ }
+ total += group.getValue().size();
+ JsonObject g = new JsonObject();
+ g.put("level", group.getKey());
+ g.put("title", ExampleHelper.getGroupTitle(group.getKey()));
+ g.put("intro", ExampleHelper.getGroupIntro(group.getKey()));
+ g.put("count", group.getValue().size());
+ groups.add(g);
+ for (JsonObject entry : group.getValue()) {
+ if (examples.size() >= limit) {
+ break;
+ }
Review Comment:
⚠️ **`break` makes `limit` per-group, not global — diverges from the other
two implementations.**
`ToolRegistry.list_examples` and `ExampleTools.camel_catalog_examples` both
treat `limit` as a global cap across all groups (they use `continue` / `if
(size < max)` inside the entry loop, so the outer group loop keeps going but no
new entries are added past the cap). Here `break` exits only the inner (entry)
loop and the outer (group) loop continues. With no `level` filter and
`limit=1`, the current code returns one example from each group (11 groups = 11
examples, `count=11`) instead of 1 total.
The test masks this: `Map.of("level", "run", "limit", 1)` passes only one
group through the outer filter, so the `break` exits correctly there. A call
without `level` exposes the divergence.
Fix: replace `break` with `continue` to match the global-cap semantics of
the other two implementations:
```suggestion
if (examples.size() >= limit) {
continue;
}
```
--
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]