From e38e84eabbd0c2d592604d5cc8148ced689e1e56 Mon Sep 17 00:00:00 2001 From: David Venable Date: Thu, 2 May 2024 15:15:27 -0500 Subject: [PATCH 1/2] Adds an enum to represent core data types in Data Prepper. This is initially created to help connect the convert_entry_type processor with upcoming work for evaluating type information as part of #4478. Signed-off-by: David Venable --- .../dataprepper/model/event/Type.java | 78 +++++++++++++++++++ .../dataprepper/model/event/TypeTest.java | 20 +++++ .../processor/dissect/DissectProcessor.java | 2 +- .../ConvertEntryTypeProcessor.java | 2 +- .../processor/mutateevent/TargetType.java | 29 ++++--- .../processor/mutateevent/TargetTypeTest.java | 46 +++++++++++ 6 files changed, 163 insertions(+), 14 deletions(-) create mode 100644 data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/Type.java create mode 100644 data-prepper-api/src/test/java/org/opensearch/dataprepper/model/event/TypeTest.java create mode 100644 data-prepper-plugins/mutate-event-processors/src/test/java/org/opensearch/dataprepper/plugins/processor/mutateevent/TargetTypeTest.java diff --git a/data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/Type.java b/data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/Type.java new file mode 100644 index 0000000000..c1ddfd348c --- /dev/null +++ b/data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/Type.java @@ -0,0 +1,78 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.model.event; + +import com.fasterxml.jackson.annotation.JsonCreator; + +import java.util.Arrays; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * Represents data types that are core to Data Prepper. + * + * @since 2.8 + */ +public enum Type { + /** + * Type of string. Compatible with {@link java.lang.String}. + * + * @since 2.8 + */ + STRING("string"), + /** + * Type of boolean. Compatible with the Java boolean primitive data type. + * + * @since 2.8 + */ + BOOLEAN("boolean"), + /** + * Type of integer. A 32-bit signed integer. Compatible with the Java int primitive data type. + * + * @since 2.8 + */ + INTEGER("integer"), + /** + * Type of long. A 64-bit signed integer. Compatible with the Java long primitive data type. + * + * @since 2.8 + */ + LONG("long"), + /** + * Type of double. A 64-bit IEEE 754 floating point number. Compatible with the Java double primitive data type. + * + * @since 2.8 + */ + DOUBLE("double"); + + private static final Map TYPES_MAP = Arrays.stream(Type.values()) + .collect(Collectors.toMap( + value -> value.typeName, + value -> value + )); + + private final String typeName; + + Type(final String typeName) { + this.typeName = typeName; + } + + /** + * Gets the name of the type. This is the name that users of + * Data Prepper use or see. + * + * @return The name of the type. + * @since 2.8 + */ + public String getTypeName() { + return typeName; + } + + @JsonCreator + static Type fromTypeName(final String option) { + return TYPES_MAP.get(option); + } +} diff --git a/data-prepper-api/src/test/java/org/opensearch/dataprepper/model/event/TypeTest.java b/data-prepper-api/src/test/java/org/opensearch/dataprepper/model/event/TypeTest.java new file mode 100644 index 0000000000..db65b3a703 --- /dev/null +++ b/data-prepper-api/src/test/java/org/opensearch/dataprepper/model/event/TypeTest.java @@ -0,0 +1,20 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.model.event; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +class TypeTest { + @ParameterizedTest + @EnumSource(Type.class) + void fromTypeName_returns_expected_value(final Type type) { + assertThat(Type.fromTypeName(type.getTypeName()), equalTo(type)); + } +} \ No newline at end of file diff --git a/data-prepper-plugins/dissect-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/dissect/DissectProcessor.java b/data-prepper-plugins/dissect-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/dissect/DissectProcessor.java index 0977d998a3..5e5fc296bc 100644 --- a/data-prepper-plugins/dissect-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/dissect/DissectProcessor.java +++ b/data-prepper-plugins/dissect-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/dissect/DissectProcessor.java @@ -91,7 +91,7 @@ private Object convertTargetType(String fieldKey, String fieldValue){ } try{ if(targetTypeMap.containsKey(fieldKey)){ - TypeConverter converter = targetTypeMap.get(fieldKey).getTargetConverter(); + final TypeConverter converter = targetTypeMap.get(fieldKey).getTargetConverter(); return converter.convert(fieldValue); } else { return fieldValue; diff --git a/data-prepper-plugins/mutate-event-processors/src/main/java/org/opensearch/dataprepper/plugins/processor/mutateevent/ConvertEntryTypeProcessor.java b/data-prepper-plugins/mutate-event-processors/src/main/java/org/opensearch/dataprepper/plugins/processor/mutateevent/ConvertEntryTypeProcessor.java index 95cbd9f714..13c28d3345 100644 --- a/data-prepper-plugins/mutate-event-processors/src/main/java/org/opensearch/dataprepper/plugins/processor/mutateevent/ConvertEntryTypeProcessor.java +++ b/data-prepper-plugins/mutate-event-processors/src/main/java/org/opensearch/dataprepper/plugins/processor/mutateevent/ConvertEntryTypeProcessor.java @@ -28,7 +28,7 @@ public class ConvertEntryTypeProcessor extends AbstractProcessor, Record> { private static final Logger LOG = LoggerFactory.getLogger(ConvertEntryTypeProcessor.class); private final List convertEntryKeys; - private final TypeConverter converter; + private final TypeConverter converter; private final String convertWhen; private final List nullValues; private final String type; diff --git a/data-prepper-plugins/mutate-event-processors/src/main/java/org/opensearch/dataprepper/plugins/processor/mutateevent/TargetType.java b/data-prepper-plugins/mutate-event-processors/src/main/java/org/opensearch/dataprepper/plugins/processor/mutateevent/TargetType.java index d7aacc5a21..75723c5256 100644 --- a/data-prepper-plugins/mutate-event-processors/src/main/java/org/opensearch/dataprepper/plugins/processor/mutateevent/TargetType.java +++ b/data-prepper-plugins/mutate-event-processors/src/main/java/org/opensearch/dataprepper/plugins/processor/mutateevent/TargetType.java @@ -6,6 +6,7 @@ package org.opensearch.dataprepper.plugins.processor.mutateevent; import com.fasterxml.jackson.annotation.JsonCreator; +import org.opensearch.dataprepper.model.event.Type; import org.opensearch.dataprepper.typeconverter.TypeConverter; import org.opensearch.dataprepper.typeconverter.IntegerConverter; import org.opensearch.dataprepper.typeconverter.StringConverter; @@ -18,30 +19,34 @@ import java.util.stream.Collectors; public enum TargetType { - INTEGER("integer", new IntegerConverter()), - STRING("string", new StringConverter()), - DOUBLE("double", new DoubleConverter()), - BOOLEAN("boolean", new BooleanConverter()), - LONG("long", new LongConverter()); + INTEGER(Type.INTEGER, new IntegerConverter()), + STRING(Type.STRING, new StringConverter()), + DOUBLE(Type.DOUBLE, new DoubleConverter()), + BOOLEAN(Type.BOOLEAN, new BooleanConverter()), + LONG(Type.LONG, new LongConverter()); private static final Map OPTIONS_MAP = Arrays.stream(TargetType.values()) .collect(Collectors.toMap( - value -> value.option, + value -> value.type.getTypeName(), value -> value )); - private final String option; - private final TypeConverter targetConverter; + private final Type type; + private final TypeConverter targetConverter; - TargetType(final String option, final TypeConverter target) { - this.option = option; + TargetType(final Type type, final TypeConverter target) { + this.type = type; this.targetConverter = target; } - public TypeConverter getTargetConverter() { + public TypeConverter getTargetConverter() { return targetConverter; } - + + Type getDataType() { + return type; + } + @JsonCreator static TargetType fromOptionValue(final String option) { return OPTIONS_MAP.get(option.toLowerCase()); diff --git a/data-prepper-plugins/mutate-event-processors/src/test/java/org/opensearch/dataprepper/plugins/processor/mutateevent/TargetTypeTest.java b/data-prepper-plugins/mutate-event-processors/src/test/java/org/opensearch/dataprepper/plugins/processor/mutateevent/TargetTypeTest.java new file mode 100644 index 0000000000..91e596ae8e --- /dev/null +++ b/data-prepper-plugins/mutate-event-processors/src/test/java/org/opensearch/dataprepper/plugins/processor/mutateevent/TargetTypeTest.java @@ -0,0 +1,46 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.plugins.processor.mutateevent; + +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; +import org.junit.jupiter.params.provider.EnumSource; +import org.opensearch.dataprepper.model.event.Type; + +import java.util.stream.Stream; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.params.provider.Arguments.arguments; + +class TargetTypeTest { + @ParameterizedTest + @EnumSource(TargetType.class) + void fromTypeName_returns_expected_value(final TargetType targetType) { + assertThat(TargetType.fromOptionValue(targetType.getDataType().getTypeName()), equalTo(targetType)); + } + @ParameterizedTest + @ArgumentsSource(DataTypeToTargetTypeArgumentsProvider.class) + void fromTypeName_returns_expected_value_based_on_DataType(final String typeName, final TargetType targetType) { + assertThat(TargetType.fromOptionValue(typeName), equalTo(targetType)); + } + + static class DataTypeToTargetTypeArgumentsProvider implements ArgumentsProvider { + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of( + arguments(Type.STRING.getTypeName(), TargetType.STRING), + arguments(Type.BOOLEAN.getTypeName(), TargetType.BOOLEAN), + arguments(Type.INTEGER.getTypeName(), TargetType.INTEGER), + arguments(Type.LONG.getTypeName(), TargetType.LONG), + arguments(Type.DOUBLE.getTypeName(), TargetType.DOUBLE) + ); + } + } +} \ No newline at end of file From ecd9d4a4eb3413025ffbe2f636bb9602e095d068 Mon Sep 17 00:00:00 2001 From: David Venable Date: Fri, 3 May 2024 11:16:38 -0500 Subject: [PATCH 2/2] Rename Type to DataType. Signed-off-by: David Venable --- .../model/event/{Type.java => DataType.java} | 8 +++---- .../{TypeTest.java => DataTypeTest.java} | 8 +++---- .../processor/mutateevent/TargetType.java | 24 +++++++++---------- .../processor/mutateevent/TargetTypeTest.java | 12 +++++----- 4 files changed, 26 insertions(+), 26 deletions(-) rename data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/{Type.java => DataType.java} (89%) rename data-prepper-api/src/test/java/org/opensearch/dataprepper/model/event/{TypeTest.java => DataTypeTest.java} (63%) diff --git a/data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/Type.java b/data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/DataType.java similarity index 89% rename from data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/Type.java rename to data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/DataType.java index c1ddfd348c..76b7a93293 100644 --- a/data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/Type.java +++ b/data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/DataType.java @@ -16,7 +16,7 @@ * * @since 2.8 */ -public enum Type { +public enum DataType { /** * Type of string. Compatible with {@link java.lang.String}. * @@ -48,7 +48,7 @@ public enum Type { */ DOUBLE("double"); - private static final Map TYPES_MAP = Arrays.stream(Type.values()) + private static final Map TYPES_MAP = Arrays.stream(DataType.values()) .collect(Collectors.toMap( value -> value.typeName, value -> value @@ -56,7 +56,7 @@ public enum Type { private final String typeName; - Type(final String typeName) { + DataType(final String typeName) { this.typeName = typeName; } @@ -72,7 +72,7 @@ public String getTypeName() { } @JsonCreator - static Type fromTypeName(final String option) { + static DataType fromTypeName(final String option) { return TYPES_MAP.get(option); } } diff --git a/data-prepper-api/src/test/java/org/opensearch/dataprepper/model/event/TypeTest.java b/data-prepper-api/src/test/java/org/opensearch/dataprepper/model/event/DataTypeTest.java similarity index 63% rename from data-prepper-api/src/test/java/org/opensearch/dataprepper/model/event/TypeTest.java rename to data-prepper-api/src/test/java/org/opensearch/dataprepper/model/event/DataTypeTest.java index db65b3a703..cb1f36b636 100644 --- a/data-prepper-api/src/test/java/org/opensearch/dataprepper/model/event/TypeTest.java +++ b/data-prepper-api/src/test/java/org/opensearch/dataprepper/model/event/DataTypeTest.java @@ -11,10 +11,10 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; -class TypeTest { +class DataTypeTest { @ParameterizedTest - @EnumSource(Type.class) - void fromTypeName_returns_expected_value(final Type type) { - assertThat(Type.fromTypeName(type.getTypeName()), equalTo(type)); + @EnumSource(DataType.class) + void fromTypeName_returns_expected_value(final DataType dataType) { + assertThat(DataType.fromTypeName(dataType.getTypeName()), equalTo(dataType)); } } \ No newline at end of file diff --git a/data-prepper-plugins/mutate-event-processors/src/main/java/org/opensearch/dataprepper/plugins/processor/mutateevent/TargetType.java b/data-prepper-plugins/mutate-event-processors/src/main/java/org/opensearch/dataprepper/plugins/processor/mutateevent/TargetType.java index 75723c5256..74e516f27c 100644 --- a/data-prepper-plugins/mutate-event-processors/src/main/java/org/opensearch/dataprepper/plugins/processor/mutateevent/TargetType.java +++ b/data-prepper-plugins/mutate-event-processors/src/main/java/org/opensearch/dataprepper/plugins/processor/mutateevent/TargetType.java @@ -6,7 +6,7 @@ package org.opensearch.dataprepper.plugins.processor.mutateevent; import com.fasterxml.jackson.annotation.JsonCreator; -import org.opensearch.dataprepper.model.event.Type; +import org.opensearch.dataprepper.model.event.DataType; import org.opensearch.dataprepper.typeconverter.TypeConverter; import org.opensearch.dataprepper.typeconverter.IntegerConverter; import org.opensearch.dataprepper.typeconverter.StringConverter; @@ -19,23 +19,23 @@ import java.util.stream.Collectors; public enum TargetType { - INTEGER(Type.INTEGER, new IntegerConverter()), - STRING(Type.STRING, new StringConverter()), - DOUBLE(Type.DOUBLE, new DoubleConverter()), - BOOLEAN(Type.BOOLEAN, new BooleanConverter()), - LONG(Type.LONG, new LongConverter()); + INTEGER(DataType.INTEGER, new IntegerConverter()), + STRING(DataType.STRING, new StringConverter()), + DOUBLE(DataType.DOUBLE, new DoubleConverter()), + BOOLEAN(DataType.BOOLEAN, new BooleanConverter()), + LONG(DataType.LONG, new LongConverter()); private static final Map OPTIONS_MAP = Arrays.stream(TargetType.values()) .collect(Collectors.toMap( - value -> value.type.getTypeName(), + value -> value.dataType.getTypeName(), value -> value )); - private final Type type; + private final DataType dataType; private final TypeConverter targetConverter; - TargetType(final Type type, final TypeConverter target) { - this.type = type; + TargetType(final DataType dataType, final TypeConverter target) { + this.dataType = dataType; this.targetConverter = target; } @@ -43,8 +43,8 @@ public TypeConverter getTargetConverter() { return targetConverter; } - Type getDataType() { - return type; + DataType getDataType() { + return dataType; } @JsonCreator diff --git a/data-prepper-plugins/mutate-event-processors/src/test/java/org/opensearch/dataprepper/plugins/processor/mutateevent/TargetTypeTest.java b/data-prepper-plugins/mutate-event-processors/src/test/java/org/opensearch/dataprepper/plugins/processor/mutateevent/TargetTypeTest.java index 91e596ae8e..e2565f7e04 100644 --- a/data-prepper-plugins/mutate-event-processors/src/test/java/org/opensearch/dataprepper/plugins/processor/mutateevent/TargetTypeTest.java +++ b/data-prepper-plugins/mutate-event-processors/src/test/java/org/opensearch/dataprepper/plugins/processor/mutateevent/TargetTypeTest.java @@ -11,7 +11,7 @@ import org.junit.jupiter.params.provider.ArgumentsProvider; import org.junit.jupiter.params.provider.ArgumentsSource; import org.junit.jupiter.params.provider.EnumSource; -import org.opensearch.dataprepper.model.event.Type; +import org.opensearch.dataprepper.model.event.DataType; import java.util.stream.Stream; @@ -35,11 +35,11 @@ static class DataTypeToTargetTypeArgumentsProvider implements ArgumentsProvider @Override public Stream provideArguments(final ExtensionContext extensionContext) { return Stream.of( - arguments(Type.STRING.getTypeName(), TargetType.STRING), - arguments(Type.BOOLEAN.getTypeName(), TargetType.BOOLEAN), - arguments(Type.INTEGER.getTypeName(), TargetType.INTEGER), - arguments(Type.LONG.getTypeName(), TargetType.LONG), - arguments(Type.DOUBLE.getTypeName(), TargetType.DOUBLE) + arguments(DataType.STRING.getTypeName(), TargetType.STRING), + arguments(DataType.BOOLEAN.getTypeName(), TargetType.BOOLEAN), + arguments(DataType.INTEGER.getTypeName(), TargetType.INTEGER), + arguments(DataType.LONG.getTypeName(), TargetType.LONG), + arguments(DataType.DOUBLE.getTypeName(), TargetType.DOUBLE) ); } }