Skip to content

Commit 5cf91cd

Browse files
authored
Filter out blank lines and comments in a multi-line transformations (openhab#4357)
Signed-off-by: Jimmy Tanagra <[email protected]>
1 parent 1145613 commit 5cf91cd

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/binding/generic/ChannelTransformation.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ public ChannelTransformation(@Nullable String transformationString) {
5656
public ChannelTransformation(@Nullable List<String> transformationStrings) {
5757
if (transformationStrings != null) {
5858
try {
59-
transformationSteps = transformationStrings.stream()
60-
.flatMap(ChannelTransformation::splitTransformationString).map(TransformationStep::new)
59+
transformationSteps = transformationStrings.stream() //
60+
.map(String::trim) //
61+
.filter(line -> !line.isBlank()) //
62+
.filter(line -> !line.startsWith("#")) //
63+
.filter(line -> !line.startsWith("//")) //
64+
.flatMap(ChannelTransformation::splitTransformationString) //
65+
.map(TransformationStep::new) //
6166
.toList();
6267
return;
6368
} catch (IllegalArgumentException e) {

bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/binding/generic/ChannelTransformationTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,15 @@ public void testIsValidTransform() {
349349
assertFalse(ChannelTransformation.isValidTransformation("FOO∩BAZ:BAR"));
350350
assertFalse(ChannelTransformation.isValidTransformation("FOO∩BAZ(BAR)"));
351351
}
352+
353+
@Test
354+
public void testBlanksAndCommentsAreDiscarded() {
355+
List<String> pattern = List.of("#hash comment", "//double slashes", " # preceded by spaces",
356+
" // ditto for slashes", " ", "\t", "\t\t\t", "\t# preceded by a tab",
357+
"\t // preceded by tab and space");
358+
359+
ChannelTransformation transformation = new ChannelTransformation(pattern);
360+
361+
assertTrue(transformation.isEmpty());
362+
}
352363
}

0 commit comments

Comments
 (0)