Skip to content
This repository has been archived by the owner on Jun 30, 2023. It is now read-only.

Commit

Permalink
Propagate null timestamps and dates [updated with CI fix] (#34)
Browse files Browse the repository at this point in the history
* Propagate null timestamps and dates

* Distinguish between default and null messages

* Non-static import for MESSAGE

Co-authored-by: Matthew Jones <[email protected]>
  • Loading branch information
2 people authored and mike9005 committed Jan 6, 2020
1 parent 2fba9ee commit 4184dae
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/main/java/com/blueapron/connect/protobuf/ProtobufData.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import com.google.protobuf.ByteString;
import com.google.protobuf.Descriptors;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Descriptors.OneofDescriptor;
import com.google.protobuf.GeneratedMessageV3;
import com.google.protobuf.InvalidProtocolBufferException;
Expand Down Expand Up @@ -229,8 +230,11 @@ private boolean isProtobufDate(Schema schema) {
private void setStructField(Schema schema, Message message, Struct result, Descriptors.FieldDescriptor fieldDescriptor) {
final String fieldName = getConnectFieldName(fieldDescriptor);
final Field field = schema.field(fieldName);
Object obj = message.getField(fieldDescriptor);
result.put(fieldName, toConnectData(field.schema(), obj));
Object obj = null;
if (fieldDescriptor.getType() != FieldDescriptor.Type.MESSAGE || fieldDescriptor.isRepeated() || fieldDescriptor.isMapField() || message.hasField(fieldDescriptor)) {
obj = toConnectData(field.schema(), message.getField(fieldDescriptor));
}
result.put(fieldName, obj);
}

Object toConnectData(Schema schema, Object value) {
Expand Down Expand Up @@ -338,9 +342,6 @@ Object toConnectData(Schema schema, Object value) {

case STRUCT: {
final Message message = (Message) value; // Validate type
if (message == message.getDefaultInstanceForType()) {
return null;
}

final Struct result = new Struct(schema.schema());
final Descriptors.Descriptor descriptor = message.getDescriptorForType();
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/com/blueapron/connect/protobuf/ProtobufDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.nio.ByteBuffer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -428,6 +429,35 @@ public void testToConnectTimestamp() throws ParseException {
assertEquals(getExpectedSchemaAndValue(timestampSchema, expectedValue, expectedName), result);
}

@Test
public void testToConnectNullTimestamp() {
String expectedName = "TimestampValue";
TimestampValueOuterClass.TimestampValue message = TimestampValueOuterClass.TimestampValue.getDefaultInstance();

ProtobufData protobufData = new ProtobufData(TimestampValueOuterClass.TimestampValue.class, LEGACY_NAME);
SchemaAndValue result = protobufData.toConnectData(message.toByteArray());

Schema timestampSchema = org.apache.kafka.connect.data.Timestamp.builder().optional().build();
assertEquals(getExpectedSchemaAndValue(timestampSchema, null, expectedName), result);
}

@Test
public void testToConnectEpochTimestamp() {
java.util.Date expectedValue = java.util.Date.from(Instant.EPOCH);
String expectedName = "TimestampValue";

Timestamp timestamp = Timestamp.getDefaultInstance();
TimestampValueOuterClass.TimestampValue.Builder builder = TimestampValueOuterClass.TimestampValue.newBuilder();
builder.setValue(timestamp);
TimestampValueOuterClass.TimestampValue message = builder.build();

ProtobufData protobufData = new ProtobufData(TimestampValueOuterClass.TimestampValue.class, LEGACY_NAME);
SchemaAndValue result = protobufData.toConnectData(message.toByteArray());

Schema timestampSchema = org.apache.kafka.connect.data.Timestamp.builder().optional().build();
assertEquals(getExpectedSchemaAndValue(timestampSchema, expectedValue, expectedName), result);
}

@Test
public void testToConnectDate() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Expand Down

0 comments on commit 4184dae

Please sign in to comment.