Convert org.pkl.core POJOs to record classes (#808)

This commit is contained in:
Kushal Pisavadia
2024-11-13 23:54:41 +00:00
committed by GitHub
parent 406fa4cf40
commit 9a27616956
7 changed files with 89 additions and 337 deletions

View File

@@ -98,21 +98,21 @@ public abstract class Member implements Serializable {
return simpleName;
}
public static class SourceLocation implements Serializable {
public record SourceLocation(int startLine, int endLine) implements Serializable {
@Serial private static final long serialVersionUID = 0L;
private final int startLine;
private final int endLine;
public SourceLocation(int startLine, int endLine) {
this.startLine = startLine;
this.endLine = endLine;
}
/**
* @deprecated As of 0.28.0, replaced by {@link #startLine()}.
*/
@Deprecated(forRemoval = true)
public int getStartLine() {
return startLine;
}
/**
* @deprecated As of 0.28.0, replaced by {@link #endLine()}.
*/
@Deprecated(forRemoval = true)
public int getEndLine() {
return endLine;
}

View File

@@ -41,20 +41,12 @@ public final class PklInfo {
return packageIndex;
}
/** A Pkl package index. */
public static final class PackageIndex {
private final String homepage;
/** Constructs a {@link PackageIndex}. */
public PackageIndex(String homepage) {
this.homepage = homepage;
}
/** The homepage of this package index. */
public String homepage() {
return homepage;
}
/**
* A Pkl package index.
*
* @param homepage The homepage of this package index.
*/
public record PackageIndex(String homepage) {
/** Returns the homepage of the given package. */
public String getPackagePage(String packageName, String packageVersion) {
return homepage + packageName + "/" + packageVersion + "/";

View File

@@ -17,14 +17,18 @@ package org.pkl.core;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.TruffleOptions;
import java.util.Objects;
import org.graalvm.home.Version;
/**
* Information about the Pkl release that the current program runs on. This class is the Java
* equivalent of standard library module {@code pkl.platform}.
*/
public final class Platform {
public record Platform(
Language language,
Runtime runtime,
VirtualMachine virtualMachine,
OperatingSystem operatingSystem,
Processor processor) {
private static final Platform CURRENT;
static {
@@ -49,225 +53,46 @@ public final class Platform {
new Processor(architecture));
}
private final Language language;
private final Runtime runtime;
private final VirtualMachine virtualMachine;
private final OperatingSystem operatingSystem;
private final Processor processor;
/** Constructs a platform. */
public Platform(
Language language,
Runtime runtime,
VirtualMachine virtualMachine,
OperatingSystem operatingSystem,
Processor processor) {
this.language = language;
this.runtime = runtime;
this.virtualMachine = virtualMachine;
this.operatingSystem = operatingSystem;
this.processor = processor;
}
/** The Pkl release that the current program runs on. */
public static Platform current() {
return CURRENT;
}
/** The language implementation of this platform. */
public Language language() {
return language;
}
/**
* The language implementation of a platform.
*
* @param version the version of this language implementation
*/
public record Language(String version) {}
/** The language runtime of this platform. */
public Runtime runtime() {
return runtime;
}
/**
* The language runtime of a platform.
*
* @param name the name of this language runtime.
* @param version the version of this language runtime.
*/
public record Runtime(String name, String version) {}
/** The virtual machine of this platform. */
public VirtualMachine virtualMachine() {
return virtualMachine;
}
/**
* The virtual machine of a platform.
*
* @param name the name of this virtual machine.
* @param version the version of this virtual machine.
*/
public record VirtualMachine(String name, String version) {}
/** The operating system of this platform. */
public OperatingSystem operatingSystem() {
return operatingSystem;
}
/**
* The operating system of a platform.
*
* @param name the name of this operating system.
* @param version the version of this operating system.
*/
public record OperatingSystem(String name, String version) {}
/** The processor of this platform. */
public Processor processor() {
return processor;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Platform other)) return false;
return language.equals(other.language)
&& runtime.equals(other.runtime)
&& virtualMachine.equals(other.virtualMachine)
&& operatingSystem.equals(other.operatingSystem)
&& processor.equals(other.processor);
}
@Override
public int hashCode() {
return Objects.hash(language, runtime, virtualMachine, operatingSystem, processor);
}
/** The language implementation of a platform. */
public static final class Language {
private final String version;
/** Constructs a {@link Language}. */
public Language(String version) {
this.version = version;
}
/** The version of this language implementation. */
public String version() {
return version;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Language other)) return false;
return version.equals(other.version);
}
@Override
public int hashCode() {
return version.hashCode();
}
}
/** The language runtime of a platform. */
public static final class Runtime {
private final String name;
private final String version;
/** Constructs a {@link Runtime}. */
public Runtime(String name, String version) {
this.name = name;
this.version = version;
}
/** The name of this language runtime. */
public String name() {
return name;
}
/** The version of this language runtime. */
public String version() {
return version;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Runtime other)) return false;
return name.equals(other.name) && version.equals(other.version);
}
@Override
public int hashCode() {
return Objects.hash(name, version);
}
}
/** The virtual machine of a platform. */
public static final class VirtualMachine {
private final String name;
private final String version;
/** Constructs a {@link VirtualMachine}. */
public VirtualMachine(String name, String version) {
this.name = name;
this.version = version;
}
/** The name of this virtual machine. */
public String name() {
return name;
}
/** The version of this virtual machine. */
public String version() {
return version;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof VirtualMachine other)) return false;
return name.equals(other.name) && version.equals(other.version);
}
@Override
public int hashCode() {
return Objects.hash(name, version);
}
}
/** The operating system of a platform. */
public static final class OperatingSystem {
private final String name;
private final String version;
/** Constructs an {@link OperatingSystem}. */
public OperatingSystem(String name, String version) {
this.name = name;
this.version = version;
}
/** The name of this operating system. */
public String name() {
return name;
}
/** The version of this operating system. */
public String version() {
return version;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof OperatingSystem other)) return false;
return name.equals(other.name) && version.equals(other.version);
}
@Override
public int hashCode() {
return Objects.hash(name, version);
}
}
/** The processor of a platform. */
public static final class Processor {
private final String architecture;
/** Constructs a {@link Processor}. */
public Processor(String architecture) {
this.architecture = architecture;
}
/** The instruction set architecture of this processor. */
public String architecture() {
return architecture;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Processor other)) return false;
return architecture.equals(other.architecture);
}
@Override
public int hashCode() {
return architecture.hashCode();
}
}
/**
* The processor of a platform.
*
* @param architecture the instruction set architecture of this processor.
*/
public record Processor(String architecture) {}
}

View File

@@ -164,25 +164,15 @@ public final class Release {
}
/** The source code of a Pkl release. */
public static final class SourceCode {
private final String homepage;
private final String version;
/** Constructs a {@link SourceCode}. */
public SourceCode(String homepage, String version) {
this.homepage = homepage;
this.version = version;
}
public record SourceCode(String homepage, String version) {
/**
* @deprecated As of 0.28.0, replaced by {@link #version()}.
*/
@Deprecated(forRemoval = true)
public String getVersion() {
return version;
}
/** The homepage of this source code. */
public String homepage() {
return homepage;
}
/**
* Returns the source code page of the file with the given path. <b>Note:</b> Files may be moved
* or deleted anytime.
@@ -195,75 +185,20 @@ public final class Release {
public String getSourceCodeUrlScheme() {
return homepage + "blob/" + version + "/stdlib%{path}#L%{line}-L%{endLine}";
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof SourceCode other)) return false;
return homepage.equals(other.homepage) && version.equals(other.version);
}
@Override
public int hashCode() {
return Objects.hash(homepage, version);
}
}
/** The documentation of a Pkl release. */
public static final class Documentation {
private final String homepage;
/** Constructs a {@link Documentation}. */
public Documentation(String homepage) {
this.homepage = homepage;
}
/** The homepage of this documentation. */
public String homepage() {
return homepage;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Documentation other)) return false;
return homepage.equals(other.homepage);
}
@Override
public int hashCode() {
return homepage.hashCode();
}
}
/**
* The documentation of a Pkl release.
*
* @param homepage the homepage of this documentation.
*/
public record Documentation(String homepage) {}
/**
* The standard library of a Pkl release.
*
* @since 0.21.0
* @param modules the modules of this standard library.
*/
public static final class StandardLibrary {
private final Set<String> modules;
/** Constructs a {@link StandardLibrary}. */
public StandardLibrary(Set<String> modules) {
this.modules = modules;
}
/** The modules of this standard library. */
public Set<String> modules() {
return modules;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof StandardLibrary other)) return false;
return modules.equals(other.modules);
}
@Override
public int hashCode() {
return modules.hashCode();
}
}
public record StandardLibrary(Set<String> modules) {}
}

View File

@@ -72,8 +72,8 @@ class EvaluateSchemaTest {
assertThat(module.moduleName).isEqualTo("test")
assertThat(module.moduleClass.sourceLocation.startLine).isEqualTo(2)
assertThat(module.moduleClass.sourceLocation.endLine).isEqualTo(26)
assertThat(module.moduleClass.sourceLocation.startLine()).isEqualTo(2)
assertThat(module.moduleClass.sourceLocation.endLine()).isEqualTo(26)
}
private fun checkModuleProperties(module: ModuleSchema) {
@@ -81,21 +81,21 @@ class EvaluateSchemaTest {
assertThat(properties).hasSize(3)
val propertyb1 = properties.getValue("propertyb1")
assertThat(propertyb1.sourceLocation.startLine).isEqualTo(5)
assertThat(propertyb1.sourceLocation.endLine).isEqualTo(5)
assertThat(propertyb1.sourceLocation.startLine()).isEqualTo(5)
assertThat(propertyb1.sourceLocation.endLine()).isEqualTo(5)
assertThat(propertyb1.type).isEqualTo(PType.UNKNOWN)
val propertyb2 = properties.getValue("propertyb2")
assertThat(propertyb2.sourceLocation.startLine).isEqualTo(8)
assertThat(propertyb2.sourceLocation.endLine).isEqualTo(9)
assertThat(propertyb2.sourceLocation.startLine()).isEqualTo(8)
assertThat(propertyb2.sourceLocation.endLine()).isEqualTo(9)
val paramType = propertyb2.type
assertThat(paramType).isInstanceOf(PType.Class::class.java)
paramType as PType.Class
assertThat(paramType.pClass).isEqualTo(BaseModule.getIntClass().export())
val propertyb3 = properties.getValue("propertyb3")
assertThat(propertyb3.sourceLocation.startLine).isEqualTo(24)
assertThat(propertyb3.sourceLocation.endLine).isEqualTo(24)
assertThat(propertyb3.sourceLocation.startLine()).isEqualTo(24)
assertThat(propertyb3.sourceLocation.endLine()).isEqualTo(24)
}
private fun checkModuleMethods(module: ModuleSchema) {
@@ -103,14 +103,14 @@ class EvaluateSchemaTest {
assertThat(methods).hasSize(3)
val methodb1 = methods.getValue("methodb1")
assertThat(methodb1.sourceLocation.startLine).isEqualTo(12)
assertThat(methodb1.sourceLocation.endLine).isEqualTo(12)
assertThat(methodb1.sourceLocation.startLine()).isEqualTo(12)
assertThat(methodb1.sourceLocation.endLine()).isEqualTo(12)
assertThat(methodb1.parameters).isEmpty()
assertThat(methodb1.returnType).isEqualTo(PType.UNKNOWN)
val methodb2 = methods.getValue("methodb2")
assertThat(methodb2.sourceLocation.startLine).isEqualTo(15)
assertThat(methodb2.sourceLocation.endLine).isEqualTo(16)
assertThat(methodb2.sourceLocation.startLine()).isEqualTo(15)
assertThat(methodb2.sourceLocation.endLine()).isEqualTo(16)
val paramType = methodb2.parameters.getValue("str")
assertThat(paramType).isInstanceOf(PType.Constrained::class.java)
paramType as PType.Constrained
@@ -130,8 +130,8 @@ class EvaluateSchemaTest {
assertThat(returnType.constraints).isEqualTo(listOf("isPositive"))
val methodb3 = methods.getValue("methodb3")
assertThat(methodb3.sourceLocation.startLine).isEqualTo(26)
assertThat(methodb3.sourceLocation.endLine).isEqualTo(26)
assertThat(methodb3.sourceLocation.startLine()).isEqualTo(26)
assertThat(methodb3.sourceLocation.endLine()).isEqualTo(26)
assertThat(methodb3.parameters.keys).containsExactly("x", "_#1", "i", "_#3")
}
@@ -139,8 +139,8 @@ class EvaluateSchemaTest {
val classes = module.classes
assertThat(classes).hasSize(1)
val classb1 = classes.getValue("Classb1")
assertThat(classb1.sourceLocation.startLine).isEqualTo(19)
assertThat(classb1.sourceLocation.endLine).isEqualTo(22)
assertThat(classb1.sourceLocation.startLine()).isEqualTo(19)
assertThat(classb1.sourceLocation.endLine()).isEqualTo(22)
assertThat(classb1.properties).hasSize(2)
}
@@ -157,8 +157,8 @@ class EvaluateSchemaTest {
.isEqualTo(URI("modulepath:/org/pkl/core/EvaluateSchemaTestBaseModule.pkl"))
assertThat(supermodule.moduleName).isEqualTo("test.base")
assertThat(supermodule.moduleClass.sourceLocation.startLine).isEqualTo(1)
assertThat(supermodule.moduleClass.sourceLocation.endLine).isEqualTo(10)
assertThat(supermodule.moduleClass.sourceLocation.startLine()).isEqualTo(1)
assertThat(supermodule.moduleClass.sourceLocation.endLine()).isEqualTo(10)
val properties = supermodule.moduleClass.properties
assertThat(properties).hasSize(1)

View File

@@ -274,8 +274,8 @@ internal abstract class ModuleOrClassPageGenerator<S>(
protected fun HtmlBlockTag.renderMemberSourceLink(member: Member) {
// Prevent churn by setting static line numbers.
// This is so our doc generator tests don't break if, say, we change sources in the stdlib.
val startLine = if (isTestMode) 123 else member.sourceLocation.startLine
val endLine = if (isTestMode) 456 else member.sourceLocation.endLine
val startLine = if (isTestMode) 123 else member.sourceLocation.startLine()
val endLine = if (isTestMode) 456 else member.sourceLocation.endLine()
val moduleSourceUrl =
pageScope.resolveModuleNameToSourceUrl(
member.moduleName,

View File

@@ -114,8 +114,8 @@ internal fun String.replaceSourceCodePlaceholders(
sourceLocation: Member.SourceLocation
): String {
return replace("%{path}", path)
.replace("%{line}", sourceLocation.startLine.toString())
.replace("%{endLine}", sourceLocation.endLine.toString())
.replace("%{line}", sourceLocation.startLine().toString())
.replace("%{endLine}", sourceLocation.endLine().toString())
}
/** Encodes a URI string, encoding characters that are part of URI syntax. */