forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for dependencies in plugin descriptor properties with sem…
…ver range (opensearch-project#1707) Signed-off-by: Abhilasha Seth <[email protected]>
- Loading branch information
1 parent
69cc2a1
commit 7e41183
Showing
23 changed files
with
1,024 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
libs/core/src/main/java/org/opensearch/semver/SemverRange.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.semver; | ||
|
||
import org.opensearch.Version; | ||
import org.opensearch.common.Nullable; | ||
import org.opensearch.core.xcontent.ToXContentFragment; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.semver.expr.Equal; | ||
import org.opensearch.semver.expr.Expression; | ||
import org.opensearch.semver.expr.Tilde; | ||
|
||
import java.io.IOException; | ||
import java.util.Locale; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import static java.lang.String.format; | ||
import static java.util.Arrays.stream; | ||
|
||
/** | ||
* Represents a single semver range. | ||
*/ | ||
public class SemverRange implements ToXContentFragment { | ||
|
||
private final Version rangeVersion; | ||
private final RangeOperator rangeOperator; | ||
|
||
public SemverRange(final Version rangeVersion, final RangeOperator rangeOperator) { | ||
this.rangeVersion = rangeVersion; | ||
this.rangeOperator = rangeOperator; | ||
} | ||
|
||
/** | ||
* Constructs a {@code SemverRange} from its string representation. | ||
* @param range given range | ||
* @return a {@code SemverRange} | ||
*/ | ||
public static SemverRange fromString(final String range) { | ||
Optional<RangeOperator> operator = stream(RangeOperator.values()).filter( | ||
rangeOperator -> rangeOperator != RangeOperator.DEFAULT && range.startsWith(rangeOperator.asString()) | ||
).findFirst(); | ||
RangeOperator rangeOperator = operator.orElse(RangeOperator.DEFAULT); | ||
String version = range.replaceFirst(rangeOperator.asString(), ""); | ||
if (!Version.stringHasLength(version)) { | ||
throw new IllegalArgumentException("Version cannot be empty"); | ||
} | ||
return new SemverRange(Version.fromString(version), rangeOperator); | ||
} | ||
|
||
/** | ||
* Return the range operator for this range. | ||
* @return range operator | ||
*/ | ||
public RangeOperator getRangeOperator() { | ||
return rangeOperator; | ||
} | ||
|
||
/** | ||
* Check if range is satisfied by given version string. | ||
* | ||
* @param version version to check | ||
* @return {@code true} if range is satisfied by version, {@code false} otherwise | ||
*/ | ||
public boolean isSatisfiedBy(final String version) { | ||
return isSatisfiedBy(Version.fromString(version)); | ||
} | ||
|
||
/** | ||
* Check if range is satisfied by given version. | ||
* | ||
* @param version version to check | ||
* @return {@code true} if range is satisfied by version, {@code false} otherwise | ||
* @see #isSatisfiedBy(String) | ||
*/ | ||
public boolean isSatisfiedBy(final Version version) { | ||
Expression expression = null; | ||
switch (rangeOperator) { | ||
case DEFAULT: | ||
case EQ: | ||
expression = new Equal(rangeVersion); | ||
break; | ||
case TILDE: | ||
expression = new Tilde(rangeVersion); | ||
break; | ||
default: | ||
throw new RuntimeException(format(Locale.ROOT, "Unsupported range operator: %s", rangeOperator)); | ||
} | ||
return expression.evaluate(version); | ||
} | ||
|
||
@Override | ||
public boolean equals(@Nullable final Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
SemverRange range = (SemverRange) o; | ||
return Objects.equals(rangeVersion, range.rangeVersion) && rangeOperator == range.rangeOperator; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(rangeVersion, rangeOperator); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return rangeOperator.asString() + rangeVersion; | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException { | ||
return builder.value(toString()); | ||
} | ||
|
||
/** | ||
* A range operator. | ||
*/ | ||
public enum RangeOperator { | ||
|
||
EQ("="), | ||
TILDE("~"), | ||
DEFAULT(""); | ||
|
||
private final String operator; | ||
|
||
RangeOperator(final String operator) { | ||
this.operator = operator; | ||
} | ||
|
||
/** | ||
* String representation of the range operator. | ||
* | ||
* @return range operator as string | ||
*/ | ||
public String asString() { | ||
return operator; | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
libs/core/src/main/java/org/opensearch/semver/expr/Equal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.semver.expr; | ||
|
||
import org.opensearch.Version; | ||
|
||
/** | ||
* Expression to evaluate equality of versions. | ||
*/ | ||
public class Equal implements Expression { | ||
|
||
private final Version version; | ||
|
||
/** | ||
* Constructs a {@code Equal} expression with the given version. | ||
* | ||
* @param version given version | ||
*/ | ||
public Equal(final Version version) { | ||
this.version = version; | ||
} | ||
|
||
/** | ||
* Checks if the current version equals the member version. | ||
* | ||
* @param version the version to evaluate | ||
* @return {@code true} if the versions are equal {@code false} otherwise | ||
*/ | ||
@Override | ||
public boolean evaluate(final Version version) { | ||
return version.equals(this.version); | ||
} | ||
} |
Oops, something went wrong.