Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for default-for attribute #432

Merged
merged 2 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is no longer accurate. Not sure if we want to change it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please elaborate?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

findLatestConfigurationFor, shouldn't it be something like findMatchingConfigurationFor now?

return findConfigurationFor(groupId, artifactId, version, Artifact::isLatest);
return findConfigurationFor(groupId, artifactId, version, artifact -> artifact.isDefaultFor(version) || artifact.isLatest());
sdeleuze marked this conversation as resolved.
Show resolved Hide resolved
}

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.*" }
]