From b77815c0ecf5361a60e6976b19ca4479673cf498 Mon Sep 17 00:00:00 2001 From: Hai Yan <8153134+oeyh@users.noreply.github.com> Date: Mon, 19 Feb 2024 11:17:22 -0600 Subject: [PATCH] Replace try-catch with if check (#4144) Signed-off-by: Hai Yan --- .../expression/JoinExpressionFunction.java | 12 ++++++------ .../expression/JoinExpressionFunctionTest.java | 7 +++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/JoinExpressionFunction.java b/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/JoinExpressionFunction.java index 6ca35782af..d52729757b 100644 --- a/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/JoinExpressionFunction.java +++ b/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/JoinExpressionFunction.java @@ -54,15 +54,15 @@ public Object evaluate(final List args, Event event, Function sourceList = event.get(sourceKey, List.class); - return joinList(sourceList, delimiter); - } catch (Exception e) { - try { + if (event.isValueAList(sourceKey)) { + final List sourceList = event.get(sourceKey, List.class); + return joinList(sourceList, delimiter); + } else { final Map sourceMap = event.get(sourceKey, Map.class); return joinListsInMap(sourceMap, delimiter); - } catch (Exception ex) { - throw new RuntimeException("Unable to perform join function on " + sourceKey, ex); } + } catch (Exception ex) { + throw new RuntimeException("Unable to perform join function on " + sourceKey, ex); } } diff --git a/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/JoinExpressionFunctionTest.java b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/JoinExpressionFunctionTest.java index 48da627e5e..3ec60a73c5 100644 --- a/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/JoinExpressionFunctionTest.java +++ b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/JoinExpressionFunctionTest.java @@ -94,6 +94,13 @@ void testSourceFieldNotExistsInEventThrowsException() { () -> joinExpressionFunction.evaluate(List.of("/missingKey"), testEvent, testFunction)); } + @Test + void testSourceFieldNotListOrMapThrowsException() { + testEvent = createTestEvent(Map.of("key", "value")); + assertThrows(RuntimeException.class, + () -> joinExpressionFunction.evaluate(List.of("/key"), testEvent, testFunction)); + } + private static Stream joinSingleList() { final String inputData = "{\"list\":[\"string\", 1, true]}"; return Stream.of(