mirror of
https://github.com/apple/pkl.git
synced 2026-04-11 03:06:55 +02:00
Add support for evaluating module output and expressions to ConfigEvaluator (#1297)
This commit is contained in:
@@ -110,7 +110,7 @@ public interface Config {
|
|||||||
return fromPklBinary(inputStream, ValueMapper.preconfigured());
|
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) {
|
if (decoded instanceof Composite composite) {
|
||||||
return new CompositeConfig("", mapper, composite);
|
return new CompositeConfig("", mapper, composite);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -41,6 +41,12 @@ public interface ConfigEvaluator extends AutoCloseable {
|
|||||||
/** Evaluates the given module source into a {@link Config} tree. */
|
/** Evaluates the given module source into a {@link Config} tree. */
|
||||||
Config evaluate(ModuleSource moduleSource);
|
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
|
* 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.
|
* executing, this method blocks until cancellation of that execution has completed.
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -34,6 +34,18 @@ final class ConfigEvaluatorImpl implements ConfigEvaluator {
|
|||||||
return new CompositeConfig("", mapper, module);
|
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
|
@Override
|
||||||
public ValueMapper getValueMapper() {
|
public ValueMapper getValueMapper() {
|
||||||
return mapper;
|
return mapper;
|
||||||
|
|||||||
@@ -15,16 +15,20 @@
|
|||||||
*/
|
*/
|
||||||
package org.pkl.config.java;
|
package org.pkl.config.java;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.pkl.core.ModuleSource.text;
|
import static org.pkl.core.ModuleSource.text;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class ConfigTest extends AbstractConfigTest {
|
public class ConfigTest extends AbstractConfigTest {
|
||||||
private static final ConfigEvaluator evaluator = ConfigEvaluator.preconfigured();
|
private static final ConfigEvaluator evaluator = ConfigEvaluator.preconfigured();
|
||||||
|
|
||||||
|
private static final String pigeonText =
|
||||||
|
"pigeon { age = 30; friends = List(\"john\", \"mary\"); address { street = \"Fuzzy St.\" } }";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Config getPigeonConfig() {
|
protected Config getPigeonConfig() {
|
||||||
return evaluator.evaluate(
|
return evaluator.evaluate(text(pigeonText));
|
||||||
text(
|
|
||||||
"pigeon { age = 30; friends = List(\"john\", \"mary\"); address { street = \"Fuzzy St.\" } }"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -42,4 +46,20 @@ public class ConfigTest extends AbstractConfigTest {
|
|||||||
protected Config getMapConfig() {
|
protected Config getMapConfig() {
|
||||||
return evaluator.evaluate(text("x = Map(\"one\", 1, \"two\", 2)"));
|
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.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user