From c9271810dbd44a2ef8935203b1e674664e5fe39e Mon Sep 17 00:00:00 2001 From: Arunan Sugunakumar Date: Mon, 15 Jul 2024 13:11:01 +0530 Subject: [PATCH] Simplify datamapper map function name --- .../datamapper/engine/core/executors/ScriptExecutor.java | 2 +- .../datamapper/engine/core/mapper/MappingResource.java | 6 ++++++ .../datamapper/engine/utils/DataMapperEngineConstants.java | 3 +++ .../wso2/carbon/mediator/datamapper/DataMapperMediator.java | 2 +- 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/core/executors/ScriptExecutor.java b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/core/executors/ScriptExecutor.java index f5a69b90ee9..abc4a8fcec1 100644 --- a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/core/executors/ScriptExecutor.java +++ b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/core/executors/ScriptExecutor.java @@ -40,6 +40,7 @@ import static org.wso2.carbon.mediator.datamapper.engine.utils.DataMapperEngineConstants.DEFAULT_ENGINE_NAME; import static org.wso2.carbon.mediator.datamapper.engine.utils.DataMapperEngineConstants.ENCODE_CHAR_HYPHEN; import static org.wso2.carbon.mediator.datamapper.engine.utils.DataMapperEngineConstants.HYPHEN; +import static org.wso2.carbon.mediator.datamapper.engine.utils.DataMapperEngineConstants.INPUT_VARIABLE_IDENTIFIER; import static org.wso2.carbon.mediator.datamapper.engine.utils.DataMapperEngineConstants.NASHORN_ENGINE_NAME; import static org.wso2.carbon.mediator.datamapper.engine.utils.DataMapperEngineConstants.PROPERTIES_OBJECT_NAME; @@ -50,7 +51,6 @@ public class ScriptExecutor implements Executor { public static final String PROPERTIES_IDENTIFIER = "properties"; - public static final String INPUT_VARIABLE_IDENTIFIER = "inputVariables"; public static final String OPENJDK_NASHORN_CLASSNAME = "org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory"; private static final Log log = LogFactory.getLog(ScriptExecutor.class); private ScriptEngine scriptEngine; diff --git a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/core/mapper/MappingResource.java b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/core/mapper/MappingResource.java index 86ee5488f8b..085363219da 100644 --- a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/core/mapper/MappingResource.java +++ b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/core/mapper/MappingResource.java @@ -39,6 +39,8 @@ import static org.wso2.carbon.mediator.datamapper.engine.utils.DataMapperEngineConstants.JS_STRINGIFY; import static org.wso2.carbon.mediator.datamapper.engine.utils.DataMapperEngineConstants.HYPHEN; import static org.wso2.carbon.mediator.datamapper.engine.utils.DataMapperEngineConstants.ENCODE_CHAR_HYPHEN; +import static org.wso2.carbon.mediator.datamapper.engine.utils.DataMapperEngineConstants.MAP_FUNCTION; +import static org.wso2.carbon.mediator.datamapper.engine.utils.DataMapperEngineConstants.MAP_FUNCTION_NAME; public class MappingResource { @@ -147,6 +149,10 @@ private JSFunction createFunction(InputStream mappingConfig, String outputType) propertiesList.add(match.group(2) + "['" + match.group(3) + "']"); } + if (jsFunctionBody.contains(MAP_FUNCTION_NAME)) { + fnName = MAP_FUNCTION; + } + if (fnName != null) { return new JSFunction(fnName, jsFunctionBody); } else { diff --git a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/utils/DataMapperEngineConstants.java b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/utils/DataMapperEngineConstants.java index 5917f8928d9..76990c29ae7 100644 --- a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/utils/DataMapperEngineConstants.java +++ b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/utils/DataMapperEngineConstants.java @@ -55,6 +55,9 @@ public class DataMapperEngineConstants { public static final String BRACKET_CLOSE = ")"; public static final String FUNCTION_NAME_CONST_1 = "map_S_"; public static final String FUNCTION_NAME_CONST_2 = "_S_"; + public static final String INPUT_VARIABLE_IDENTIFIER = "inputVariables"; + public static final String MAP_FUNCTION_NAME = "mapFunction"; + public static final String MAP_FUNCTION = "mapFunction(JSON.parse(" + INPUT_VARIABLE_IDENTIFIER + ")"; public static final String NAME_SEPERATOR = "_Separat0r_"; public static final String ENCODE_CHAR_HYPHEN = "_EnC0DeCHaRHyPh3n_"; public static final String HYPHEN = "-"; diff --git a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/src/main/java/org/wso2/carbon/mediator/datamapper/DataMapperMediator.java b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/src/main/java/org/wso2/carbon/mediator/datamapper/DataMapperMediator.java index 0ea8e45855f..46e50678ec3 100644 --- a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/src/main/java/org/wso2/carbon/mediator/datamapper/DataMapperMediator.java +++ b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/src/main/java/org/wso2/carbon/mediator/datamapper/DataMapperMediator.java @@ -85,8 +85,8 @@ import static org.wso2.carbon.mediator.datamapper.config.xml.DataMapperMediatorConstants.SYNAPSE_CONTEXT; import static org.wso2.carbon.mediator.datamapper.config.xml.DataMapperMediatorConstants.TRANSPORT_CONTEXT; import static org.wso2.carbon.mediator.datamapper.config.xml.DataMapperMediatorConstants.TRANSPORT_HEADERS; +import static org.wso2.carbon.mediator.datamapper.engine.utils.DataMapperEngineConstants.INPUT_VARIABLE_IDENTIFIER; import static org.wso2.carbon.mediator.datamapper.engine.utils.DataMapperEngineConstants.ORG_APACHE_SYNAPSE_DATAMAPPER_EXECUTOR_POOL_SIZE; -import static org.wso2.carbon.mediator.datamapper.engine.core.executors.ScriptExecutor.INPUT_VARIABLE_IDENTIFIER; import static org.wso2.carbon.mediator.datamapper.engine.utils.DataMapperEngineConstants.ENCODE_CHAR_HYPHEN; import static org.wso2.carbon.mediator.datamapper.engine.utils.DataMapperEngineConstants.HYPHEN; import static org.wso2.carbon.mediator.datamapper.engine.utils.DataMapperEngineConstants.PROPERTIES_OBJECT_NAME;