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(scripting): add kotlin script (JSR-223) support #73

Merged
merged 4 commits into from
Nov 10, 2020
Merged
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Available script languages:
* [groovy](http://groovy-lang.org/)
* [feel](https://github.com/camunda/feel-scala)
* [mustache](http://mustache.github.io/mustache.5.html)
* [kotlin](https://kotlinlang.org/)

## Install

Expand Down
26 changes: 26 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

<version.groovy>2.4.20</version.groovy>
<version.feel>1.12.3</version.feel>
<version.kotlin>1.4.10</version.kotlin>

<version.spring.boot>2.3.5.RELEASE</version.spring.boot>
<version.java>11</version.java>
Expand Down Expand Up @@ -51,6 +52,14 @@
<type>pom</type>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-bom</artifactId>
<version>${version.kotlin}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
Expand Down Expand Up @@ -111,6 +120,12 @@
<version>${version.feel}</version>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-scripting-jsr223</artifactId>
<version>${version.kotlin}</version>
</dependency>

<!-- test -->
<dependency>
<groupId>junit</groupId>
Expand Down Expand Up @@ -155,6 +170,17 @@
</goals>
</execution>
</executions>
<configuration>
<!-- Needs to unpack this dependency.
See: https://github.com/sdeleuze/kotlin-script-templating/issues/7#issuecomment-364572513
-->
<requiresUnpack>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-compiler-embeddable</artifactId>
</dependency>
</requiresUnpack>
</configuration>
</plugin>

<plugin>
Expand Down
61 changes: 61 additions & 0 deletions src/test/java/io/zeebe/script/EvaluationKotlinTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.zeebe.script;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.junit.Test;

public class EvaluationKotlinTest {

private final ScriptEvaluator scriptEvaluator = new ScriptEvaluator();

@Test
public void shouldReturnNumber() {
final Object result =
scriptEvaluator.evaluate("kotlin", "x * 2", Collections.singletonMap("x", 2));

assertThat(result).isEqualTo(4);
}

@Test
public void shouldReturnString() {
final Object result =
scriptEvaluator.evaluate("kotlin", "\"url?id=\" + id", Collections.singletonMap("id", "123"));

assertThat(result).isEqualTo("url?id=123");
}

@Test
public void shouldReturnList() {
@SuppressWarnings("unchecked")
final List<Object> result =
(List<Object>)
scriptEvaluator.evaluate("kotlin", "listOf(1, 2, x)", Collections.singletonMap("x", 3));

assertThat(result).hasSize(3).contains(1, 2, 3);
}

@Test
public void shouldReturnObject() {
@SuppressWarnings("unchecked")
final Map<String, Object> result =
(Map<String, Object>)
scriptEvaluator.evaluate(
"kotlin", "mapOf(\"foo\" to foo, \"bar\" to \"bar\")", Collections.singletonMap("foo", 123));

assertThat(result).hasSize(2).contains(entry("foo", 123), entry("bar", "bar"));
}

@Test
public void shouldReturnResultOfStringInterpolation() {

final Object result =
scriptEvaluator.evaluate(
"kotlin", "\"url?id=${id}\".toString()", Collections.singletonMap("id", 123));

assertThat(result).isEqualTo("url?id=123");
}
}
14 changes: 14 additions & 0 deletions src/test/java/io/zeebe/script/ScriptEvaluatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ public void shouldEvaluateFeel() {
assertThat(result).isEqualTo(123L);
}

@Test
public void shouldEvaluateKotlin() {
final Object result = scriptEvaluator.evaluate("kotlin", "123", Collections.emptyMap());

assertThat(result).isEqualTo(123);
}

@Test
public void shouldEvaluateJavaScriptWithVariables() {

Expand All @@ -72,6 +79,13 @@ public void shouldEvaluateFeelWithVariables() {
assertThat(result).isEqualTo(123L);
}

@Test
public void shouldEvaluateKotlinWithVariables() {
final Object result = scriptEvaluator.evaluate("kotlin", "a", Collections.singletonMap("a", 123));

assertThat(result).isEqualTo(123);
}

@Test
public void shouldThrowExceptionIfScriptEngineNotFound() {
assertThatThrownBy(() -> scriptEvaluator.evaluate("foobar", "", Collections.emptyMap()))
Expand Down