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

Commit 433be31

Browse files
authored
Merge pull request #5 from blueapron/default-values
Handle default values according to the proto3 standard
2 parents e6a7841 + 24ac9d0 commit 433be31

File tree

5 files changed

+1088
-259
lines changed

5 files changed

+1088
-259
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<groupId>com.blueapron</groupId>
88
<artifactId>kafka-connect-protobuf-converter</artifactId>
99
<packaging>jar</packaging>
10-
<version>1.2.0</version>
10+
<version>1.3.0</version>
1111

1212
<properties>
1313
<protobuf.version>3.4.0</protobuf.version>

src/main/java/com/blueapron/connect/protobuf/ProtobufData.java

+31-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.google.protobuf.Descriptors;
66
import com.google.protobuf.GeneratedMessageV3;
77
import com.google.protobuf.InvalidProtocolBufferException;
8-
import com.google.protobuf.MapEntry;
98
import com.google.protobuf.Message;
109
import org.apache.kafka.connect.data.Date;
1110
import org.apache.kafka.connect.data.Field;
@@ -26,11 +25,24 @@
2625
import java.util.Map;
2726
import com.google.protobuf.util.Timestamps;
2827

28+
import static com.google.protobuf.Descriptors.FieldDescriptor.Type.BOOL;
29+
import static com.google.protobuf.Descriptors.FieldDescriptor.Type.BYTES;
30+
import static com.google.protobuf.Descriptors.FieldDescriptor.Type.DOUBLE;
31+
import static com.google.protobuf.Descriptors.FieldDescriptor.Type.ENUM;
32+
import static com.google.protobuf.Descriptors.FieldDescriptor.Type.FLOAT;
33+
import static com.google.protobuf.Descriptors.FieldDescriptor.Type.INT32;
34+
import static com.google.protobuf.Descriptors.FieldDescriptor.Type.INT64;
35+
import static com.google.protobuf.Descriptors.FieldDescriptor.Type.MESSAGE;
36+
import static com.google.protobuf.Descriptors.FieldDescriptor.Type.SINT32;
37+
import static com.google.protobuf.Descriptors.FieldDescriptor.Type.SINT64;
38+
import static com.google.protobuf.Descriptors.FieldDescriptor.Type.STRING;
39+
40+
2941
class ProtobufData {
30-
private final Class<? extends com.google.protobuf.GeneratedMessageV3> clazz;
3142
private final Method newBuilder;
3243
private final Schema schema;
3344
private final String legacyName;
45+
public static final Descriptors.FieldDescriptor.Type[] PROTO_TYPES_WITH_DEFAULTS = new Descriptors.FieldDescriptor.Type[] { INT32, INT64, SINT32, SINT64, FLOAT, DOUBLE, BOOL, STRING, BYTES, ENUM };
3446
private HashMap<String, String> connectProtoNameMap = new HashMap<String, String>();
3547

3648
private GeneratedMessageV3.Builder getBuilder() {
@@ -69,8 +81,13 @@ private String getProtoFieldName(String descriptorForTypeName, String connectFie
6981
return connectProtoNameMap.get(getProtoMapKey(descriptorForTypeName, connectFieldName));
7082
}
7183

84+
private boolean isUnsetOneof(Descriptors.FieldDescriptor fieldDescriptor, Object value) {
85+
return fieldDescriptor.getContainingOneof() != null &&
86+
fieldDescriptor.getType() != MESSAGE &&
87+
fieldDescriptor.getDefaultValue().equals(value);
88+
}
89+
7290
ProtobufData(Class<? extends com.google.protobuf.GeneratedMessageV3> clazz, String legacyName) {
73-
this.clazz = clazz;
7491
this.legacyName = legacyName;
7592

7693
try {
@@ -209,10 +226,6 @@ private boolean isProtobufDate(Schema schema) {
209226
}
210227

211228
Object toConnectData(Schema schema, Object value) {
212-
if (value == null) {
213-
return null;
214-
}
215-
216229
try {
217230
if (isProtobufTimestamp(schema)) {
218231
com.google.protobuf.Timestamp timestamp = (com.google.protobuf.Timestamp) value;
@@ -311,14 +324,20 @@ Object toConnectData(Schema schema, Object value) {
311324

312325
case STRUCT: {
313326
final Message message = (Message) value; // Validate type
327+
if (message == message.getDefaultInstanceForType()) {
328+
return null;
329+
}
330+
314331
final Struct result = new Struct(schema.schema());
315-
final Map<Descriptors.FieldDescriptor, Object> fieldsMap = message.getAllFields();
316-
for (Map.Entry<Descriptors.FieldDescriptor, Object> pair : fieldsMap.entrySet()) {
317-
final Descriptors.FieldDescriptor fieldDescriptor = pair.getKey();
332+
333+
for (Descriptors.FieldDescriptor fieldDescriptor : message.getDescriptorForType().getFields()) {
318334
final String fieldName = getConnectFieldName(fieldDescriptor);
319335
final Field field = schema.field(fieldName);
320-
final Object obj = pair.getValue();
321-
result.put(fieldName, toConnectData(field.schema(), obj));
336+
337+
Object obj = message.getField(fieldDescriptor);
338+
if (!isUnsetOneof(fieldDescriptor, obj)) {
339+
result.put(fieldName, toConnectData(field.schema(), obj));
340+
}
322341
}
323342

324343
converted = result;

0 commit comments

Comments
 (0)