Skip to content
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

Added 'long' as a target type for 'convert_entry_type' processor #4359

Merged
merged 8 commits into from
Apr 4, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.dataprepper.typeconverter;

public class LongConverter implements TypeConverter<Long> {
public Long convert(Object source) throws IllegalArgumentException {
if (source instanceof String) {
return Long.parseLong((String)source);
}
if (source instanceof Float) {
return (long)(float)((Float)source);
}
if (source instanceof Double) {
return (long)(double)((Double)source);
}
if (source instanceof Boolean) {
return ((Boolean)source) ? 1L : 0L;
}
if (source instanceof Integer) {
return ((Integer)source).longValue();
}
if (source instanceof Long) {
return (Long)source;
}
throw new IllegalArgumentException("Unsupported type conversion. Source class: " + source.getClass());
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.dataprepper.typeconverter;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class LongConverterTests {
@ParameterizedTest
@ValueSource(strings = {"1234", "5678"})
void testStringToLongConversion(String stringValue) {
LongConverter converter = new LongConverter();
assertThat(converter.convert(stringValue), equalTo(Long.parseLong(stringValue)));
}
@ParameterizedTest
@ValueSource(floats = {(float)1234.56789, Float.MAX_VALUE, Float.MIN_VALUE})
void testfloatToLongConversion(float floatValue) {
LongConverter converter = new LongConverter();
assertThat(converter.convert(floatValue), equalTo((long)(float)floatValue));
}
@ParameterizedTest
@ValueSource(doubles = {12345678.12345678, 2.0 * Integer.MAX_VALUE, Double.MAX_VALUE, Double.MIN_VALUE})
void testDoubleToLongConversion(double doubleValue) {
LongConverter converter = new LongConverter();
assertThat(converter.convert(doubleValue), equalTo((long)(double)doubleValue));
}
@ParameterizedTest
@ValueSource(booleans = {false,true})
void testBooleanToLongConversion(boolean booleanValue) {
LongConverter converter = new LongConverter();
if (booleanValue)
assertThat(converter.convert(booleanValue), equalTo(1L));
else
assertThat(converter.convert(booleanValue), equalTo(0L));
}
@ParameterizedTest
@ValueSource(ints = {1234, Integer.MAX_VALUE, Integer.MIN_VALUE})
void testIntToLongConverstion(int intValue){
LongConverter converter = new LongConverter();
assertThat(converter.convert(intValue), equalTo((long)intValue));
}
@ParameterizedTest
@ValueSource(longs = {(long)1234, Long.MAX_VALUE, Long.MIN_VALUE})
void testLongToLongConversion(long longValue) {
LongConverter converter = new LongConverter();
assertThat(converter.convert(longValue), equalTo(longValue));
}
@Test
void testInvalidStringConversion() {
LongConverter converter = new LongConverter();
assertThrows(IllegalArgumentException.class, () -> converter.convert(new Object()));
}
}

2 changes: 1 addition & 1 deletion data-prepper-plugins/mutate-event-processors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ and the type conversion processor will change it to the following output, where
### Configuration
* `key` - keys whose value needs to be converted to a different type. Required if `keys` option is not defined.
* `keys` - list of keys whose value needs to be converted to a different type. Required if `key` option is not defined.
* `type` - target type for the value of the key. Possible values are `integer`, `double`, `string`, and `boolean`. Default is `integer`.
* `type` - target type for the value of the key. Possible values are `integer`, `double`, `long`, `string`, and `boolean`. Default is `integer`.
* `null_values` - treat any value in the null_values list as null.
* Example: `null_values` is `["-"]` and `key` is `key1`. `{"key1": "-", "key2": "value2"}` will parse into `{"key2": "value2"}`
* `tags_on_failure`(Optional)- A `List` of `String`s that specifies the tags to be set in the event the processor fails to convert `key` or `keys` to configured `type`. These tags may be used in conditional expressions in other parts of the configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.opensearch.dataprepper.typeconverter.StringConverter;
import org.opensearch.dataprepper.typeconverter.DoubleConverter;
import org.opensearch.dataprepper.typeconverter.BooleanConverter;
import org.opensearch.dataprepper.typeconverter.LongConverter;

import java.util.Map;
import java.util.Arrays;
Expand All @@ -20,7 +21,8 @@ public enum TargetType {
INTEGER("integer", new IntegerConverter()),
STRING("string", new StringConverter()),
DOUBLE("double", new DoubleConverter()),
BOOLEAN("boolean", new BooleanConverter());
BOOLEAN("boolean", new BooleanConverter()),
LONG("long", new LongConverter());

private static final Map<String, TargetType> OPTIONS_MAP = Arrays.stream(TargetType.values())
.collect(Collectors.toMap(
Expand Down
Loading