Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Qute standalone - list element can be accessed directly via an index #24493

Merged
merged 1 commit into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/qute-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1613,7 +1613,7 @@ TIP: A map value can be also accessed directly: `{map.myKey}`. Use the bracket n
* `takeLast`: Returns the last `n` elements from the given list; throws an `IndexOutOfBoundsException` if `n` is out of range
** `{#for r in recordsList.takeLast(3)}`

TIP: A list element can be accessed directly: `{list.10}` or `{list[10]}`.
TIP: A list element can be accessed directly via an index: `{list.10}` or even `{list[10]}`.

===== Numbers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public CompletionStage<Object> resolve(EvalContext context) {
public static ValueResolver listResolver() {
return new ValueResolver() {

@Override
public int getPriority() {
// Use this resolver before collectionResolver()
return WithPriority.DEFAULT_PRIORITY + 1;
}

public boolean appliesTo(EvalContext context) {
return ValueResolver.matchClass(context, List.class);
}
Expand Down Expand Up @@ -397,7 +403,8 @@ private static CompletionStage<Object> collectionResolveAsync(EvalContext contex

private static CompletionStage<Object> listResolveAsync(EvalContext context) {
List<?> list = (List<?>) context.getBase();
switch (context.getName()) {
String name = context.getName();
switch (name) {
case "get":
if (context.getParams().size() == 1) {
return context.evaluate(context.getParams().get(0))
Expand Down Expand Up @@ -445,7 +452,14 @@ private static CompletionStage<Object> listResolveAsync(EvalContext context) {
});
}
default:
return Results.notFound(context);
// Try to use the name as an index
int index;
try {
index = Integer.parseInt(name);
} catch (NumberFormatException e) {
gastaldi marked this conversation as resolved.
Show resolved Hide resolved
return Results.notFound(context);
}
return CompletedStage.of(list.get(index));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.quarkus.qute;

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import org.junit.jupiter.api.Test;

public class ListResolverTest {

@Test
public void tesResolver() {
List<String> list = List.of("jedna", "dva", "tri");
Engine engine = Engine.builder().addDefaults().build();
assertEquals("3::jedna::jedna::dva::tri",
engine.parse("{list.size}::{list.get(0)}::{list.take(1).0}::{list.takeLast(2).get(0)}::{list.2}")
.data("list", list).render());
assertThatExceptionOfType(TemplateException.class)
.isThrownBy(() -> engine.parse("{list.abc}").data("list", List.of()).render())
.withMessageContaining(
"Property \"abc\" not found on the base object")
.withMessageContaining("in expression {list.abc}");
}

}