Skip to content

Commit

Permalink
Update parameter order
Browse files Browse the repository at this point in the history
Signed-off-by: Hai Yan <oeyh@amazon.com>
  • Loading branch information
oeyh committed Feb 2, 2024
1 parent 4d9feec commit 24f8c73
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,18 @@ public Object evaluate(final List<Object> args, Event event, Function<Object, Ob
}

final String delimiter;
final String sourceKey;
if (argStrings.size() == 2) {
final String trimmedDelimiter = argStrings.get(1).substring(1, argStrings.get(1).length() - 1);
final String trimmedDelimiter = argStrings.get(0).substring(1, argStrings.get(0).length() - 1);

// remove slashes used to escape comma
delimiter = trimmedDelimiter.replace("\\\\,", ",");
sourceKey = argStrings.get(1);
} else {
delimiter = ",";
sourceKey = argStrings.get(0);
}

final String sourceKey = argStrings.get(0);
try {
final List<Object> sourceList = event.get(sourceKey, List.class);
return joinList(sourceList, delimiter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ private static Stream<Arguments> validStringExpressionArguments() {
Arguments.of("/status_message+/message", event("{\"status_message\": \""+testString+"\", \"message\":\""+testString2+"\"}"), testString+testString2, String.class),
Arguments.of("getMetadata(\"strAttr\")+\""+testString2+"\"+/key", testEvent, testString+testString2+"value", String.class),
Arguments.of("join(/list)", testEvent, "string,1,true", String.class),
Arguments.of("join(/list, \"\\\\, \")", testEvent, "string, 1, true", String.class),
Arguments.of("join(/list, \" \")", testEvent, "string 1 true", String.class)
Arguments.of("join(\"\\\\, \", /list)", testEvent, "string, 1, true", String.class),
Arguments.of("join(\" \", /list)", testEvent, "string 1 true", String.class)
);
}

Expand All @@ -152,8 +152,8 @@ private static Stream<Arguments> validMapExpressionArguments() {
Event testEvent2 = event("{\"list\":{\"key1\": [\"string\", 1, true], \"key2\": [1,2,3], \"key3\": \"value3\"}}");
return Stream.of(
Arguments.of("join(/list)", testEvent1, Map.of("key", "string,1,true")),
Arguments.of("join(/list, \"\\\\, \")", testEvent1, Map.of("key", "string, 1, true")),
Arguments.of("join(/list, \" \")", testEvent1, Map.of("key", "string 1 true")),
Arguments.of("join(\"\\\\, \", /list)", testEvent1, Map.of("key", "string, 1, true")),
Arguments.of("join(\" \", /list)", testEvent1, Map.of("key", "string 1 true")),
Arguments.of("join(/list)", testEvent2, Map.of("key1", "string,1,true", "key2", "1,2,3", "key3", "value3"))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void testJoinSingleListSuccess(final String sourceKey, final String delimiter, f
if (delimiter == null) {
args = List.of(sourceKey);
} else {
args = List.of(sourceKey, delimiter);
args = List.of(delimiter, sourceKey);
}
Object expressionResult = joinExpressionFunction.evaluate(args, testEvent, testFunction);
assertThat((String)expressionResult, equalTo(expectedOutput));
Expand All @@ -59,7 +59,7 @@ void testJoinListsInMapSuccess(final String sourceKey, final String delimiter, f
if (delimiter == null) {
args = List.of(sourceKey);
} else {
args = List.of(sourceKey, delimiter);
args = List.of(delimiter, sourceKey);
}
Object expressionResult = joinExpressionFunction.evaluate(args, testEvent, testFunction);
assertThat((Map<String, Object>)expressionResult, equalTo(expectedOutput));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ void testCoerceTerminalNodeFunctionTypeWithUnescapedCommaInArgumentsThrowsExcept
final String value = RandomStringUtils.randomAlphabetic(10);
final Event testEvent = createTestEvent(Map.of(key, value));
when(terminalNode.getSymbol()).thenReturn(token);
when(terminalNode.getText()).thenReturn("join(/"+key+", \",\")");
when(terminalNode.getText()).thenReturn("join(\",\" /" +key+")");
when(token.getType()).thenReturn(DataPrepperExpressionParser.Function);
Throwable exception = assertThrows(RuntimeException.class, () -> objectUnderTest.coercePrimaryTerminalNode(terminalNode, testEvent));
assertThat(exception.getMessage(), containsStringIgnoringCase("check if any argument is missing a closing double quote or contains comma that's not escaped with `\\`"));
Expand All @@ -311,7 +311,7 @@ void testCoerceTerminalNodeFunctionTypeWithEscapedCommaInArgumentsReturnsExpecte
final String output = RandomStringUtils.randomAlphabetic(10);
final Event testEvent = createTestEvent(Map.of(key, value));
when(terminalNode.getSymbol()).thenReturn(token);
when(terminalNode.getText()).thenReturn("join(/"+key+", \"\\\\,\")");
when(terminalNode.getText()).thenReturn("join(\"\\\\,\", /"+key+")");
when(expressionFunctionProvider.provideFunction(eq("join"), any(List.class), any(Event.class), any(Function.class))).thenReturn(output);
when(token.getType()).thenReturn(DataPrepperExpressionParser.Function);
assertThat(objectUnderTest.coercePrimaryTerminalNode(terminalNode, testEvent), equalTo(output));
Expand Down

0 comments on commit 24f8c73

Please sign in to comment.