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

Support conversion of String type to Utf8 in AvroWrapper #61

Merged
merged 3 commits into from
Nov 10, 2020
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 @@ -49,8 +49,13 @@ public static StdData createStdData(Object avroData, Schema avroSchema) {
return new AvroLong((Long) avroData);
case BOOLEAN:
return new AvroBoolean((Boolean) avroData);
case STRING:
return new AvroString((Utf8) avroData);
case STRING: {
if (avroData instanceof Utf8) {
return new AvroString((Utf8) avroData);
} else if (avroData instanceof String) {
return new AvroString(new Utf8((String) avroData));
curtiscwang marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +53 to +56
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that this PR handles this case better. I will make some time soon to merge it :)

}
}
case FLOAT:
return new AvroFloat((Float) avroData);
case DOUBLE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ private void testSimpleType(String typeName, Class<? extends StdType> expectedAv
StdData stdData = AvroWrapper.createStdData(testData, avroSchema);
assertNotNull(stdData);
assertTrue(expectedDataClass.isAssignableFrom(stdData.getClass()));
assertEquals(testData, ((PlatformData) stdData).getUnderlyingData());
if ("string".equals(typeName)) {
// Use String values for equality assertion as we support both Utf8 and String input types
assertEquals(testData.toString(), ((PlatformData) stdData).getUnderlyingData().toString());
} else {
assertEquals(testData, ((PlatformData) stdData).getUnderlyingData());
}
}

@Test
Expand Down Expand Up @@ -94,10 +99,15 @@ public void testDoubleType() {
}

@Test
public void testStringType() {
public void testStringTypeUtf8() {
curtiscwang marked this conversation as resolved.
Show resolved Hide resolved
testSimpleType("string", AvroStringType.class, new Utf8("foo"), AvroString.class);
}

@Test
public void testStringType() {
testSimpleType("string", AvroStringType.class, "foo", AvroString.class);
}

@Test
public void testBinaryType() {
testSimpleType("bytes", AvroBinaryType.class, ByteBuffer.wrap("bar".getBytes()), AvroBinary.class);
Expand Down