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

feat: Ability to easily compare versions using #toArtifactVersion #1183

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions kork-expressions/kork-expressions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies {
api "org.slf4j:slf4j-api"

implementation "org.springframework.boot:spring-boot"
implementation "org.apache.maven:maven-artifact:3.9.6"

testImplementation project(":kork-artifacts")
testImplementation "org.assertj:assertj-core"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.pf4j.PluginManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -90,6 +91,7 @@ public ExpressionsSupport(
LinkedHashSet.class,
HashMap.class,
LinkedHashMap.class,
DefaultArtifactVersion.class,
TreeMap.class,
TreeSet.class));
Collections.addAll(allowedReturnTypes, extraAllowedReturnTypes);
Expand Down Expand Up @@ -281,6 +283,16 @@ public static Integer toInt(String str) {
return Integer.valueOf(str);
}

/**
* Parses a string to an integer
*
* @param str represents an int
* @return an integer
*/
public static DefaultArtifactVersion toArtifactVersion(String str) {
return new DefaultArtifactVersion(str);
}

/**
* Parses a string to a float
*
Expand Down Expand Up @@ -343,6 +355,13 @@ public Functions getFunctions() {
"toInt",
"Converts a string to integer",
new FunctionParameter(String.class, "value", "A String value to convert to an int")),
new FunctionDefinition(
"toArtifactVersion",
"Converts a string to a Maven Default Artifact Version",
new FunctionParameter(
String.class,
"value",
"A String value to convert to a Maven Default Artifact Version")),
new FunctionDefinition(
"toFloat",
"Converts a string to float",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,26 @@ public void delegatesTypeConversion() {
assertThat(evaluated).isEqualTo("00000000-0000-0000-0000-000000000000");
}

@Test
public void compareArtifactVersion() {
// If a thing is not an artifact URI, it should delegate to StandardTypeConverter
ExpressionProperties expressionProperties = new ExpressionProperties();

// StandardTypeConverter does things like convert ints to longs
String testInput = ("${#toArtifactVersion('2.0.0') > #toArtifactVersion('1.0.0')}");
Map<String, Object> testContext = Map.of();

String evaluated =
new ExpressionTransform(parserContext, parser, Function.identity())
.transformString(
Copy link
Member

Choose a reason for hiding this comment

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

SO this tries to translate the expression to a string. What's interesting is I DON'T think it's ever actually USED. Transform MAP is used. Ignoring that, you can use this but this I believe returns a boolean which it tries to cast as a string and then fails. Can switch to a transformMap (which is what's REALLY used) OR maybe add a .toString() on the results. I'd be inclined to do the map based test via a "transformMap(Map.of("test", testInput)...).get("test"). " kinda test.

testInput,
new ExpressionsSupport(null, expressionProperties)
.buildEvaluationContext(testContext, true),
new ExpressionEvaluationSummary());

assertThat(evaluated).isEqualTo(true);
}

public class MockArtifactStore extends ArtifactStore {
public Map<String, String> cache = new HashMap<>();

Expand Down
Loading