Skip to content

Commit 54c30ca

Browse files
committed
fix negatives and add octal test
1 parent c406980 commit 54c30ca

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ConvertProcessor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ enum Type {
4343
public Object convert(Object value) {
4444
try {
4545
String strValue = value.toString();
46-
if (strValue.startsWith("0x")) {
46+
if (strValue.startsWith("0x") || strValue.startsWith("-0x")) {
4747
return Integer.decode(strValue);
4848
}
4949
return Integer.parseInt(strValue);
@@ -57,7 +57,7 @@ public Object convert(Object value) {
5757
public Object convert(Object value) {
5858
try {
5959
String strValue = value.toString();
60-
if (strValue.startsWith("0x")) {
60+
if (strValue.startsWith("0x") || strValue.startsWith("-0x")) {
6161
return Long.decode(strValue);
6262
}
6363
return Long.parseLong(strValue);

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ConvertProcessorTests.java

+16
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ public void testConvertIntHex() throws Exception {
5959
assertThat(ingestDocument.getFieldValue(fieldName, Integer.class), equalTo(randomInt));
6060
}
6161

62+
public void testConvertIntLeadingZero() throws Exception {
63+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
64+
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "010");
65+
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.INTEGER, false);
66+
processor.execute(ingestDocument);
67+
assertThat(ingestDocument.getFieldValue(fieldName, Integer.class), equalTo(10));
68+
}
69+
6270
public void testConvertIntHexError() {
6371
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
6472
String value = "0x" + randomAlphaOfLengthBetween(1, 10);
@@ -121,6 +129,14 @@ public void testConvertLongHex() throws Exception {
121129
assertThat(ingestDocument.getFieldValue(fieldName, Long.class), equalTo(randomLong));
122130
}
123131

132+
public void testConvertLongLeadingZero() throws Exception {
133+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
134+
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "010");
135+
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.LONG, false);
136+
processor.execute(ingestDocument);
137+
assertThat(ingestDocument.getFieldValue(fieldName, Long.class), equalTo(10));
138+
}
139+
124140
public void testConvertLongHexError() {
125141
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
126142
String value = "0x" + randomAlphaOfLengthBetween(1, 10);

0 commit comments

Comments
 (0)