diff --git a/pkl-config-java/src/main/java/org/pkl/config/java/Config.java b/pkl-config-java/src/main/java/org/pkl/config/java/Config.java index a8b7e612d..45f59c5db 100644 --- a/pkl-config-java/src/main/java/org/pkl/config/java/Config.java +++ b/pkl-config-java/src/main/java/org/pkl/config/java/Config.java @@ -110,7 +110,7 @@ static Config fromPklBinary(InputStream inputStream) { return fromPklBinary(inputStream, ValueMapper.preconfigured()); } - private static Config makeConfig(Object decoded, ValueMapper mapper) { + static Config makeConfig(Object decoded, ValueMapper mapper) { if (decoded instanceof Composite composite) { return new CompositeConfig("", mapper, composite); } diff --git a/pkl-config-java/src/main/java/org/pkl/config/java/ConfigEvaluator.java b/pkl-config-java/src/main/java/org/pkl/config/java/ConfigEvaluator.java index 12f8a1c0a..b0f7b37cf 100644 --- a/pkl-config-java/src/main/java/org/pkl/config/java/ConfigEvaluator.java +++ b/pkl-config-java/src/main/java/org/pkl/config/java/ConfigEvaluator.java @@ -1,5 +1,5 @@ /* - * Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. + * Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,6 +41,12 @@ static ConfigEvaluator preconfigured() { /** Evaluates the given module source into a {@link Config} tree. */ Config evaluate(ModuleSource moduleSource); + /** Evaluates the given module's {@code output.value} property into a {@link Config} tree. */ + Config evaluateOutputValue(ModuleSource moduleSource); + + /** Evaluates the Pkl expression represented as {@code expression} into a {@link Config} tree. */ + Config evaluateExpression(ModuleSource moduleSource, String expression); + /** * Releases all resources held by this evaluator. If an {@code evaluate} method is currently * executing, this method blocks until cancellation of that execution has completed. diff --git a/pkl-config-java/src/main/java/org/pkl/config/java/ConfigEvaluatorImpl.java b/pkl-config-java/src/main/java/org/pkl/config/java/ConfigEvaluatorImpl.java index 6ade32123..937c0d56a 100644 --- a/pkl-config-java/src/main/java/org/pkl/config/java/ConfigEvaluatorImpl.java +++ b/pkl-config-java/src/main/java/org/pkl/config/java/ConfigEvaluatorImpl.java @@ -1,5 +1,5 @@ /* - * Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. + * Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,18 @@ public Config evaluate(ModuleSource moduleSource) { return new CompositeConfig("", mapper, module); } + @Override + public Config evaluateOutputValue(ModuleSource moduleSource) { + var value = evaluator.evaluateOutputValue(moduleSource); + return Config.makeConfig(value, mapper); + } + + @Override + public Config evaluateExpression(ModuleSource moduleSource, String expression) { + var value = evaluator.evaluateExpression(moduleSource, expression); + return Config.makeConfig(value, mapper); + } + @Override public ValueMapper getValueMapper() { return mapper; diff --git a/pkl-config-java/src/test/java/org/pkl/config/java/ConfigTest.java b/pkl-config-java/src/test/java/org/pkl/config/java/ConfigTest.java index dd00a2636..c03c0e149 100644 --- a/pkl-config-java/src/test/java/org/pkl/config/java/ConfigTest.java +++ b/pkl-config-java/src/test/java/org/pkl/config/java/ConfigTest.java @@ -15,16 +15,20 @@ */ package org.pkl.config.java; +import static org.assertj.core.api.Assertions.assertThat; import static org.pkl.core.ModuleSource.text; +import org.junit.jupiter.api.Test; + public class ConfigTest extends AbstractConfigTest { private static final ConfigEvaluator evaluator = ConfigEvaluator.preconfigured(); + private static final String pigeonText = + "pigeon { age = 30; friends = List(\"john\", \"mary\"); address { street = \"Fuzzy St.\" } }"; + @Override protected Config getPigeonConfig() { - return evaluator.evaluate( - text( - "pigeon { age = 30; friends = List(\"john\", \"mary\"); address { street = \"Fuzzy St.\" } }")); + return evaluator.evaluate(text(pigeonText)); } @Override @@ -42,4 +46,20 @@ protected Config getPairConfig() { protected Config getMapConfig() { return evaluator.evaluate(text("x = Map(\"one\", 1, \"two\", 2)")); } + + @Test + public void evaluateOutputValue() { + var valueConfig = + evaluator.evaluateOutputValue( + text(pigeonText + "\noutput { value = (outer) { pigeon { age = 99 } } }")); + var pigeon = valueConfig.get("pigeon").as(Person.class); + assertThat(pigeon.age).isEqualTo(99); + } + + @Test + public void evaluateExpression() { + var addressConfig = evaluator.evaluateExpression(text(pigeonText), "pigeon.address"); + var address = addressConfig.as(Address.class); + assertThat(address.street).isEqualTo("Fuzzy St."); + } }