-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add startsWith expression function #4840
Merged
graytaylor0
merged 1 commit into
opensearch-project:main
from
graytaylor0:StartsWithFunction
Aug 16, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...ion/src/main/java/org/opensearch/dataprepper/expression/StartsWithExpressionFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.opensearch.dataprepper.expression; | ||
|
||
import org.opensearch.dataprepper.model.event.Event; | ||
|
||
import javax.inject.Named; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
@Named | ||
public class StartsWithExpressionFunction implements ExpressionFunction { | ||
private static final int NUMBER_OF_ARGS = 2; | ||
|
||
static final String STARTS_WITH_FUNCTION_NAME = "startsWith"; | ||
@Override | ||
public String getFunctionName() { | ||
return STARTS_WITH_FUNCTION_NAME; | ||
} | ||
|
||
@Override | ||
public Object evaluate( | ||
final List<Object> args, | ||
final Event event, | ||
final Function<Object, Object> convertLiteralType) { | ||
|
||
if (args.size() != NUMBER_OF_ARGS) { | ||
throw new RuntimeException("startsWith() takes exactly two arguments"); | ||
} | ||
|
||
String[] strArgs = new String[NUMBER_OF_ARGS]; | ||
for (int i = 0; i < NUMBER_OF_ARGS; i++) { | ||
Object arg = args.get(i); | ||
if (!(arg instanceof String)) { | ||
throw new RuntimeException(String.format("startsWith() takes only string type arguments. \"%s\" is not of type string", arg)); | ||
} | ||
String stringOrKey = (String) arg; | ||
if (stringOrKey.charAt(0) == '"') { | ||
strArgs[i] = stringOrKey.substring(1, stringOrKey.length()-1); | ||
} else if (stringOrKey.charAt(0) == '/') { | ||
Object obj = event.get(stringOrKey, Object.class); | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (!(obj instanceof String)) { | ||
throw new RuntimeException(String.format("startsWith() only operates on string types. The value at \"%s\" is \"%s\" which is not a string type.", stringOrKey, obj)); | ||
} | ||
strArgs[i] = (String)obj; | ||
} else { | ||
throw new RuntimeException(String.format("Arguments to startsWith() must be a literal string or a Json Pointer. \"%s\" is not string literal or json pointer", stringOrKey)); | ||
} | ||
} | ||
return strArgs[0].startsWith(strArgs[1]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
...src/test/java/org/opensearch/dataprepper/expression/StartsWithExpressionFunctionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package org.opensearch.dataprepper.expression; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.opensearch.dataprepper.model.event.Event; | ||
import org.opensearch.dataprepper.model.event.JacksonEvent; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.function.Function; | ||
import java.util.stream.Stream; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.mock; | ||
import static org.opensearch.dataprepper.expression.StartsWithExpressionFunction.STARTS_WITH_FUNCTION_NAME; | ||
|
||
public class StartsWithExpressionFunctionTest { | ||
|
||
private Event testEvent; | ||
|
||
private Event createTestEvent(final Object data) { | ||
return JacksonEvent.builder().withEventType("event").withData(data).build(); | ||
} | ||
|
||
private ExpressionFunction createObjectUnderTest() { | ||
return new StartsWithExpressionFunction(); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("validStartsWithProvider") | ||
void startsWith_returns_expected_result_when_evaluated( | ||
final String value, final String prefix, final boolean expectedResult) { | ||
final String key = "test_key"; | ||
testEvent = createTestEvent(Map.of(key, value)); | ||
|
||
final ExpressionFunction objectUnderTest = createObjectUnderTest(); | ||
assertThat(objectUnderTest.getFunctionName(), equalTo(STARTS_WITH_FUNCTION_NAME)); | ||
|
||
final Object result = objectUnderTest.evaluate(List.of("/" + key, "\"" + prefix + "\""), testEvent, mock(Function.class)); | ||
|
||
assertThat(result, equalTo(expectedResult)); | ||
} | ||
|
||
@Test | ||
void startsWith_with_a_key_as_the_prefix_returns_expected_result() { | ||
|
||
final String prefixKey = "prefix"; | ||
final String prefixValue = "te"; | ||
|
||
final String key = "test_key"; | ||
final String value = "test"; | ||
testEvent = createTestEvent(Map.of(key, value, prefixKey, prefixValue)); | ||
|
||
final ExpressionFunction objectUnderTest = createObjectUnderTest(); | ||
assertThat(objectUnderTest.getFunctionName(), equalTo(STARTS_WITH_FUNCTION_NAME)); | ||
|
||
final Object result = objectUnderTest.evaluate(List.of("/" + key, "/" + prefixKey), testEvent, mock(Function.class)); | ||
|
||
assertThat(result, equalTo(true)); | ||
} | ||
|
||
@Test | ||
void startsWith_returns_false_when_key_does_not_exist_in_Event() { | ||
final String key = "test_key"; | ||
testEvent = createTestEvent(Map.of(UUID.randomUUID().toString(), UUID.randomUUID().toString())); | ||
|
||
final ExpressionFunction startsWithExpressionFunction = createObjectUnderTest(); | ||
final Object result = startsWithExpressionFunction.evaluate(List.of("/" + key, "\"abcd\""), testEvent, mock(Function.class)); | ||
|
||
assertThat(result, equalTo(false)); | ||
} | ||
|
||
@Test | ||
void startsWith_without_2_arguments_throws_RuntimeException() { | ||
final ExpressionFunction startsWithExpressionFunction = createObjectUnderTest(); | ||
assertThrows(RuntimeException.class, () -> startsWithExpressionFunction.evaluate(List.of("abcd"), testEvent, mock(Function.class))); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("invalidStartsWithProvider") | ||
void invalid_startsWith_arguments_throws_RuntimeException(final String firstArg, final Object secondArg, final Object value) { | ||
final ExpressionFunction startsWithExpressionFunction = createObjectUnderTest(); | ||
final String testKey = "test_key"; | ||
|
||
assertThrows(RuntimeException.class, () -> startsWithExpressionFunction.evaluate(List.of(firstArg, secondArg), createTestEvent(Map.of(testKey, value)), mock(Function.class))); | ||
} | ||
|
||
private static Stream<Arguments> validStartsWithProvider() { | ||
return Stream.of( | ||
Arguments.of("{test", "{te", true), | ||
Arguments.of("{test", "{", true), | ||
Arguments.of("test", "{", false), | ||
Arguments.of("MyPrefix", "My", true), | ||
Arguments.of("MyPrefix", "Prefix", false) | ||
); | ||
} | ||
|
||
private static Stream<Arguments> invalidStartsWithProvider() { | ||
return Stream.of( | ||
Arguments.of("\"abc\"", "/test_key", 1234), | ||
Arguments.of("abcd", "/test_key", "value"), | ||
Arguments.of("\"abcd\"", "/test_key", 1234), | ||
Arguments.of("\"/test_key\"", 1234, "value") | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you are supporting a key for the substring part. Add a unit test for that as well.