Add net stdlib module with URL support (#1834)

This commit is contained in:
Islon Scherer authored and GitHub committed 2026-10-06 15:23:32 +02:00
1 parent dbcecd9677
commit 862c61f2e2
46 files changed
+3682 -15

No files matched your search

@@ -1,5 +1,5 @@
/*
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2026 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.
@@ -265,6 +265,36 @@ public final class Conversions {
}
});
/**
* Conversion from {@code pkl.net#Url} to {@link String}. Gives the same result as {@code
* Url.toString()}.
*/
public static final Conversion<PObject, String> pUrlToString =
Conversion.of(PClassInfo.Url, String.class, (value, mapper) -> serializeUrl(value));
/**
* Conversion from {@code pkl.net#Url} to {@link URI}. Throws {@link ConversionException} if the
* URL cannot be represented as a {@link URI}, which is the case for a host that is an IPvFuture
* address.
*/
public static final Conversion<PObject, URI> pUrlToURI =
Conversion.of(
PClassInfo.Url,
URI.class,
(value, mapper) -> {
var url = serializeUrl(value);
try {
return new URI(url);
} catch (URISyntaxException e) {
throw new ConversionException(
String.format("Failed to convert `pkl.net#Url` `%s` to `java.net.URI`.", url), e);
}
});
private static String serializeUrl(PObject url) {
return (String) url.getProperty("stringValue");
}
/**
* Identity conversions used when the Java representation of the Pkl type matches the target type
* or when the target type is {@link Object}.
@@ -323,7 +353,9 @@ public final class Conversions {
pDurationToDuration,
pVersionToVersion,
pVersionToString,
pStringToVersion);
pStringToVersion,
pUrlToString,
pUrlToURI);
/** All conversions defined in this class. */
public static final Collection<Conversion<?, ?>> all = collectAll();
@@ -0,0 +1,66 @@
/*
* Copyright © 2026 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pkl.config.java.mapper;
import static org.assertj.core.api.Assertions.assertThat;
import static org.pkl.core.ModuleSource.modulePath;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.pkl.core.Evaluator;
import org.pkl.core.PModule;
public class PUrlToStringTest {
private static final Evaluator evaluator = Evaluator.preconfigured();
private static final PModule module =
evaluator.evaluate(modulePath("org/pkl/config/java/mapper/PUrlToURITest.pkl"));
private static final ValueMapper mapper = ValueMapperBuilder.preconfigured().build();
@AfterAll
public static void afterAll() {
evaluator.close();
}
@Test
public void absolute() {
assertThat(map("absolute")).isEqualTo("https://user:pw@example.com:8080/a/b?q=1#frag");
}
@Test
public void percentEncoding() {
assertThat(map("literalSpace")).isEqualTo("https://example.com/a%20b");
assertThat(map("nonAscii")).isEqualTo("https://example.com/%C3%A9");
}
@Test
public void relative() {
assertThat(map("relative")).isEqualTo("/foo/bar");
assertThat(map("colonSegment")).isEqualTo("./a:b");
}
@Test
public void ipvFuture() {
assertThat(map("ipvFuture")).isEqualTo("http://[v1.fe80::a+en1]/");
}
private static String map(String propertyName) {
var url = module.get(propertyName);
assert url != null;
return mapper.map(url, String.class);
}
}
@@ -0,0 +1,92 @@
/*
* Copyright © 2026 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pkl.config.java.mapper;
import static org.assertj.core.api.Assertions.*;
import static org.pkl.core.ModuleSource.modulePath;
import java.net.URI;
import java.net.URISyntaxException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.pkl.core.Evaluator;
import org.pkl.core.PModule;
public class PUrlToURITest {
private static final Evaluator evaluator = Evaluator.preconfigured();
private static final PModule module =
evaluator.evaluate(modulePath("org/pkl/config/java/mapper/PUrlToURITest.pkl"));
private static final ValueMapper mapper = ValueMapperBuilder.preconfigured().build();
@AfterAll
public static void afterAll() {
evaluator.close();
}
@Test
public void absolute() {
assertThat(map("absolute"))
.isEqualTo(URI.create("https://user:pw@example.com:8080/a/b?q=1#frag"));
}
@Test
public void percentEncoding() {
// characters that cannot appear literally are encoded, and what is already encoded is kept
assertThat(map("literalSpace")).isEqualTo(URI.create("https://example.com/a%20b"));
assertThat(map("encodedSpace")).isEqualTo(URI.create("https://example.com/a%20b"));
assertThat(map("encodedSlash")).isEqualTo(URI.create("https://example.com/a%2Fb"));
assertThat(map("nonAscii")).isEqualTo(URI.create("https://example.com/%C3%A9"));
}
@Test
public void relative() {
assertThat(map("relative")).isEqualTo(URI.create("/foo/bar"));
// a first segment holding a ":" has to be preceded by a dot-segment, or it would be read as a
// scheme
assertThat(map("colonSegment")).isEqualTo(URI.create("./a:b"));
}
@Test
public void authority() {
assertThat(map("rootless")).isEqualTo(URI.create("mailto:user@example.com"));
assertThat(map("noAuthority")).isEqualTo(URI.create("file:/tmp"));
assertThat(map("emptyAuthority")).isEqualTo(URI.create("file:///tmp"));
assertThat(map("emptyQuery")).isEqualTo(URI.create("https://example.com/?"));
assertThat(map("ipv6Zone")).isEqualTo(URI.create("http://[fe80::1%25eth0]/p"));
}
@Test
public void constructed() {
assertThat(map("constructed")).isEqualTo(URI.create("https://example.com/a%20b?x%20y"));
}
@Test
public void ipvFuture() {
// `java.net.URI` doesn't know IPvFuture addresses
assertThatThrownBy(() -> map("ipvFuture"))
.isInstanceOf(ConversionException.class)
.hasMessageContaining("http://[v1.fe80::a+en1]/")
.hasCauseInstanceOf(URISyntaxException.class);
}
private static URI map(String propertyName) {
var url = module.get(propertyName);
assert url != null;
return mapper.map(url, URI.class);
}
}
@@ -0,0 +1,16 @@
import "pkl:net"
absolute = net.Url("https://user:pw@example.com:8080/a/b?q=1#frag")
literalSpace = net.Url("https://example.com/a b")
encodedSpace = net.Url("https://example.com/a%20b")
encodedSlash = net.Url("https://example.com/a%2Fb")
nonAscii = net.Url("https://example.com/é")
relative = net.Url("/foo/bar")
rootless = net.Url("mailto:user@example.com")
noAuthority = net.Url("file:/tmp")
emptyAuthority = net.Url("file:///tmp")
emptyQuery = net.Url("https://example.com/?")
ipv6Zone = net.Url("http://[fe80::1%25eth0]/p")
ipvFuture = net.Url("http://[v1.fe80::a+en1]/")
constructed = new net.Url { scheme = "https"; rawHost = "example.com"; rawPath = "/a%20b"; rawQuery = "x%20y" }
colonSegment = new net.Url { rawPath = "./a:b" }