Skip to content

Commit

Permalink
Modify typeof operator grammar and add support for ArrayList (#4523)
Browse files Browse the repository at this point in the history
* Modify typeof operator grammar and add support for ArrayList

Signed-off-by: Krishna Kondaka <[email protected]>

* Fixed checkstyle error

Signed-off-by: Krishna Kondaka <[email protected]>

* Added test case for more code coverage

Signed-off-by: Krishna Kondaka <[email protected]>

---------

Signed-off-by: Krishna Kondaka <[email protected]>
Co-authored-by: Krishna Kondaka <[email protected]>
  • Loading branch information
kkondaka and Krishna Kondaka authored May 9, 2024
1 parent cc37d00 commit fbe059f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -98,7 +99,7 @@ public static boolean isSameType(final Object object, final String option) {
case MAP:
return (object instanceof Map);
case ARRAY:
return (object.getClass().isArray());
return (object instanceof ArrayList || object.getClass().isArray());
case DOUBLE:
return (object instanceof Double);
case BOOLEAN:
Expand Down
20 changes: 19 additions & 1 deletion data-prepper-expression/src/main/antlr/DataPrepperExpression.g4
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ relationalOperator
;

typeOfOperatorExpression
: JsonPointer TYPEOF String
: JsonPointer TYPEOF DataTypes
;

setOperatorExpression
Expand Down Expand Up @@ -285,6 +285,16 @@ EscapeSequence
: '\\' [btnfr"'\\$]
;

DataTypes
: INTEGER
| BOOLEAN
| LONG
| MAP
| ARRAY
| DOUBLE
| STRING
;

SET_DELIMITER
: COMMA
;
Expand Down Expand Up @@ -324,6 +334,14 @@ EXPONENTLETTER
| 'e'
;

INTEGER: 'integer';
BOOLEAN: 'boolean';
LONG : 'long';
DOUBLE : 'double';
STRING : 'string';
MAP : 'map';
ARRAY : 'array';

fragment
SPACE : ' ';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public Object coercePrimaryTerminalNode(final TerminalNode node, final Event eve
return Boolean.valueOf(nodeStringValue);
case DataPrepperExpressionParser.Null:
return null;
case DataPrepperExpressionParser.DataTypes:
return nodeStringValue;

default:
throw new ExpressionCoercionException("Unsupported terminal node type symbol string: " +
DataPrepperExpressionParser.VOCABULARY.getDisplayName(nodeType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser;
Expand Down Expand Up @@ -112,6 +113,18 @@ void testCoerceTerminalNodeFloatType() {
assertThat(result, equalTo(testFloat));
}

@ParameterizedTest
@ValueSource(strings={"integer", "boolean", "long", "string", "double", "map", "array"})
void testCoerceTerminalNodeDataTypesType(String testString) {
when(token.getType()).thenReturn(DataPrepperExpressionParser.DataTypes);
when(terminalNode.getSymbol()).thenReturn(token);
when(terminalNode.getText()).thenReturn(testString);
final Event testEvent = createTestEvent(new HashMap<>());
final Object result = objectUnderTest.coercePrimaryTerminalNode(terminalNode, testEvent);
assertThat(result, instanceOf(String.class));
assertThat(result, equalTo(testString));
}

@Test
void testCoerceTerminalNodeBooleanType() {
when(token.getType()).thenReturn(DataPrepperExpressionParser.Boolean);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

Expand Down Expand Up @@ -66,6 +68,9 @@ void testEvalInValidArgType() {

private static Stream<Arguments> getTypeOfTestData() {
int testArray[] = {1,2};
List<Integer> testArrayList = new ArrayList<Integer>();
testArrayList.add(1);
testArrayList.add(2);
return Stream.of(
Arguments.of(2, "integer", true),
Arguments.of("testString", "string", true),
Expand All @@ -74,6 +79,7 @@ private static Stream<Arguments> getTypeOfTestData() {
Arguments.of(true, "boolean", true),
Arguments.of(Map.of("k","v"), "map", true),
Arguments.of(testArray, "array", true),
Arguments.of(testArrayList, "array", true),
Arguments.of(2.0, "integer", false),
Arguments.of(2, "string", false),
Arguments.of("testString", "long", false),
Expand Down

0 comments on commit fbe059f

Please sign in to comment.