Skip to content
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
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
Expand Down
26 changes: 23 additions & 3 deletions pkl-config-java/src/test/java/org/pkl/config/java/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.");
}
}