-
Notifications
You must be signed in to change notification settings - Fork 207
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
Add startsWith expression function #4840
Conversation
a7373c5
to
cc96fb9
Compare
final ExpressionFunction startsWithExpressionFunction = createObjectUnderTest(); | ||
final String testKey = "test_key"; | ||
|
||
assertThrows(RuntimeException.class, () -> startsWithExpressionFunction.evaluate(List.of("abcd"), testEvent, mock(Function.class))); |
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 would be better to split this test up using arguments provider.
|
||
private Event testEvent; | ||
|
||
@BeforeEach |
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.
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() { |
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.
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)); |
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.
return false; | ||
} | ||
if (!(obj instanceof String)) { | ||
throw new RuntimeException(String.format("startsWith() takes only string type arguments. \"%s\" is not of type string", obj)); |
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.
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; |
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.
Rename for clarity. Perhaps stringOrKey
.
cc96fb9
to
a507bdd
Compare
Signed-off-by: Taylor Gray <[email protected]>
a507bdd
to
a68fb12
Compare
Description
Adds a
startsWith
function to Data Prepper expressions, similar to thecontains
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.