generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 273
SubList function #5535
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
Open
charan2628
wants to merge
2
commits into
opensearch-project:main
Choose a base branch
from
charan2628:sublist-function-5529
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
SubList function #5535
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -167,6 +167,7 @@ fragment | |
| FunctionArg | ||
| : JsonPointer | ||
| | String | ||
| | SUBTRACT? Integer | ||
| ; | ||
|
|
||
| variableIdentifier | ||
|
|
||
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
68 changes: 68 additions & 0 deletions
68
...ession/src/main/java/org/opensearch/dataprepper/expression/SubListExpressionFunction.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,68 @@ | ||
| package org.opensearch.dataprepper.expression; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.function.Function; | ||
|
|
||
| import org.opensearch.dataprepper.model.event.Event; | ||
|
|
||
| import javax.inject.Named; | ||
|
|
||
| @Named | ||
| public class SubListExpressionFunction implements ExpressionFunction { | ||
|
|
||
| @Override | ||
| public String getFunctionName() { | ||
| return "subList"; | ||
| } | ||
|
|
||
| @Override | ||
| public Object evaluate(List<Object> args, Event event, Function<Object, Object> convertLiteralType) { | ||
| if (args.size() != 3) { | ||
| throw new IllegalArgumentException("subList() takes 3 arguments"); | ||
| } | ||
| if (!(args.get(0) instanceof String)) { | ||
| throw new IllegalArgumentException("subList() takes 1st argument as string type"); | ||
| } | ||
| int startIndex, endIndex; | ||
| try { | ||
| if (args.get(1) instanceof Integer) { | ||
| startIndex = (Integer) args.get(1); | ||
| } else { | ||
| String str = (String) args.get(1); | ||
| startIndex = Integer.parseInt(str.substring(1, str.length() - 1)); | ||
| } | ||
| if (args.get(2) instanceof Integer) { | ||
| endIndex = (Integer) args.get(2); | ||
| } else { | ||
| String str = (String) args.get(2); | ||
| endIndex = Integer.parseInt(str.substring(1, str.length() - 1)); | ||
| } | ||
| } catch (NumberFormatException | ClassCastException e) { | ||
| throw new IllegalArgumentException("subList() takes 2nd and 3rd arguments as integers"); | ||
| } | ||
|
|
||
| String key = (String)args.get(0); | ||
| final Object value = event.get(key, Object.class); | ||
| if (value == null) return null; | ||
| if (!(value instanceof List)) { | ||
| throw new RuntimeException(key + " is not of list type"); | ||
| } | ||
| List<?> sourceList = (List<?>)value; | ||
|
|
||
| if (endIndex == -1) endIndex = sourceList.size(); | ||
|
|
||
| if (startIndex < 0 || startIndex >= sourceList.size()) { | ||
| throw new RuntimeException("subList() start index should be between 0 and list length (inclusive)"); | ||
| } | ||
|
|
||
| if (endIndex < 0 || endIndex > sourceList.size()) { | ||
charan2628 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new RuntimeException("subList() end index should be between 0 and list length or -1 for list length (exclusive)"); | ||
| } | ||
| if (startIndex > endIndex) { | ||
| throw new RuntimeException("subList() start index should be less than or equal to end index"); | ||
| } | ||
| return new ArrayList<>(sourceList.subList(startIndex, endIndex)); | ||
| } | ||
|
|
||
| } | ||
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
174 changes: 174 additions & 0 deletions
174
...on/src/test/java/org/opensearch/dataprepper/expression/SubListExpressionFunctionTest.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,174 @@ | ||
| package org.opensearch.dataprepper.expression; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.opensearch.dataprepper.model.event.Event; | ||
| import org.opensearch.dataprepper.model.event.JacksonEvent; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.hamcrest.CoreMatchers.equalTo; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
|
|
||
| public class SubListExpressionFunctionTest { | ||
| private SubListExpressionFunction subListExpressionFunction; | ||
| private Event testEvent; | ||
| private Function<Object, Object> testFunction; | ||
|
|
||
| private Event createTestEvent(final Object data) { | ||
| return JacksonEvent.builder().withEventType("event").withData(data).build(); | ||
| } | ||
|
|
||
|
|
||
| public SubListExpressionFunction createObjectUnderTest() { | ||
| testFunction = mock(Function.class); | ||
| return new SubListExpressionFunction(); | ||
| } | ||
|
|
||
| @Test | ||
| void testFunctionName() { | ||
| subListExpressionFunction = createObjectUnderTest(); | ||
| assertEquals("subList", subListExpressionFunction.getFunctionName()); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithValidArguments() { | ||
| subListExpressionFunction = createObjectUnderTest(); | ||
| List<Integer> testList = List.of(1, 2, 3, 4); | ||
| testEvent = createTestEvent(Map.of("list", testList)); | ||
| assertThat(subListExpressionFunction.evaluate(List.of("/list", "\"1\"", "\"3\""), testEvent, testFunction), equalTo(List.of(2, 3))); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithValidArgumentsCase2() { | ||
| subListExpressionFunction = createObjectUnderTest(); | ||
| List<Integer> testList = List.of(1, 2, 3, 4); | ||
| testEvent = createTestEvent(Map.of("list", testList)); | ||
| assertThat(subListExpressionFunction.evaluate(List.of("/list", "\"1\"", "\"3\""), testEvent, testFunction), equalTo(List.of(2, 3))); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithValidArgumentsCase3() { | ||
| subListExpressionFunction = createObjectUnderTest(); | ||
| List<Integer> testList = List.of(1, 2, 3, 4); | ||
| testEvent = createTestEvent(Map.of("main", Map.of("list", testList))); | ||
| assertThat(subListExpressionFunction.evaluate(List.of("/main/list", "\"1\"", "\"3\""), testEvent, testFunction), equalTo(List.of(2, 3))); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithValidArgumentsCase4() { | ||
| subListExpressionFunction = createObjectUnderTest(); | ||
| List<Integer> testList = List.of(1, 2, 3, 4); | ||
| testEvent = createTestEvent(Map.of("main", Map.of("list", testList))); | ||
| assertThat(subListExpressionFunction.evaluate(List.of("/main/list", "\"1\"", "\"-1\""), testEvent, testFunction), equalTo(List.of(2, 3, 4))); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithValidArgumentsCase5() { | ||
| subListExpressionFunction = createObjectUnderTest(); | ||
| List<Integer> testList = List.of(1, 2, 3, 4); | ||
| testEvent = createTestEvent(Map.of("list", testList)); | ||
| assertThat(subListExpressionFunction.evaluate(List.of("/list", 1, 3), testEvent, testFunction), equalTo(List.of(2, 3))); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithOutOfBoundArgumentsCase1() { | ||
| subListExpressionFunction = createObjectUnderTest(); | ||
| List<Integer> testList = List.of(1, 2, 3, 4, 5, 6); | ||
| testEvent = createTestEvent(Map.of("list", testList)); | ||
| Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", "\"-1\"", "\"4\""), testEvent, testFunction)); | ||
| assertEquals("subList() start index should be between 0 and list length (inclusive)", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithOutOfBoundArgumentsCase2() { | ||
| subListExpressionFunction = createObjectUnderTest(); | ||
| List<Integer> testList = List.of(1, 2, 3, 4, 5, 6); | ||
| testEvent = createTestEvent(Map.of("list", testList)); | ||
| Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", "\"10\"", "\"4\""), testEvent, testFunction)); | ||
| assertEquals("subList() start index should be between 0 and list length (inclusive)", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithOutOfBoundArgumentsCase3() { | ||
| subListExpressionFunction = createObjectUnderTest(); | ||
| List<Integer> testList = List.of(1, 2, 3, 4, 5, 6); | ||
| testEvent = createTestEvent(Map.of("list", testList)); | ||
| Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", "\"4\"", "\"2\""), testEvent, testFunction)); | ||
| assertEquals("subList() start index should be less than or equal to end index", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithOutOfBoundArgumentsCase4() { | ||
| subListExpressionFunction = createObjectUnderTest(); | ||
| List<Integer> testList = List.of(1, 2, 3, 4, 5, 6); | ||
| testEvent = createTestEvent(Map.of("list", testList)); | ||
| Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", "\"1\"", "\"10\""), testEvent, testFunction)); | ||
| assertEquals("subList() end index should be between 0 and list length or -1 for list length (exclusive)", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithOutOfBoundArgumentsCase5() { | ||
| subListExpressionFunction = createObjectUnderTest(); | ||
| List<Integer> testList = List.of(1, 2, 3, 4, 5, 6); | ||
| testEvent = createTestEvent(Map.of("list", testList)); | ||
| Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", "\"1\"", "\"-2\""), testEvent, testFunction)); | ||
| assertEquals("subList() end index should be between 0 and list length or -1 for list length (exclusive)", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithInvalidArguments() { | ||
| subListExpressionFunction = createObjectUnderTest(); | ||
| List<Integer> testList = List.of(1, 2, 3, 4, 5, 6); | ||
| testEvent = createTestEvent(Map.of("list", testList)); | ||
| Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", "\"5\"", "\"2\""), testEvent, testFunction)); | ||
| assertEquals("subList() start index should be less than or equal to end index", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithInvalidArgumentsCase2() { | ||
| subListExpressionFunction = createObjectUnderTest(); | ||
| List<Integer> testList = List.of(1, 2, 3, 4, 5, 6); | ||
| testEvent = createTestEvent(Map.of("list", testList)); | ||
| Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", "\"five\"", "\"two\""), testEvent, testFunction)); | ||
| assertEquals("subList() takes 2nd and 3rd arguments as integers", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithInvalidArgumentsCase3() { | ||
| subListExpressionFunction = createObjectUnderTest(); | ||
| List<Integer> testList = List.of(1, 2, 3, 4, 5, 6); | ||
| testEvent = createTestEvent(Map.of("list", testList)); | ||
| Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", "\"0\""), testEvent, testFunction)); | ||
| assertEquals("subList() takes 3 arguments", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithInvalidArgumentsCase4() { | ||
| subListExpressionFunction = createObjectUnderTest(); | ||
| List<Integer> testList = List.of(1, 2, 3, 4, 5, 6); | ||
| testEvent = createTestEvent(Map.of("list", testList)); | ||
| Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of(1, "\"0\"", "\"2\""), testEvent, testFunction)); | ||
| assertEquals("subList() takes 1st argument as string type", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithInvalidArgumentsCase5() { | ||
| subListExpressionFunction = createObjectUnderTest(); | ||
| testEvent = createTestEvent(Map.of("list", "testList")); | ||
| Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", "\"0\"", "\"2\""), testEvent, testFunction)); | ||
| assertEquals("/list is not of list type", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithUnknownKeyArgument() { | ||
| subListExpressionFunction = createObjectUnderTest(); | ||
| List<Integer> testList = List.of(1, 2, 3, 4, 5, 6); | ||
| testEvent = createTestEvent(Map.of("list", testList)); | ||
| assertThat(subListExpressionFunction.evaluate(List.of("/unknownList", 1, 2), testEvent, testFunction), equalTo(null)); | ||
| } | ||
| } |
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.
I think this should be
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.
We are using 0 based index and startIndex is inclusive so startIndex equals to sourceList.size() causes index out of bounds for subList function, that's why I used >=
For endIndex I used > since it's exclusive