Skip to content

Ingest: Support integer and long hex values in convert #32213

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

Merged
merged 5 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ enum Type {
@Override
public Object convert(Object value) {
try {
return Integer.parseInt(value.toString());
String str = value.toString();
if (str.startsWith("0x")) {
return Integer.decode(str);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add a test for when it is Integer.decode that throws the NumberFormatException?

same goes for Long.decode

}
return Integer.parseInt(str);
} catch(NumberFormatException e) {
throw new IllegalArgumentException("unable to convert [" + value + "] to integer", e);
}
Expand All @@ -52,7 +56,11 @@ public Object convert(Object value) {
@Override
public Object convert(Object value) {
try {
return Long.parseLong(value.toString());
String str = value.toString();
if (str.startsWith("0x")) {
return Long.decode(str);
}
return Long.parseLong(str);
} catch(NumberFormatException e) {
throw new IllegalArgumentException("unable to convert [" + value + "] to long", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ public void testConvertInt() throws Exception {
assertThat(ingestDocument.getFieldValue(fieldName, Integer.class), equalTo(randomInt));
}

public void testConvertIntHex() throws Exception {
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
int randomInt = randomInt();
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "0x" + Integer.toHexString(randomInt));
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.INTEGER, false);
processor.execute(ingestDocument);
assertThat(ingestDocument.getFieldValue(fieldName, Integer.class), equalTo(randomInt));
}

public void testConvertIntList() throws Exception {
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
int numItems = randomIntBetween(1, 10);
Expand Down Expand Up @@ -92,6 +101,15 @@ public void testConvertLong() throws Exception {
assertThat(ingestDocument.getFieldValue(fieldName, Long.class), equalTo(randomLong));
}

public void testConvertLongHex() throws Exception {
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
long randomLong = randomLong();
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "0x" + Long.toHexString(randomLong));
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.LONG, false);
processor.execute(ingestDocument);
assertThat(ingestDocument.getFieldValue(fieldName, Long.class), equalTo(randomLong));
}

public void testConvertLongList() throws Exception {
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
int numItems = randomIntBetween(1, 10);
Expand Down