From 62bcb4cbb7c0057cc18f734579effc501f019af8 Mon Sep 17 00:00:00 2001 From: Vishal Boinapalli Date: Wed, 19 Jul 2023 14:55:16 -0700 Subject: [PATCH] Changed non-exact mathching logic Signed-off-by: Vishal Boinapalli --- .../processor/translate/TranslateProcessor.java | 6 +++++- .../processor/translate/TranslateProcessorTest.java | 10 ++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/data-prepper-plugins/translate-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/translate/TranslateProcessor.java b/data-prepper-plugins/translate-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/translate/TranslateProcessor.java index d627f7a77e..c0d4a57c07 100644 --- a/data-prepper-plugins/translate-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/translate/TranslateProcessor.java +++ b/data-prepper-plugins/translate-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/translate/TranslateProcessor.java @@ -164,9 +164,13 @@ private Optional matchesPatternEntry(final String sourceValue, TargetsPa final boolean exact = targetConfig.getRegexParameterConfiguration().getExact(); for (Pattern pattern : compiledPatterns.keySet()) { Matcher matcher = pattern.matcher(sourceValue); - if (matcher.matches() || (!exact && matcher.find())) { + if (matcher.matches()) { return Optional.of(compiledPatterns.get(pattern)); } + if(!exact && matcher.find()) { + String targetValue = (String)compiledPatterns.get(pattern); + return Optional.of(matcher.replaceAll(targetValue)); + } } return Optional.empty(); } diff --git a/data-prepper-plugins/translate-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/translate/TranslateProcessorTest.java b/data-prepper-plugins/translate-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/translate/TranslateProcessorTest.java index fbefe75eeb..a21fae7255 100644 --- a/data-prepper-plugins/translate-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/translate/TranslateProcessorTest.java +++ b/data-prepper-plugins/translate-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/translate/TranslateProcessorTest.java @@ -391,7 +391,7 @@ void test_non_exact_matching() { when(mockRegexConfig.getExact()).thenReturn(false); when(mockRegexConfig.getPatterns()).thenReturn(createMapEntries( createMapping("^(1[0-9]|20)$", "patternValue1"), - createMapping("foo", "bar2"))); + createMapping("foo", "bar"))); targetsParameterConfig = new TargetsParameterConfig(null, "targetField", mockRegexConfig, null, null, null); when(mappingsParameterConfig.getTargetsParameterConfigs()).thenReturn(List.of(targetsParameterConfig)); @@ -400,7 +400,13 @@ void test_non_exact_matching() { final List> translatedRecords = (List>) processor.doExecute(Collections.singletonList(record)); assertTrue(translatedRecords.get(0).getData().containsKey("targetField")); - assertThat(translatedRecords.get(0).getData().get("targetField", String.class), is("bar2")); + assertThat(translatedRecords.get(0).getData().get("targetField", String.class), is("barter")); + + final Record replaceAllRecord = getEvent("foofoo"); + final List> translatedReplaceAllRecords = (List>) processor.doExecute(Collections.singletonList(replaceAllRecord)); + + assertTrue(translatedReplaceAllRecords.get(0).getData().containsKey("targetField")); + assertThat(translatedReplaceAllRecords.get(0).getData().get("targetField", String.class), is("barbar")); final Record regexRecord = getEvent("15"); final List> translatedRegexRecords = (List>) processor.doExecute(Collections.singletonList(regexRecord));