Skip to content

Commit 569d288

Browse files
committed
Add support for evaluating module output and expressions to ConfigEvaluator
1 parent 4c13952 commit 569d288

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

pkl-config-java/src/main/java/org/pkl/config/java/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ static Config fromPklBinary(InputStream inputStream) {
110110
return fromPklBinary(inputStream, ValueMapper.preconfigured());
111111
}
112112

113-
private static Config makeConfig(Object decoded, ValueMapper mapper) {
113+
static Config makeConfig(Object decoded, ValueMapper mapper) {
114114
if (decoded instanceof Composite composite) {
115115
return new CompositeConfig("", mapper, composite);
116116
}

pkl-config-java/src/main/java/org/pkl/config/java/ConfigEvaluator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ static ConfigEvaluator preconfigured() {
4141
/** Evaluates the given module source into a {@link Config} tree. */
4242
Config evaluate(ModuleSource moduleSource);
4343

44+
/** Evaluates the given module's {@code output.value} property into a {@link Config} tree. */
45+
Config evaluateOutputValue(ModuleSource moduleSource);
46+
47+
/** Evaluates the Pkl expression represented as {@code expression} into a {@link Config} tree. */
48+
Config evaluateExpression(ModuleSource moduleSource, String expression);
49+
4450
/**
4551
* Releases all resources held by this evaluator. If an {@code evaluate} method is currently
4652
* executing, this method blocks until cancellation of that execution has completed.

pkl-config-java/src/main/java/org/pkl/config/java/ConfigEvaluatorImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ public Config evaluate(ModuleSource moduleSource) {
3434
return new CompositeConfig("", mapper, module);
3535
}
3636

37+
@Override
38+
public Config evaluateOutputValue(ModuleSource moduleSource) {
39+
var value = evaluator.evaluateOutputValue(moduleSource);
40+
return Config.makeConfig(value, mapper);
41+
}
42+
43+
@Override
44+
public Config evaluateExpression(ModuleSource moduleSource, String expression) {
45+
var value = evaluator.evaluateExpression(moduleSource, expression);
46+
return Config.makeConfig(value, mapper);
47+
}
48+
3749
@Override
3850
public ValueMapper getValueMapper() {
3951
return mapper;

pkl-config-java/src/test/java/org/pkl/config/java/ConfigTest.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@
1515
*/
1616
package org.pkl.config.java;
1717

18+
import static org.assertj.core.api.Assertions.assertThat;
1819
import static org.pkl.core.ModuleSource.text;
1920

21+
import org.junit.jupiter.api.Test;
22+
2023
public class ConfigTest extends AbstractConfigTest {
2124
private static final ConfigEvaluator evaluator = ConfigEvaluator.preconfigured();
25+
26+
private static final String pigeonText = "pigeon { age = 30; friends = List(\"john\", \"mary\"); address { street = \"Fuzzy St.\" } }";
2227

2328
@Override
2429
protected Config getPigeonConfig() {
2530
return evaluator.evaluate(
26-
text(
27-
"pigeon { age = 30; friends = List(\"john\", \"mary\"); address { street = \"Fuzzy St.\" } }"));
31+
text(pigeonText));
2832
}
2933

3034
@Override
@@ -42,4 +46,18 @@ protected Config getPairConfig() {
4246
protected Config getMapConfig() {
4347
return evaluator.evaluate(text("x = Map(\"one\", 1, \"two\", 2)"));
4448
}
49+
50+
@Test
51+
public void evaluateOutputValue() {
52+
var valueConfig = evaluator.evaluateOutputValue(text(pigeonText+"\noutput { value = (outer) { pigeon { age = 99 } } }"));
53+
var pigeon = valueConfig.get("pigeon").as(Person.class);
54+
assertThat(pigeon.age).isEqualTo(99);
55+
}
56+
57+
@Test
58+
public void evaluateExpression() {
59+
var addressConfig = evaluator.evaluateExpression(text(pigeonText), "pigeon.address");
60+
var address = addressConfig.as(Address.class);
61+
assertThat(address.street).isEqualTo("Fuzzy St.");
62+
}
4563
}

0 commit comments

Comments
 (0)