-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JsonObject ValueResolver to Qute
- Loading branch information
Showing
5 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/JsonObjectProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
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; | ||
|
||
public class JsonObjectProcessor { | ||
|
||
@BuildStep | ||
void init(Capabilities capabilities, BuildProducer<AdditionalBeanBuildItem> beans) { | ||
if (capabilities.isPresent(Capability.VERTX)) { | ||
beans.produce(new AdditionalBeanBuildItem("io.quarkus.qute.runtime.jsonobject.JsonObjectConfigurator")); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...ment/src/test/java/io/quarkus/qute/deployment/jsonobject/JsonObjectValueResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...qute/runtime/src/main/java/io/quarkus/qute/runtime/jsonobject/JsonObjectConfigurator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.quarkus.qute.runtime.jsonobject; | ||
|
||
import jakarta.enterprise.event.Observes; | ||
|
||
import io.quarkus.qute.EngineBuilder; | ||
|
||
public class JsonObjectConfigurator { | ||
|
||
/** | ||
* Configure the engine to use the {@link JsonObjectValueResolver}. | ||
* | ||
* @param builder the engine builder. | ||
*/ | ||
void configureEngine(@Observes EngineBuilder builder) { | ||
builder.addValueResolver(new JsonObjectValueResolver()); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...ute/runtime/src/main/java/io/quarkus/qute/runtime/jsonobject/JsonObjectValueResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.quarkus.qute.runtime.jsonobject; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
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}. | ||
*/ | ||
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); | ||
} | ||
} | ||
} |