Skip to content
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
merged 1 commit into from
Aug 16, 2024

Conversation

graytaylor0
Copy link
Member

@graytaylor0 graytaylor0 commented Aug 15, 2024

Description

Adds a startsWith function to Data Prepper expressions, similar to the contains function, but for checking if a String has a prefix of some other String.

Check List

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

final ExpressionFunction startsWithExpressionFunction = createObjectUnderTest();
final String testKey = "test_key";

assertThrows(RuntimeException.class, () -> startsWithExpressionFunction.evaluate(List.of("abcd"), testEvent, mock(Function.class)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to split this test up using arguments provider.


private Event testEvent;

@BeforeEach
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this.

assertThrows(RuntimeException.class, () -> startsWithExpressionFunction.evaluate(List.of("\"abc\"", "/"+testKey), createTestEvent(Map.of(testKey, 1234)), mock(Function.class)));
}

private static Stream<Arguments> ValidStartsWithProvider() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static Stream<Arguments> ValidStartsWithProvider() {
private static Stream<Arguments> validStartsWithProvider() {

Give a method name

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));
Copy link
Member

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.

return false;
}
if (!(obj instanceof String)) {
throw new RuntimeException(String.format("startsWith() takes only string type arguments. \"%s\" is not of type string", obj));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new RuntimeException(String.format("startsWith() takes only string type arguments. \"%s\" is not of type string", obj));
throw new RuntimeException(String.format("startsWith() only operates on string types. The value at \"%s"\ is \"%s\" which is not a string type.", str, obj));

This error may be confusing. In this case, startsWith() is given a key and the value is not of type string.

if (!(arg instanceof String)) {
throw new RuntimeException(String.format("startsWith() takes only string type arguments. \"%s\" is not of type string", arg));
}
String str = (String) arg;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename for clarity. Perhaps stringOrKey.

@dlvenable dlvenable added this to the v2.9 milestone Aug 16, 2024
dlvenable
dlvenable previously approved these changes Aug 16, 2024
Signed-off-by: Taylor Gray <tylgry@amazon.com>
@graytaylor0 graytaylor0 merged commit 1ac6df2 into opensearch-project:main Aug 16, 2024
43 of 47 checks passed
@graytaylor0 graytaylor0 deleted the StartsWithFunction branch August 16, 2024 21:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants