Ingest: Support integer and long hex values in convert (#32213)
This commit adds checks for hex formatted strings in the convert processor, allowing strings like `0x1` to be parsed as integer `1`. closes #32182
This commit is contained in:
parent
be40a6982a
commit
49d4b26f16
|
@ -42,7 +42,11 @@ public final class ConvertProcessor extends AbstractProcessor {
|
||||||
@Override
|
@Override
|
||||||
public Object convert(Object value) {
|
public Object convert(Object value) {
|
||||||
try {
|
try {
|
||||||
return Integer.parseInt(value.toString());
|
String strValue = value.toString();
|
||||||
|
if (strValue.startsWith("0x") || strValue.startsWith("-0x")) {
|
||||||
|
return Integer.decode(strValue);
|
||||||
|
}
|
||||||
|
return Integer.parseInt(strValue);
|
||||||
} catch(NumberFormatException e) {
|
} catch(NumberFormatException e) {
|
||||||
throw new IllegalArgumentException("unable to convert [" + value + "] to integer", e);
|
throw new IllegalArgumentException("unable to convert [" + value + "] to integer", e);
|
||||||
}
|
}
|
||||||
|
@ -52,7 +56,11 @@ public final class ConvertProcessor extends AbstractProcessor {
|
||||||
@Override
|
@Override
|
||||||
public Object convert(Object value) {
|
public Object convert(Object value) {
|
||||||
try {
|
try {
|
||||||
return Long.parseLong(value.toString());
|
String strValue = value.toString();
|
||||||
|
if (strValue.startsWith("0x") || strValue.startsWith("-0x")) {
|
||||||
|
return Long.decode(strValue);
|
||||||
|
}
|
||||||
|
return Long.parseLong(strValue);
|
||||||
} catch(NumberFormatException e) {
|
} catch(NumberFormatException e) {
|
||||||
throw new IllegalArgumentException("unable to convert [" + value + "] to long", e);
|
throw new IllegalArgumentException("unable to convert [" + value + "] to long", e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,6 +49,33 @@ public class ConvertProcessorTests extends ESTestCase {
|
||||||
assertThat(ingestDocument.getFieldValue(fieldName, Integer.class), equalTo(randomInt));
|
assertThat(ingestDocument.getFieldValue(fieldName, Integer.class), equalTo(randomInt));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testConvertIntHex() throws Exception {
|
||||||
|
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||||
|
int randomInt = randomInt();
|
||||||
|
String intString = randomInt < 0 ? "-0x" + Integer.toHexString(-randomInt) : "0x" + Integer.toHexString(randomInt);
|
||||||
|
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, intString);
|
||||||
|
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.INTEGER, false);
|
||||||
|
processor.execute(ingestDocument);
|
||||||
|
assertThat(ingestDocument.getFieldValue(fieldName, Integer.class), equalTo(randomInt));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testConvertIntLeadingZero() throws Exception {
|
||||||
|
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||||
|
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "010");
|
||||||
|
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.INTEGER, false);
|
||||||
|
processor.execute(ingestDocument);
|
||||||
|
assertThat(ingestDocument.getFieldValue(fieldName, Integer.class), equalTo(10));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testConvertIntHexError() {
|
||||||
|
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||||
|
String value = "0x" + randomAlphaOfLengthBetween(1, 10);
|
||||||
|
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, value);
|
||||||
|
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.INTEGER, false);
|
||||||
|
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> processor.execute(ingestDocument));
|
||||||
|
assertThat(e.getMessage(), equalTo("unable to convert [" + value + "] to integer"));
|
||||||
|
}
|
||||||
|
|
||||||
public void testConvertIntList() throws Exception {
|
public void testConvertIntList() throws Exception {
|
||||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||||
int numItems = randomIntBetween(1, 10);
|
int numItems = randomIntBetween(1, 10);
|
||||||
|
@ -92,6 +119,33 @@ public class ConvertProcessorTests extends ESTestCase {
|
||||||
assertThat(ingestDocument.getFieldValue(fieldName, Long.class), equalTo(randomLong));
|
assertThat(ingestDocument.getFieldValue(fieldName, Long.class), equalTo(randomLong));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testConvertLongHex() throws Exception {
|
||||||
|
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||||
|
long randomLong = randomLong();
|
||||||
|
String longString = randomLong < 0 ? "-0x" + Long.toHexString(-randomLong) : "0x" + Long.toHexString(randomLong);
|
||||||
|
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, longString);
|
||||||
|
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.LONG, false);
|
||||||
|
processor.execute(ingestDocument);
|
||||||
|
assertThat(ingestDocument.getFieldValue(fieldName, Long.class), equalTo(randomLong));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testConvertLongLeadingZero() throws Exception {
|
||||||
|
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||||
|
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "010");
|
||||||
|
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.LONG, false);
|
||||||
|
processor.execute(ingestDocument);
|
||||||
|
assertThat(ingestDocument.getFieldValue(fieldName, Long.class), equalTo(10L));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testConvertLongHexError() {
|
||||||
|
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||||
|
String value = "0x" + randomAlphaOfLengthBetween(1, 10);
|
||||||
|
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, value);
|
||||||
|
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.LONG, false);
|
||||||
|
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> processor.execute(ingestDocument));
|
||||||
|
assertThat(e.getMessage(), equalTo("unable to convert [" + value + "] to long"));
|
||||||
|
}
|
||||||
|
|
||||||
public void testConvertLongList() throws Exception {
|
public void testConvertLongList() throws Exception {
|
||||||
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
||||||
int numItems = randomIntBetween(1, 10);
|
int numItems = randomIntBetween(1, 10);
|
||||||
|
|
Loading…
Reference in New Issue