Skip to content

Commit d9d1a6b

Browse files
authored
Allow to iterate on JsonArray of JsonObject (#676)
1 parent 2ce2e70 commit d9d1a6b

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

blog/content/docs/basics.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,10 @@ Can be access with `{cdi:foo.bar}` in any template.
458458

459459
== Template Extensions
460460

461-
The Qute templating language supports a concept called https://quarkus.io/guides/qute#template-extension-methods[template extension methods]. These methods allow us to add functional to types and expose it in our templates. Moving logic from the template to Java code gives us the ability to use a more robust language for more complex logic and makes the functionality more easily reusable,as well as keeping our templates clean. Roq has several built-in template extensions (link:{roqjavadocmodelurl}RoqTemplateExtension.html[javadoc,window=_blank]):
461+
The Qute templating language supports a concept called https://quarkus.io/guides/qute#template-extension-methods[template extension methods]. These methods allow us to add functional to types and expose it in our templates. Moving logic from the template to Java code gives us the ability to use a more robust language for more complex logic and makes the functionality more easily reusable,as well as keeping our templates clean:
462+
463+
- Qute provides https://quarkus.io/guides/qute-reference#built-in-template-extension[built-in template extensions,window=_blank]
464+
- Roq has several built-in template extensions (link:{roqjavadocmodelurl}RoqTemplateExtension.html[javadoc,window=_blank]):
462465

463466
[cols="1,2", options="header"]
464467
|===
@@ -488,14 +491,11 @@ The Qute templating language supports a concept called https://quarkus.io/guides
488491
| ``page.contentAbstract(limit)``
489492
| Returns the page content limited to `limit` words
490493

491-
| ``list.limit(n)``
492-
| Returns only the first `n` items of the list
493-
494494
| ``list.randomise``
495495
| Returns the list in random order
496496

497-
| ``list.reverse``
498-
| Returns the list in reverse order
497+
| ``jsonArray.asJsonObjects``
498+
| Returns a list of JsonObject. All items must be JSON objects.
499499

500500
| ``collections.collection(key)``
501501
| Returns the collection for the given key

roq-data/deployment/src/main/java/io/quarkiverse/roq/data/deployment/RoqDataBeanProcessor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ FeatureBuildItem feature() {
3434
@BuildStep
3535
@Record(ExecutionTime.STATIC_INIT)
3636
void generateSyntheticBeans(RoqDataConfig config,
37+
3738
BuildProducer<SyntheticBeanBuildItem> beansProducer,
3839
List<RoqDataJsonBuildItem> roqDataJsonBuildItems,
3940
List<RoqDataBeanBuildItem> dataBeanBuildItems,

roq-frontmatter/runtime/src/main/java/io/quarkiverse/roq/frontmatter/runtime/RoqTemplateExtension.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.quarkus.qute.TemplateExtension;
1616
import io.vertx.core.http.impl.MimeMapping;
1717
import io.vertx.core.json.JsonArray;
18+
import io.vertx.core.json.JsonObject;
1819

1920
@TemplateExtension
2021
public class RoqTemplateExtension {
@@ -152,11 +153,18 @@ public static String mimeType(String fileName) {
152153
}
153154

154155
/**
155-
* Returns the same list with a limited number of items.<br>
156-
* Example: "{list.limit(5)}" → Only the first 5 items of the list.
156+
* Returns a list of JsonObject. All items must be Json objects.
157157
*/
158-
public static <T> List<T> limit(List<T> list, int limit) {
159-
return list.subList(0, Math.min(limit, list.size()));
158+
@SuppressWarnings({ "unchecked", "rawtypes" })
159+
public static List<JsonObject> asJsonObjects(JsonArray jsonArray) {
160+
return jsonArray.stream().map(item -> {
161+
if (item instanceof JsonObject o) {
162+
return o;
163+
} else {
164+
throw new RuntimeException(
165+
"asJsonObjects should only be called if all items are instance of JsonObject (not: %s)".formatted(item));
166+
}
167+
}).toList();
160168
}
161169

162170
/**
@@ -168,15 +176,6 @@ public static <T> List<T> randomise(List<T> l) {
168176
return list;
169177
}
170178

171-
/**
172-
* Returns a new list with the elements of the given list in reverse order.
173-
*/
174-
public static <T> List<T> reverse(List<T> l) {
175-
List<T> copy = new ArrayList<>(l);
176-
Collections.reverse(copy);
177-
return copy;
178-
}
179-
180179
/**
181180
* Returns a new list with all the documents that matches the given value for the key (in the FM data).<br>
182181
* Example: "{list.filter('author', 'john')}" → Only the documents where the author is john.

0 commit comments

Comments
 (0)