From 54c30ca9f25d2ef725ce66842fb07e331c1a522c Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Tue, 24 Jul 2018 08:53:26 -0700 Subject: [PATCH] fix negatives and add octal test --- .../ingest/common/ConvertProcessor.java | 4 ++-- .../ingest/common/ConvertProcessorTests.java | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ConvertProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ConvertProcessor.java index 7fe852cf698de..2e881b82b59de 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ConvertProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ConvertProcessor.java @@ -43,7 +43,7 @@ enum Type { public Object convert(Object value) { try { String strValue = value.toString(); - if (strValue.startsWith("0x")) { + if (strValue.startsWith("0x") || strValue.startsWith("-0x")) { return Integer.decode(strValue); } return Integer.parseInt(strValue); @@ -57,7 +57,7 @@ public Object convert(Object value) { public Object convert(Object value) { try { String strValue = value.toString(); - if (strValue.startsWith("0x")) { + if (strValue.startsWith("0x") || strValue.startsWith("-0x")) { return Long.decode(strValue); } return Long.parseLong(strValue); diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ConvertProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ConvertProcessorTests.java index c8bf2c519ed41..8d6f62cb781c3 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ConvertProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ConvertProcessorTests.java @@ -59,6 +59,14 @@ public void testConvertIntHex() throws Exception { 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); @@ -121,6 +129,14 @@ public void testConvertLongHex() throws Exception { 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(10)); + } + public void testConvertLongHexError() { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); String value = "0x" + randomAlphaOfLengthBetween(1, 10);