Skip to content

Commit

Permalink
Allow inline scripts in SCRIPT transformation (openhab#3249)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <github@klug.nrw>
GitOrigin-RevId: 95e04fc
  • Loading branch information
J-N-K authored and splatch committed Jul 12, 2023
1 parent 1038e73 commit 4e969f1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,20 @@ public void deactivate() {
scriptRecord.lock.lock();
try {
if (scriptRecord.script.isBlank()) {
Transformation transformation = transformationRegistry.get(scriptUid);
if (transformation != null) {
if (!SUPPORTED_CONFIGURATION_TYPE.equals(transformation.getType())) {
throw new TransformationException("Configuration does not have correct type 'script' but '"
+ transformation.getType() + "'.");
if (scriptUid.startsWith("|")) {
// inline script -> strip inline-identifier
scriptRecord.script = scriptUid.substring(1);
} else {
// get script from transformation registry
Transformation transformation = transformationRegistry.get(scriptUid);
if (transformation != null) {
if (!SUPPORTED_CONFIGURATION_TYPE.equals(transformation.getType())) {
throw new TransformationException("Configuration does not have correct type 'script' but '"
+ transformation.getType() + "'.");
}
scriptRecord.script = transformation.getConfiguration().getOrDefault(Transformation.FUNCTION,
"");
}
scriptRecord.script = transformation.getConfiguration().getOrDefault(Transformation.FUNCTION, "");
}
if (scriptRecord.script.isBlank()) {
throw new TransformationException("Could not get script for UID '" + scriptUid + "'.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class ScriptTransformationServiceTest {
private static final String SCRIPT_UID = "scriptUid";
private static final String INVALID_SCRIPT_UID = "invalidScriptUid";

private static final String INLINE_SCRIPT = "|inlineScript";

private static final String SCRIPT = "script";
private static final String SCRIPT_OUTPUT = "output";

Expand Down Expand Up @@ -176,4 +178,11 @@ public void invalidConfigurationTypeThrowsTransformationException() {

assertThat(e.getMessage(), is("Configuration does not have correct type 'script' but 'invalid'."));
}

@Test
public void inlineScriptProperlyProcessed() throws TransformationException, ScriptException {
service.transform(SCRIPT_LANGUAGE + ":" + INLINE_SCRIPT, "input");

verify(scriptEngine).eval(INLINE_SCRIPT.substring(1));
}
}

0 comments on commit 4e969f1

Please sign in to comment.