From 5cf91cde1b3bcdbda5c7acf315adb006b495b783 Mon Sep 17 00:00:00 2001 From: jimtng <2554958+jimtng@users.noreply.github.com> Date: Sat, 7 Sep 2024 14:59:22 +1000 Subject: [PATCH] Filter out blank lines and comments in a multi-line transformations (#4357) Signed-off-by: Jimmy Tanagra --- .../thing/binding/generic/ChannelTransformation.java | 9 +++++++-- .../binding/generic/ChannelTransformationTest.java | 11 +++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/generic/ChannelTransformation.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/generic/ChannelTransformation.java index e8c744982..a5870f4e3 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/generic/ChannelTransformation.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/generic/ChannelTransformation.java @@ -56,8 +56,13 @@ public class ChannelTransformation { public ChannelTransformation(@Nullable List transformationStrings) { if (transformationStrings != null) { try { - transformationSteps = transformationStrings.stream() - .flatMap(ChannelTransformation::splitTransformationString).map(TransformationStep::new) + transformationSteps = transformationStrings.stream() // + .map(String::trim) // + .filter(line -> !line.isBlank()) // + .filter(line -> !line.startsWith("#")) // + .filter(line -> !line.startsWith("//")) // + .flatMap(ChannelTransformation::splitTransformationString) // + .map(TransformationStep::new) // .toList(); return; } catch (IllegalArgumentException e) { diff --git a/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/binding/generic/ChannelTransformationTest.java b/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/binding/generic/ChannelTransformationTest.java index 128f00dc9..16ce6372f 100644 --- a/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/binding/generic/ChannelTransformationTest.java +++ b/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/binding/generic/ChannelTransformationTest.java @@ -349,4 +349,15 @@ public class ChannelTransformationTest { assertFalse(ChannelTransformation.isValidTransformation("FOO∩BAZ:BAR")); assertFalse(ChannelTransformation.isValidTransformation("FOO∩BAZ(BAR)")); } + + @Test + public void testBlanksAndCommentsAreDiscarded() { + List pattern = List.of("#hash comment", "//double slashes", " # preceded by spaces", + " // ditto for slashes", " ", "\t", "\t\t\t", "\t# preceded by a tab", + "\t // preceded by tab and space"); + + ChannelTransformation transformation = new ChannelTransformation(pattern); + + assertTrue(transformation.isEmpty()); + } }