Skip to content

Commit

Permalink
Add support for default-for attribute
Browse files Browse the repository at this point in the history
This new index.json attribute is design to allow more
flexible version matching to avoid breakage due to
switch on latest version for untested ones.

It should have a `default-for` key and a regexp as a
string value, for example "1\\.0\\..*" which should
match versions like "1.0.0", "1.0.1", "1.0.2", etc.

See oracle/graalvm-reachability-metadata#62
See oracle/graalvm-reachability-metadata#275
  • Loading branch information
sdeleuze committed Apr 28, 2023
1 parent 81a8717 commit 1feb0d4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Set;
import java.util.regex.Pattern;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Artifact {
Expand All @@ -53,18 +54,21 @@ public class Artifact {
private final String directory;
private final boolean latest;
private final boolean override;
private final Pattern defaultForPattern;

@JsonCreator
public Artifact(@JsonProperty("module") String module,
@JsonProperty("tested-versions") Set<String> versions,
@JsonProperty("metadata-version") String directory,
@JsonProperty(value = "latest", defaultValue = "false") boolean latest,
@JsonProperty(value = "override", defaultValue = "false") boolean override) {
@JsonProperty(value = "override", defaultValue = "false") boolean override,
@JsonProperty(value = "default-for") String defaultFor) {
this.module = module;
this.versions = versions;
this.directory = directory;
this.latest = latest;
this.override = override;
this.defaultForPattern = (defaultFor == null ? null : Pattern.compile(defaultFor));
}

public String getModule() {
Expand All @@ -86,4 +90,8 @@ public boolean isLatest() {
public boolean isOverride() {
return override;
}

public boolean isDefaultFor(String version) {
return defaultForPattern != null && defaultForPattern.matcher(version).matches();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ private Map<String, List<Artifact>> parseIndexFile(Path rootPath) {
*/
@Override
public Optional<DirectoryConfiguration> findConfiguration(String groupId, String artifactId, String version) {
return findConfigurationFor(groupId, artifactId, version, artifact -> artifact.getVersions().contains(version));
return findConfigurationFor(groupId, artifactId, version,
artifact -> artifact.getVersions().contains(version) || artifact.isDefaultFor(version));
}

@Override
Expand All @@ -101,15 +102,16 @@ public Optional<DirectoryConfiguration> findLatestConfigurationFor(String groupI
}

/**
* Returns the latest configuration directory for the requested artifact.
* Returns the matching configuration directory for the requested artifact.
*
* @param groupId the group ID of the artifact
* @param artifactId the artifact ID of the artifact
* @param version the version of the artifact
* @return a configuration directory, or empty if no configuration directory is available
*/
@Override
public Optional<DirectoryConfiguration> findLatestConfigurationFor(String groupId, String artifactId, String version) {
return findConfigurationFor(groupId, artifactId, version, Artifact::isLatest);
return findConfigurationFor(groupId, artifactId, version, artifact -> artifact.isDefaultFor(version) || artifact.isLatest());
}

private Optional<DirectoryConfiguration> findConfigurationFor(String groupId, String artifactId, String version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,36 @@ void checkIndex() throws URISyntaxException {

}

@Test
void checkIndexWithDefaultFor() throws URISyntaxException {
withIndex("artifact-2");

Optional<DirectoryConfiguration> config = index.findConfiguration("com.foo", "bar", "1.0");
assertTrue(config.isPresent());
assertEquals(repoPath.resolve("1.0"), config.get().getDirectory());

config = index.findConfiguration("com.foo", "bar", "1.1");
assertTrue(config.isPresent());
assertEquals(repoPath.resolve("1.0"), config.get().getDirectory());

config = index.findConfiguration("com.foo", "bar", "2.0");
assertTrue(config.isPresent());
assertEquals(repoPath.resolve("2.0"), config.get().getDirectory());

config = index.findConfiguration("com.foo", "bar", "2.1");
assertTrue(config.isPresent());
assertEquals(repoPath.resolve("2.0"), config.get().getDirectory());

Optional<DirectoryConfiguration> latest = index.findLatestConfigurationFor("com.foo", "bar", "123");
assertTrue(latest.isPresent());
assertEquals(repoPath.resolve("2.0"), latest.get().getDirectory());

config = index.findConfiguration("com.foo", "baz", "1.1");
assertTrue(config.isPresent());
assertEquals(repoPath.resolve("1.0"), config.get().getDirectory());

}

private void withIndex(String json) throws URISyntaxException {
repoPath = new File(SingleModuleJsonVersionToConfigDirectoryIndexTest.class.getResource("/json/" + json).toURI()).toPath();
index = new SingleModuleJsonVersionToConfigDirectoryIndex(repoPath);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{ "module": "com.foo:bar", "tested-versions": ["1.0"], "metadata-version": "1.0", "default-for": "1\\..*" },
{ "module": "com.foo:bar", "tested-versions": ["2.0", "2.1"], "metadata-version": "2.0", "latest": true },
{ "module": "com.foo:baz", "tested-versions": ["1.0"], "metadata-version": "1.0", "default-for": "1.*" }
]

0 comments on commit 1feb0d4

Please sign in to comment.