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

Nested oneof #1

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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 @@ -73,19 +73,23 @@ public Schema toConnectSchema(@NonNull final Message message) {
builder.version(CONVERTER_VERSION);
builder.parameter(PROTOBUF_PACKAGE, descriptor.getFile().getPackage());

processFieldDescriptors(builder, fieldDescriptorList);

return builder.build();
}

private void processFieldDescriptors(SchemaBuilder builder, List<Descriptors.FieldDescriptor> fieldDescriptorList) {
for (final Descriptors.FieldDescriptor fieldDescriptor : fieldDescriptorList) {
if (fieldDescriptor.getRealContainingOneof() != null) {
Descriptors.OneofDescriptor oneofDescriptor = fieldDescriptor.getRealContainingOneof();
if (!builder.fields().stream().anyMatch(field -> field.name().equals(oneofDescriptor.getName()))) {
if (builder.fields().stream().noneMatch(field -> field.name().equals(oneofDescriptor.getName()))) {
builder.field(oneofDescriptor.getName(), toConnectSchemaForOneOfField(oneofDescriptor));
}
continue;
} else {
final String fieldName = fieldDescriptor.getName();
builder.field(fieldName, toConnectSchemaForField(fieldDescriptor));
}
final String fieldName = fieldDescriptor.getName();
builder.field(fieldName, toConnectSchemaForField(fieldDescriptor));
}

return builder.build();
}

private Schema toConnectSchemaForField(final Descriptors.FieldDescriptor fieldDescriptor) {
Expand Down Expand Up @@ -204,9 +208,7 @@ private SchemaBuilder toConnectSchemaBuilderForField(final Descriptors.FieldDesc

String fullName = fieldDescriptor.getMessageType().getFullName();
schemaBuilder = SchemaBuilder.struct().name(fullName);
for (Descriptors.FieldDescriptor field : fieldDescriptor.getMessageType().getFields()) {
schemaBuilder.field(field.getName(), toConnectSchemaForField(field));
}
processFieldDescriptors(schemaBuilder, fieldDescriptor.getMessageType().getFields());
break;
}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ private static Stream<Arguments> getToConnectTestCases() {
Arguments.of(ToConnectTestDataGenerator.getOneofProtobufMessages().get(0),
ToConnectTestDataGenerator.getOneofSchema(PACKAGE_NAME),
ToConnectTestDataGenerator.getOneofTypeData(PACKAGE_NAME)),
Arguments.of(ToConnectTestDataGenerator.getNestedOneofProtobufMessages().get(0),
ToConnectTestDataGenerator.getNestedOneOfSchema(PACKAGE_NAME),
ToConnectTestDataGenerator.getNestedOneOfTypeData(PACKAGE_NAME)),
Arguments.of(ToConnectTestDataGenerator.getAllTypesProtobufMessages().get(0),
ToConnectTestDataGenerator.getAllTypesSchema(PACKAGE_NAME),
ToConnectTestDataGenerator.getAllTypesData(PACKAGE_NAME))
Expand Down Expand Up @@ -293,4 +296,4 @@ public void testSchemaCache_fromConnectConversion() {
protobufSchemaConverter.fromConnectData(TOPIC_NAME, connectEnumSchema, connectEnumData);
assertEquals(2, fromConnectSchemaCache.size());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.amazonaws.services.schemaregistry.kafkaconnect.tests.syntax3.ArrayTypeSyntax3;
import com.amazonaws.services.schemaregistry.kafkaconnect.tests.syntax3.MapTypeSyntax3;
import com.amazonaws.services.schemaregistry.kafkaconnect.tests.syntax3.NestedTypeSyntax3;
import com.amazonaws.services.schemaregistry.kafkaconnect.tests.syntax3.NestedOneofTypeSyntax3;
import com.amazonaws.services.schemaregistry.kafkaconnect.tests.syntax3.OneofTypeSyntax3;
import com.amazonaws.services.schemaregistry.kafkaconnect.tests.syntax3.PrimitiveTypesSyntax3;
import com.amazonaws.services.schemaregistry.kafkaconnect.tests.syntax2.EnumTypeSyntax2;
Expand Down Expand Up @@ -619,7 +620,10 @@ public static Struct getStructTypeData(String packageName) {
final Struct connectData = new Struct(connectSchema);

connectData
.put("address", new Struct(connectSchema.field("address").schema()).put("street", "8th").put("zipcode", 98121))
.put("address",
new Struct(connectSchema.field("address").schema())
.put("street", "8th")
.put("zipcode", 98121))
.put("status", "VALID")
.put("customer", new Struct(connectSchema.field("customer").schema()).put("name", "joe"))
.put("mapping", ImmutableMap.of("hello", true))
Expand Down Expand Up @@ -702,6 +706,62 @@ private static Map<String, Schema> getOneofType() {
.build();
}

public static List<Message> getNestedOneofProtobufMessages() {
NestedOneofTypeSyntax3.Metadata metadata = NestedOneofTypeSyntax3.Metadata.newBuilder()
.setMetadataId("hi")
.setRegistered("yup")
.build();
return Arrays.asList(
NestedOneofTypeSyntax3.Event.newBuilder()
.setMetadata(metadata)
.setEventId("123123")
.build()
);
}

public static Schema getNestedOneOfSchema(String packageName) {
return createConnectSchema(
"Event",
getNestedOneOfType(packageName),
ImmutableMap.of(PROTOBUF_PACKAGE, packageName)
);
}

public static Struct getNestedOneOfTypeData(String packageName) {
final Schema connectSchema = getNestedOneOfSchema(packageName);
final Struct connectData = new Struct(connectSchema);

connectData
.put("metadata",
new Struct(connectSchema.field("metadata").schema())
.put("metadata_id", "hi")
.put("status",
new Struct(connectSchema.field("metadata").schema().field("status").schema())
.put("registered", "yup")
)
)
.put("event_id", "123123");
return connectData;
}

private static Map<String, Schema> getNestedOneOfType(String packageName) {
final SchemaBuilder metadataBuilder = SchemaBuilder.struct().name(getFullName(packageName, "Metadata"))
.field("metadata_id", SchemaBuilder.string().parameter(PROTOBUF_TAG, "1").build())
.field("status", SchemaBuilder.struct()
.name("status")
.field("registered", SchemaBuilder.string().parameter(PROTOBUF_TAG, "2").optional().build())
.field("unregistered", SchemaBuilder.string().parameter(PROTOBUF_TAG, "3").optional().build())
.parameter("protobuf.type", "oneof")
.optional()
.build()
);

return ImmutableMap.<String, Schema> builder()
.put("metadata", metadataBuilder.parameter(PROTOBUF_TAG, "1").build())
.put("event_id", SchemaBuilder.string().parameter(PROTOBUF_TAG, "2").build())
.build();
}

public static List<Message> getAllTypesProtobufMessages() {

com.google.type.Date.Builder dateBuilder = com.google.type.Date.newBuilder();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
syntax = "proto3";

package com.amazonaws.services.schemaregistry.kafkaconnect.tests.syntax3;

message Metadata {
string metadata_id = 1;
oneof status {
string registered = 2;
string unregistered = 3;
}
}

message Event {
.com.amazonaws.services.schemaregistry.kafkaconnect.tests.syntax3.Metadata metadata = 1;
string event_id = 2;
}