use of com.squareup.wire.schema.internal.parser.MessageElement in project aws-glue-schema-registry by awslabs.
the class FileDescriptorUtils method toMessage.
private static MessageElement toMessage(FileDescriptorProto file, DescriptorProto descriptor) {
String name = descriptor.getName();
ImmutableList.Builder<FieldElement> fields = ImmutableList.builder();
ImmutableList.Builder<TypeElement> nested = ImmutableList.builder();
ImmutableList.Builder<ReservedElement> reserved = ImmutableList.builder();
ImmutableList.Builder<ExtensionsElement> extensions = ImmutableList.builder();
LinkedHashMap<String, ImmutableList.Builder<FieldElement>> oneofsMap = new LinkedHashMap<>();
for (OneofDescriptorProto od : descriptor.getOneofDeclList()) {
oneofsMap.put(od.getName(), ImmutableList.builder());
}
List<Map.Entry<String, ImmutableList.Builder<FieldElement>>> oneofs = new ArrayList<>(oneofsMap.entrySet());
List<FieldElement> proto3OptionalFields = new ArrayList<>();
for (FieldDescriptorProto fd : descriptor.getFieldList()) {
if (fd.hasProto3Optional()) {
proto3OptionalFields.add(toField(file, fd, false));
continue;
}
if (fd.hasOneofIndex()) {
FieldElement field = toField(file, fd, true);
oneofs.get(fd.getOneofIndex()).getValue().add(field);
} else {
FieldElement field = toField(file, fd, false);
fields.add(field);
}
}
fields.addAll(proto3OptionalFields);
for (DescriptorProto nestedDesc : descriptor.getNestedTypeList()) {
MessageElement nestedMessage = toMessage(file, nestedDesc);
nested.add(nestedMessage);
}
for (EnumDescriptorProto nestedDesc : descriptor.getEnumTypeList()) {
EnumElement nestedEnum = toEnum(nestedDesc);
nested.add(nestedEnum);
}
for (String reservedName : descriptor.getReservedNameList()) {
ReservedElement reservedElem = new ReservedElement(DEFAULT_LOCATION, "", Collections.singletonList(reservedName));
reserved.add(reservedElem);
}
for (DescriptorProto.ReservedRange reservedRange : descriptor.getReservedRangeList()) {
List<IntRange> values = new ArrayList<>();
int start = reservedRange.getStart();
int end = reservedRange.getEnd() - 1;
values.add(new IntRange(start, end));
ReservedElement reservedElem = new ReservedElement(DEFAULT_LOCATION, "", values);
reserved.add(reservedElem);
}
for (DescriptorProto.ExtensionRange extensionRange : descriptor.getExtensionRangeList()) {
List<IntRange> values = new ArrayList<>();
int start = extensionRange.getStart();
int end = extensionRange.getEnd() - 1;
values.add(new IntRange(start, end));
ExtensionsElement extensionsElement = new ExtensionsElement(DEFAULT_LOCATION, "", values);
extensions.add(extensionsElement);
}
ImmutableList.Builder<OptionElement> options = ImmutableList.builder();
if (descriptor.getOptions().hasMapEntry()) {
OptionElement option = new OptionElement(MAP_ENTRY_OPTION, booleanKind, descriptor.getOptions().getMapEntry(), false);
options.add(option);
}
if (descriptor.getOptions().hasNoStandardDescriptorAccessor()) {
OptionElement option = new OptionElement(NO_STANDARD_DESCRIPTOR_OPTION, booleanKind, descriptor.getOptions().getNoStandardDescriptorAccessor(), false);
options.add(option);
}
return new MessageElement(DEFAULT_LOCATION, name, "", nested.build(), options.build(), reserved.build(), fields.build(), oneofs.stream().filter(e -> e.getValue().build().size() != 0).map(e -> toOneof(e.getKey(), e.getValue())).collect(Collectors.toList()), extensions.build(), Collections.emptyList());
}
use of com.squareup.wire.schema.internal.parser.MessageElement in project aws-glue-schema-registry by awslabs.
the class ProtobufFile method processMessageElement.
@SuppressWarnings({ "rawtypes", "unchecked" })
private void processMessageElement(String scope, MessageElement messageElement) {
// reservedFields
Set<Object> reservedFieldSet = new HashSet<>();
for (ReservedElement reservedElement : messageElement.getReserveds()) {
for (Object value : reservedElement.getValues()) {
if (value instanceof Range) {
reservedFieldSet.addAll(ContiguousSet.create((Range) value, DiscreteDomain.integers()));
} else {
reservedFieldSet.add(value);
}
}
}
if (!reservedFieldSet.isEmpty()) {
reservedFields.put(scope + messageElement.getName(), reservedFieldSet);
}
// fieldMap, mapMap, FieldsIDName
Map<String, FieldElement> fieldTypeMap = new HashMap<>();
Map<String, FieldElement> mapMap = new HashMap<>();
Map<Integer, String> idsToNames = new HashMap<>();
for (FieldElement fieldElement : messageElement.getFields()) {
fieldTypeMap.put(fieldElement.getName(), fieldElement);
if (fieldElement.getType().startsWith("map<")) {
mapMap.put(fieldElement.getName(), fieldElement);
}
idsToNames.put(fieldElement.getTag(), fieldElement.getName());
}
for (OneOfElement oneOfElement : messageElement.getOneOfs()) {
for (FieldElement fieldElement : oneOfElement.getFields()) {
fieldTypeMap.put(fieldElement.getName(), fieldElement);
if (fieldElement.getType().startsWith("map<")) {
mapMap.put(fieldElement.getName(), fieldElement);
}
idsToNames.put(fieldElement.getTag(), fieldElement.getName());
}
}
if (!fieldTypeMap.isEmpty()) {
fieldMap.put(scope + messageElement.getName(), fieldTypeMap);
}
if (!mapMap.isEmpty()) {
this.mapMap.put(scope + messageElement.getName(), mapMap);
}
if (!idsToNames.isEmpty()) {
fieldsById.put(scope + messageElement.getName(), idsToNames);
}
// nonReservedFields
Set<Object> fieldKeySet = new HashSet<>();
for (FieldElement fieldElement : messageElement.getFields()) {
fieldKeySet.add(fieldElement.getTag());
fieldKeySet.add(fieldElement.getName());
}
for (OneOfElement oneOfElement : messageElement.getOneOfs()) {
for (FieldElement fieldElement : oneOfElement.getFields()) {
fieldKeySet.add(fieldElement.getTag());
fieldKeySet.add(fieldElement.getName());
}
}
if (!fieldKeySet.isEmpty()) {
nonReservedFields.put(scope + messageElement.getName(), fieldKeySet);
}
for (TypeElement typeElement : messageElement.getNestedTypes()) {
if (typeElement instanceof MessageElement) {
processMessageElement(messageElement.getName() + ".", (MessageElement) typeElement);
} else if (typeElement instanceof EnumElement) {
processEnumElement(messageElement.getName() + ".", (EnumElement) typeElement);
}
}
}
use of com.squareup.wire.schema.internal.parser.MessageElement in project schema-registry by confluentinc.
the class ProtobufDataTest method testFromConnectComplex.
@Test
public void testFromConnectComplex() {
Schema schema = SchemaBuilder.struct().field("int8", SchemaBuilder.int8().defaultValue((byte) 2).doc("int8 field").build()).field("int16", Schema.INT16_SCHEMA).field("int32", Schema.INT32_SCHEMA).field("int64", Schema.INT64_SCHEMA).field("float32", Schema.FLOAT32_SCHEMA).field("float64", Schema.FLOAT64_SCHEMA).field("boolean", Schema.BOOLEAN_SCHEMA).field("string", Schema.STRING_SCHEMA).field("bytes", Schema.BYTES_SCHEMA).field("array", SchemaBuilder.array(Schema.STRING_SCHEMA).build()).field("map", SchemaBuilder.map(Schema.STRING_SCHEMA, Schema.INT32_SCHEMA).build()).field("mapNonStringKeys", SchemaBuilder.map(Schema.INT32_SCHEMA, Schema.INT32_SCHEMA).build()).field("enum", SchemaBuilder.string().name("Status").optional().parameter(ProtobufData.PROTOBUF_TYPE_ENUM, "Status").parameter(ProtobufData.PROTOBUF_TYPE_ENUM_PREFIX + "ACTIVE", "0").parameter(ProtobufData.PROTOBUF_TYPE_ENUM_PREFIX + "INACTIVE", "1").build()).build();
Struct struct = new Struct(schema).put("int8", (byte) 12).put("int16", (short) 12).put("int32", 12).put("int64", 12L).put("float32", 12.2f).put("float64", 12.2).put("boolean", true).put("string", "foo").put("bytes", ByteBuffer.wrap("foo".getBytes())).put("array", Arrays.asList("a", "b", "c")).put("map", Collections.singletonMap("field", 1)).put("mapNonStringKeys", Collections.singletonMap(1, 1)).put("enum", "INACTIVE");
ProtobufSchemaAndValue convertedRecord = new ProtobufData().fromConnectData(schema, struct);
ProtobufSchema protobufSchema = convertedRecord.getSchema();
Message message = (Message) convertedRecord.getValue();
MessageElement messageElem = (MessageElement) protobufSchema.rawSchema().getTypes().get(0);
FieldElement fieldElem = messageElem.getFields().get(0);
assertEquals("int8", fieldElem.getName());
assertEquals("int32", fieldElem.getType());
fieldElem = messageElem.getFields().get(1);
assertEquals("int16", fieldElem.getName());
assertEquals("int32", fieldElem.getType());
fieldElem = messageElem.getFields().get(2);
assertEquals("int32", fieldElem.getName());
assertEquals("int32", fieldElem.getType());
fieldElem = messageElem.getFields().get(3);
assertEquals("int64", fieldElem.getName());
assertEquals("int64", fieldElem.getType());
fieldElem = messageElem.getFields().get(4);
assertEquals("float32", fieldElem.getName());
assertEquals("float", fieldElem.getType());
fieldElem = messageElem.getFields().get(5);
assertEquals("float64", fieldElem.getName());
assertEquals("double", fieldElem.getType());
fieldElem = messageElem.getFields().get(6);
assertEquals("boolean", fieldElem.getName());
assertEquals("bool", fieldElem.getType());
fieldElem = messageElem.getFields().get(7);
assertEquals("string", fieldElem.getName());
assertEquals("string", fieldElem.getType());
fieldElem = messageElem.getFields().get(8);
assertEquals("bytes", fieldElem.getName());
assertEquals("bytes", fieldElem.getType());
fieldElem = messageElem.getFields().get(9);
assertEquals("array", fieldElem.getName());
assertEquals("string", fieldElem.getType());
fieldElem = messageElem.getFields().get(10);
assertEquals("map", fieldElem.getName());
assertEquals("ConnectDefault2Entry", fieldElem.getType());
fieldElem = messageElem.getFields().get(11);
assertEquals("mapNonStringKeys", fieldElem.getName());
assertEquals("ConnectDefault3Entry", fieldElem.getType());
fieldElem = messageElem.getFields().get(12);
assertEquals("enum", fieldElem.getName());
assertEquals("Status", fieldElem.getType());
Descriptors.FieldDescriptor fieldDescriptor = message.getDescriptorForType().findFieldByName("int8");
assertEquals(12, message.getField(fieldDescriptor));
fieldDescriptor = message.getDescriptorForType().findFieldByName("int16");
assertEquals(12, message.getField(fieldDescriptor));
fieldDescriptor = message.getDescriptorForType().findFieldByName("int32");
assertEquals(12, message.getField(fieldDescriptor));
fieldDescriptor = message.getDescriptorForType().findFieldByName("int64");
assertEquals(12L, message.getField(fieldDescriptor));
fieldDescriptor = message.getDescriptorForType().findFieldByName("float32");
assertEquals(12.2f, message.getField(fieldDescriptor));
fieldDescriptor = message.getDescriptorForType().findFieldByName("float64");
assertEquals(12.2, message.getField(fieldDescriptor));
fieldDescriptor = message.getDescriptorForType().findFieldByName("boolean");
assertEquals(true, message.getField(fieldDescriptor));
fieldDescriptor = message.getDescriptorForType().findFieldByName("string");
assertEquals("foo", message.getField(fieldDescriptor));
fieldDescriptor = message.getDescriptorForType().findFieldByName("bytes");
assertEquals(ByteString.copyFrom("foo".getBytes()), message.getField(fieldDescriptor));
fieldDescriptor = message.getDescriptorForType().findFieldByName("array");
assertEquals(Arrays.asList("a", "b", "c"), message.getField(fieldDescriptor));
fieldDescriptor = message.getDescriptorForType().findFieldByName("map");
Object value = message.getField(fieldDescriptor);
DynamicMessage dynamicMessage = ((List<DynamicMessage>) value).get(0);
fieldDescriptor = dynamicMessage.getDescriptorForType().findFieldByName("key");
assertEquals("field", dynamicMessage.getField(fieldDescriptor));
fieldDescriptor = dynamicMessage.getDescriptorForType().findFieldByName("value");
assertEquals(1, dynamicMessage.getField(fieldDescriptor));
fieldDescriptor = message.getDescriptorForType().findFieldByName("mapNonStringKeys");
value = message.getField(fieldDescriptor);
dynamicMessage = ((List<DynamicMessage>) value).get(0);
fieldDescriptor = dynamicMessage.getDescriptorForType().findFieldByName("key");
assertEquals(1, dynamicMessage.getField(fieldDescriptor));
fieldDescriptor = dynamicMessage.getDescriptorForType().findFieldByName("value");
assertEquals(1, dynamicMessage.getField(fieldDescriptor));
fieldDescriptor = message.getDescriptorForType().findFieldByName("enum");
EnumValueDescriptor enumValueDescriptor = (EnumValueDescriptor) message.getField(fieldDescriptor);
assertEquals("INACTIVE", enumValueDescriptor.getName());
}
use of com.squareup.wire.schema.internal.parser.MessageElement in project schema-registry by confluentinc.
the class ProtobufDataTest method testRoundTripConnectPreserveSignedAndFixed.
@Test
public void testRoundTripConnectPreserveSignedAndFixed() throws Exception {
TestMessageProtos.TestMessage originalMessage = TestMessageProtos.TestMessage.newBuilder().setTestSint32(12).setTestSint64(12L).setTestFixed32(12).setTestFixed64(12L).setTestSfixed32(12).setTestSfixed64(12L).setTestUint32(12).setTestUint64(12L).build();
SchemaAndValue toConnectResult = getSchemaAndValue(originalMessage);
ProtobufData protobufData = new ProtobufData();
ProtobufSchemaAndValue fromConnectResult = protobufData.fromConnectData(toConnectResult.schema(), toConnectResult.value());
Message message = (Message) fromConnectResult.getValue();
MessageElement messageElem = (MessageElement) fromConnectResult.getSchema().rawSchema().getTypes().get(0);
FieldElement fieldElem = messageElem.getFields().get(5);
assertEquals("test_fixed32", fieldElem.getName());
assertEquals("fixed32", fieldElem.getType());
fieldElem = messageElem.getFields().get(6);
assertEquals("test_fixed64", fieldElem.getName());
assertEquals("fixed64", fieldElem.getType());
fieldElem = messageElem.getFields().get(9);
assertEquals("test_sfixed32", fieldElem.getName());
assertEquals("sfixed32", fieldElem.getType());
fieldElem = messageElem.getFields().get(10);
assertEquals("test_sfixed64", fieldElem.getName());
assertEquals("sfixed64", fieldElem.getType());
fieldElem = messageElem.getFields().get(11);
assertEquals("test_sint32", fieldElem.getName());
assertEquals("sint32", fieldElem.getType());
fieldElem = messageElem.getFields().get(12);
assertEquals("test_sint64", fieldElem.getName());
assertEquals("sint64", fieldElem.getType());
fieldElem = messageElem.getFields().get(13);
assertEquals("test_uint32", fieldElem.getName());
assertEquals("uint32", fieldElem.getType());
fieldElem = messageElem.getFields().get(14);
assertEquals("test_uint64", fieldElem.getName());
assertEquals("uint64", fieldElem.getType());
Descriptors.FieldDescriptor fieldDescriptor = message.getDescriptorForType().findFieldByName("test_fixed32");
assertEquals(12, message.getField(fieldDescriptor));
fieldDescriptor = message.getDescriptorForType().findFieldByName("test_fixed64");
assertEquals(12L, message.getField(fieldDescriptor));
fieldDescriptor = message.getDescriptorForType().findFieldByName("test_sfixed32");
assertEquals(12, message.getField(fieldDescriptor));
fieldDescriptor = message.getDescriptorForType().findFieldByName("test_sfixed64");
assertEquals(12L, message.getField(fieldDescriptor));
fieldDescriptor = message.getDescriptorForType().findFieldByName("test_sint32");
assertEquals(12, message.getField(fieldDescriptor));
fieldDescriptor = message.getDescriptorForType().findFieldByName("test_sint64");
assertEquals(12L, message.getField(fieldDescriptor));
fieldDescriptor = message.getDescriptorForType().findFieldByName("test_uint32");
assertEquals(12, message.getField(fieldDescriptor));
fieldDescriptor = message.getDescriptorForType().findFieldByName("test_uint64");
assertEquals(12L, message.getField(fieldDescriptor));
TestMessageProtos.TestMessage convertedMessage = TestMessageProtos.TestMessage.parseFrom(message.toByteArray());
assertEquals(originalMessage, convertedMessage);
}
use of com.squareup.wire.schema.internal.parser.MessageElement in project schema-registry by confluentinc.
the class ProtobufSchema method toMessageIndexes.
public MessageIndexes toMessageIndexes(String name) {
List<Integer> indexes = new ArrayList<>();
String[] parts = name.split("\\.");
List<TypeElement> types = schemaObj.getTypes();
for (String part : parts) {
int i = 0;
for (TypeElement type : types) {
if (type instanceof MessageElement) {
if (type.getName().equals(part)) {
indexes.add(i);
types = type.getNestedTypes();
break;
}
i++;
}
}
}
return new MessageIndexes(indexes);
}
Aggregations