From 95e04fccbde19a87cb949d87e99444a06a69f300 Mon Sep 17 00:00:00 2001 From: J-N-K Date: Sun, 12 Mar 2023 12:00:06 +0100 Subject: [PATCH] Allow inline scripts in SCRIPT transformation (#3249) Signed-off-by: Jan N. Klug --- .../script/ScriptTransformationService.java | 19 +++++++++++++------ .../ScriptTransformationServiceTest.java | 9 +++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/ScriptTransformationService.java b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/ScriptTransformationService.java index 130dfcf8f1..a94ff083d4 100644 --- a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/ScriptTransformationService.java +++ b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/ScriptTransformationService.java @@ -96,13 +96,20 @@ public class ScriptTransformationService implements TransformationService, Regis 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 + "'."); diff --git a/bundles/org.openhab.core.automation.module.script/src/test/java/org/openhab/core/automation/module/script/ScriptTransformationServiceTest.java b/bundles/org.openhab.core.automation.module.script/src/test/java/org/openhab/core/automation/module/script/ScriptTransformationServiceTest.java index df834febe7..f339903120 100644 --- a/bundles/org.openhab.core.automation.module.script/src/test/java/org/openhab/core/automation/module/script/ScriptTransformationServiceTest.java +++ b/bundles/org.openhab.core.automation.module.script/src/test/java/org/openhab/core/automation/module/script/ScriptTransformationServiceTest.java @@ -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"; @@ -176,4 +178,11 @@ public class ScriptTransformationServiceTest { 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)); + } }