From 55522208702e4c1daca4cc05d6f3c07ef7a475ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20L=27hopital?= Date: Mon, 17 Oct 2022 19:43:12 +0200 Subject: [PATCH] [MAP] Adds fallback to original value when input not found (#13560) * [MAP] Adding fallback to original value Solves #10092 Signed-off-by: clinique --- bundles/org.openhab.transform.map/README.md | 5 ++++- .../conf/transform/map/doorstatus_fallback.map | 6 ++++++ .../transform/map/internal/MapTransformationService.java | 6 ++++-- .../map/internal/MapTransformationServiceTest.java | 7 ++++++- 4 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 bundles/org.openhab.transform.map/conf/transform/map/doorstatus_fallback.map diff --git a/bundles/org.openhab.transform.map/README.md b/bundles/org.openhab.transform.map/README.md index e1aecbd176e..c70d7fbaf7e 100644 --- a/bundles/org.openhab.transform.map/README.md +++ b/bundles/org.openhab.transform.map/README.md @@ -7,7 +7,9 @@ This file should be in property syntax, i.e. simple lines with "key=value" pairs The file format is documented [here](https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html#load-java.io.Reader-). To organize the various transformations one might use subfolders. -A default value can be provided if no matching entry is found by using "=value" syntax +A default value can be provided if no matching entry is found by using "=value" syntax. +Defining this default value using `_source_` would then return the non transformed input string. + ## Example @@ -31,6 +33,7 @@ white\ space=using escape | `white space` | `using escape` | | `anything` | `default` | + ## Usage as a Profile The functionality of this `TransformationService` can be used in a `Profile` on an `ItemChannelLink` too. diff --git a/bundles/org.openhab.transform.map/conf/transform/map/doorstatus_fallback.map b/bundles/org.openhab.transform.map/conf/transform/map/doorstatus_fallback.map new file mode 100644 index 00000000000..05b912fc561 --- /dev/null +++ b/bundles/org.openhab.transform.map/conf/transform/map/doorstatus_fallback.map @@ -0,0 +1,6 @@ +CLOSED=closed +OPEN=open +-=- + +# Default mapping for missing keys +=_source_ diff --git a/bundles/org.openhab.transform.map/src/main/java/org/openhab/transform/map/internal/MapTransformationService.java b/bundles/org.openhab.transform.map/src/main/java/org/openhab/transform/map/internal/MapTransformationService.java index c70bfa37ffb..4f32bec8c34 100644 --- a/bundles/org.openhab.transform.map/src/main/java/org/openhab/transform/map/internal/MapTransformationService.java +++ b/bundles/org.openhab.transform.map/src/main/java/org/openhab/transform/map/internal/MapTransformationService.java @@ -52,12 +52,12 @@ import org.slf4j.LoggerFactory; "openhab.transform=MAP" }) public class MapTransformationService implements TransformationService, ConfigOptionProvider, RegistryChangeListener { - private final Logger logger = LoggerFactory.getLogger(MapTransformationService.class); - + private static final String SOURCE_VALUE = "_source_"; private static final String PROFILE_CONFIG_URI = "profile:transform:MAP"; private static final String CONFIG_PARAM_FUNCTION = "function"; private static final Set SUPPORTED_CONFIGURATION_TYPES = Set.of("map"); + private final Logger logger = LoggerFactory.getLogger(MapTransformationService.class); private final TransformationRegistry transformationRegistry; private final Map cachedTransformations = new ConcurrentHashMap<>(); @@ -89,6 +89,8 @@ public class MapTransformationService target = properties.getProperty(""); if (target == null) { throw new TransformationException("Target value not found in map for '" + source + "'"); + } else if (SOURCE_VALUE.equals(target)) { + target = source; } } diff --git a/bundles/org.openhab.transform.map/src/test/java/org/openhab/transform/map/internal/MapTransformationServiceTest.java b/bundles/org.openhab.transform.map/src/test/java/org/openhab/transform/map/internal/MapTransformationServiceTest.java index 5718730b670..675f93d1fd7 100644 --- a/bundles/org.openhab.transform.map/src/test/java/org/openhab/transform/map/internal/MapTransformationServiceTest.java +++ b/bundles/org.openhab.transform.map/src/test/java/org/openhab/transform/map/internal/MapTransformationServiceTest.java @@ -53,6 +53,7 @@ public class MapTransformationServiceTest extends JavaTest { private static final String NON_DEFAULTED_TRANSFORMATION_DE = "map" + File.separator + "doorstatus_de.map"; private static final String NON_DEFAULTED_TRANSFORMATION_FR = "map" + File.separator + "doorstatus_fr.map"; private static final String DEFAULTED_TRANSFORMATION = "map" + File.separator + "doorstatus_defaulted.map"; + private static final String FALLBACK_TRANSFORMATION = "map" + File.separator + "doorstatus_fallback.map"; private static final String UNKNOWN_TRANSFORMATION = "map" + File.separator + "de.map"; private static final String SRC_FOLDER = "conf" + File.separator + "transform"; @@ -103,6 +104,11 @@ public class MapTransformationServiceTest extends JavaTest { assertEquals("Default Value", processor.transform(DEFAULTED_TRANSFORMATION, SOURCE_UNKNOWN)); } + @Test + public void testTransformSucceedsWithFallbackDefault() throws TransformationException { + assertEquals(SOURCE_UNKNOWN, processor.transform(FALLBACK_TRANSFORMATION, SOURCE_UNKNOWN)); + } + @Test public void testTransformFailsOnUnknownTransformation() { assertThrows(TransformationException.class, () -> processor.transform(UNKNOWN_TRANSFORMATION, SOURCE_CLOSED)); @@ -123,7 +129,6 @@ public class MapTransformationServiceTest extends JavaTest { public void setTransformationIsNotUpdatedIfOldElementMissing() throws TransformationException { // update configuration Transformation transformationDE = Objects.requireNonNull(configurationMap.get(NON_DEFAULTED_TRANSFORMATION_DE)); - Transformation transformationFR = Objects.requireNonNull(configurationMap.get(NON_DEFAULTED_TRANSFORMATION_FR)); Transformation transformationModified = new Transformation(transformationDE.getUID(), transformationDE.getLabel(), transformationDE.getType(), transformationDE.getConfiguration()); processor.updated(transformationDE, transformationModified);