-
Notifications
You must be signed in to change notification settings - Fork 330
Add getEventType() expression function
#5686
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fc0b4c2
add geteventType expresion
ps48 26e3d80
update antlr grammar, parser and add expression tests
ps48 bc0b866
update expression syntax documentation
ps48 8e2cb29
move expression parse exception check to evaluation exceptions
ps48 01c342c
resolve comments, update getEventTypet test to become parameterized
ps48 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
This file contains hidden or 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
28 changes: 28 additions & 0 deletions
28
...n/src/main/java/org/opensearch/dataprepper/expression/GetEventTypeExpressionFunction.java
This file contains hidden or 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,28 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.dataprepper.expression; | ||
|
|
||
| import org.opensearch.dataprepper.model.event.Event; | ||
| import javax.inject.Named; | ||
| import java.util.function.Function; | ||
| import java.util.List; | ||
|
|
||
| @Named | ||
| public class GetEventTypeExpressionFunction implements ExpressionFunction { | ||
|
|
||
| @Override | ||
| public String getFunctionName() { | ||
| return "getEventType"; | ||
| } | ||
|
|
||
| @Override | ||
| public Object evaluate(final List<Object> args, final Event event, final Function<Object, Object> convertLiteralType) { | ||
| if (!args.isEmpty()) { | ||
| throw new RuntimeException("getEventType() does not take any arguments"); | ||
| } | ||
| return event.getMetadata().getEventType(); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
59 changes: 59 additions & 0 deletions
59
...c/test/java/org/opensearch/dataprepper/expression/GetEventTypeExpressionFunctionTest.java
This file contains hidden or 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,59 @@ | ||
| package org.opensearch.dataprepper.expression; | ||
|
|
||
| import org.opensearch.dataprepper.model.event.Event; | ||
| import org.opensearch.dataprepper.model.event.JacksonEvent; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.equalTo; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
|
|
||
| class GetEventTypeExpressionFunctionTest { | ||
|
|
||
| private GetEventTypeExpressionFunction createObjectUnderTest() { | ||
| return new GetEventTypeExpressionFunction(); | ||
| } | ||
|
|
||
| private Event createTestEvent(final String eventType) { | ||
| return JacksonEvent.builder() | ||
| .withEventType(eventType) | ||
| .withData(Map.of()) | ||
| .build(); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = {"LOG", "TRACE", "METRIC"}) | ||
| void testGetEventTypeReturnsCorrectType(String eventType) { | ||
| GetEventTypeExpressionFunction function = createObjectUnderTest(); | ||
| Event testEvent = createTestEvent(eventType); | ||
|
|
||
| Object result = function.evaluate(List.of(), testEvent, Function.identity()); | ||
|
|
||
| assertThat(result, equalTo(eventType)); | ||
| } | ||
|
|
||
| @Test | ||
| void testGetEventTypeThrowsExceptionWhenArgumentsProvided() { | ||
| GetEventTypeExpressionFunction function = createObjectUnderTest(); | ||
| Event testEvent = createTestEvent("LOG"); | ||
|
|
||
| assertThrows(RuntimeException.class, () -> function.evaluate(List.of("arg1"), testEvent, Function.identity())); | ||
| } | ||
|
|
||
| @Test | ||
| void testGetEventTypeFunctionRegistered() { | ||
| Event testEvent = createTestEvent("event"); | ||
| final GenericExpressionEvaluator evaluator = mock(GenericExpressionEvaluator.class); | ||
| when(evaluator.evaluateConditional("getEventType() == \"event\"", testEvent)).thenReturn(true); | ||
| assertDoesNotThrow(() -> evaluator.evaluateConditional("getEventType() == \"event\"", testEvent)); | ||
| } | ||
| } | ||
This file contains hidden or 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
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.
is this test relevant here ?
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.
@dinujoh This function is to check if the function name registration for
getEventTypeis working as expected within expressions. I have a similar one in generic to check actual execution over mock (as done here):data-prepper/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/GenericExpressionEvaluator_ConditionalIT.java
Lines 252 to 253 in 01c342c
I feel it is good to have both, but not necessary. Please let me know if want it removed, can do it right away.