Skip to content

Commit

Permalink
feat(expressions): Add support for comparing versions
Browse files Browse the repository at this point in the history
  • Loading branch information
KristianOellegaard committed May 15, 2024
1 parent 2d682c9 commit 71e08bd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
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(
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

0 comments on commit 71e08bd

Please sign in to comment.