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

Add JsonObjectValueResolver to qute #41403

Merged
merged 1 commit into from
Jun 28, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.quarkus.qute.deployment;

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.qute.runtime.jsonobject.JsonObjectValueResolver;

public class JsonObjectProcessor {

@BuildStep
void init(Capabilities capabilities, BuildProducer<AdditionalBeanBuildItem> beans) {
if (capabilities.isPresent(Capability.VERTX)) {
beans.produce(new AdditionalBeanBuildItem(JsonObjectValueResolver.class));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.quarkus.qute.deployment.jsonobject;

import java.util.HashMap;

import jakarta.inject.Inject;

import org.assertj.core.api.Assertions;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.qute.Template;
import io.quarkus.test.QuarkusUnitTest;
import io.vertx.core.json.JsonObject;

public class JsonObjectValueResolverTest {

@RegisterExtension
static final QuarkusUnitTest quarkusApp = new QuarkusUnitTest()
.withApplicationRoot(
app -> app.addAsResource(new StringAsset(
"{tool.name} {tool.fieldNames} {tool.fields} {tool.size} {tool.empty} {tool.isEmpty} {tool.get('name')} {tool.containsKey('name')}"),
"templates/foo.txt"));

@Inject
Template foo;

@Test
void testJsonObjectValueResolver() {
HashMap<String, Object> toolMap = new HashMap<>();
toolMap.put("name", "Roq");
JsonObject jsonObject = new JsonObject(toolMap);
String render = foo.data("tool", jsonObject).render();

Assertions.assertThat(render).isEqualTo("Roq [name] [name] 1 false false Roq true");
}
}
5 changes: 5 additions & 0 deletions extensions/qute/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
<artifactId>quarkus-cache</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.quarkus.qute.runtime.jsonobject;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

import io.quarkus.qute.EngineConfiguration;
import io.quarkus.qute.EvalContext;
import io.quarkus.qute.Results;
import io.quarkus.qute.ValueResolver;
import io.vertx.core.json.JsonObject;

/**
* A value resolver for {@link JsonObject}.
*/
@EngineConfiguration
public class JsonObjectValueResolver implements ValueResolver {

@Override
public boolean appliesTo(EvalContext context) {
return ValueResolver.matchClass(context, JsonObject.class);
}

@Override
public CompletionStage<Object> resolve(EvalContext context) {

JsonObject jsonObject = (JsonObject) context.getBase();
switch (context.getName()) {
case "fieldNames":
case "fields":
return CompletableFuture.completedFuture(jsonObject.fieldNames());
case "size":
return CompletableFuture.completedFuture(jsonObject.size());
case "empty":
case "isEmpty":
return CompletableFuture.completedFuture(jsonObject.isEmpty());
case "get":
if (context.getParams().size() == 1) {
return context.evaluate(context.getParams().get(0)).thenCompose(k -> {
return CompletableFuture.completedFuture(jsonObject.getValue((String) k));
});
}
case "containsKey":
if (context.getParams().size() == 1) {
return context.evaluate(context.getParams().get(0)).thenCompose(k -> {
return CompletableFuture.completedFuture(jsonObject.containsKey((String) k));
});
}
default:
return jsonObject.containsKey(context.getName())
? CompletableFuture.completedFuture(jsonObject.getValue(context.getName()))
: Results.notFound(context);
}
}
}