Skip to content

Commit

Permalink
Replace try-catch with if check (opensearch-project#4144)
Browse files Browse the repository at this point in the history
Signed-off-by: Hai Yan <[email protected]>
  • Loading branch information
oeyh authored Feb 19, 2024
1 parent bd9357d commit b77815c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ public Object evaluate(final List<Object> args, Event event, Function<Object, Ob
}

try {
final List<Object> sourceList = event.get(sourceKey, List.class);
return joinList(sourceList, delimiter);
} catch (Exception e) {
try {
if (event.isValueAList(sourceKey)) {
final List<Object> sourceList = event.get(sourceKey, List.class);
return joinList(sourceList, delimiter);
} else {
final Map<String, Object> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arguments> joinSingleList() {
final String inputData = "{\"list\":[\"string\", 1, true]}";
return Stream.of(
Expand Down

0 comments on commit b77815c

Please sign in to comment.