Search in sources :

Example 11 with MessageElement

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());
}
Also used : DescriptorProtos(com.google.protobuf.DescriptorProtos) QuaternionProto(com.google.type.QuaternionProto) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) DescriptorValidationException(com.google.protobuf.Descriptors.DescriptorValidationException) Service(com.squareup.wire.schema.Service) Type(com.squareup.wire.schema.Type) EmptyProto(com.google.protobuf.EmptyProto) Map(java.util.Map) MethodDescriptorProto(com.google.protobuf.DescriptorProtos.MethodDescriptorProto) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) PhoneNumberProto(com.google.type.PhoneNumberProto) ExprProto(com.google.type.ExprProto) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) DayOfWeek(com.google.type.DayOfWeek) IntRange(kotlin.ranges.IntRange) ExtensionsElement(com.squareup.wire.schema.internal.parser.ExtensionsElement) Predicate(java.util.function.Predicate) MonthProto(com.google.type.MonthProto) Set(java.util.Set) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) ProtoType(com.squareup.wire.schema.ProtoType) Collectors(java.util.stream.Collectors) EnumConstant(com.squareup.wire.schema.EnumConstant) StructProto(com.google.protobuf.StructProto) IntervalProto(com.google.type.IntervalProto) Objects(java.util.Objects) DurationProto(com.google.protobuf.DurationProto) FieldMaskProto(com.google.protobuf.FieldMaskProto) List(java.util.List) EnumConstantElement(com.squareup.wire.schema.internal.parser.EnumConstantElement) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) PostalAddressProto(com.google.type.PostalAddressProto) EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) Optional(java.util.Optional) SourceContextProto(com.google.protobuf.SourceContextProto) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement) EnumValueDescriptorProto(com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto) ColorProto(com.google.type.ColorProto) TypeProto(com.google.protobuf.TypeProto) FileOptions(com.google.protobuf.DescriptorProtos.FileOptions) TimeOfDayProto(com.google.type.TimeOfDayProto) Schema(com.squareup.wire.schema.Schema) OneOf(com.squareup.wire.schema.OneOf) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Rpc(com.squareup.wire.schema.Rpc) Location(com.squareup.wire.schema.Location) ImmutableList(com.google.common.collect.ImmutableList) Syntax(com.squareup.wire.Syntax) MoneyProto(com.google.type.MoneyProto) RpcElement(com.squareup.wire.schema.internal.parser.RpcElement) AnyProto(com.google.protobuf.AnyProto) MessageType(com.squareup.wire.schema.MessageType) LinkedHashSet(java.util.LinkedHashSet) LOWER_UNDERSCORE(com.google.common.base.CaseFormat.LOWER_UNDERSCORE) WrappersProto(com.google.protobuf.WrappersProto) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) DateProto(com.google.type.DateProto) ProtoFile(com.squareup.wire.schema.ProtoFile) ApiProto(com.google.protobuf.ApiProto) LatLng(com.google.type.LatLng) UPPER_CAMEL(com.google.common.base.CaseFormat.UPPER_CAMEL) CalendarPeriodProto(com.google.type.CalendarPeriodProto) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) TreeMap(java.util.TreeMap) TimestampProto(com.google.protobuf.TimestampProto) Field(com.squareup.wire.schema.Field) ServiceElement(com.squareup.wire.schema.internal.parser.ServiceElement) Options(com.squareup.wire.schema.Options) LocalizedTextProto(com.google.type.LocalizedTextProto) EnumType(com.squareup.wire.schema.EnumType) Comparator(java.util.Comparator) DescriptorProto(com.google.protobuf.DescriptorProtos.DescriptorProto) Collections(java.util.Collections) MethodOptions(com.google.protobuf.DescriptorProtos.MethodOptions) FractionProto(com.google.type.FractionProto) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) ImmutableList(com.google.common.collect.ImmutableList) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ExtensionsElement(com.squareup.wire.schema.internal.parser.ExtensionsElement) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) MethodDescriptorProto(com.google.protobuf.DescriptorProtos.MethodDescriptorProto) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) EnumValueDescriptorProto(com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) DescriptorProto(com.google.protobuf.DescriptorProtos.DescriptorProto) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) IntRange(kotlin.ranges.IntRange) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement)

Example 12 with MessageElement

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);
        }
    }
}
Also used : HashMap(java.util.HashMap) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) Range(com.google.common.collect.Range) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement) HashSet(java.util.HashSet)

Example 13 with MessageElement

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());
}
Also used : NestedMessage(io.confluent.kafka.serializers.protobuf.test.NestedTestProto.NestedMessage) DynamicMessage(com.google.protobuf.DynamicMessage) MapReferencesMessage(io.confluent.connect.protobuf.test.MapReferences.MapReferencesMessage) Message(com.google.protobuf.Message) Schema(org.apache.kafka.connect.data.Schema) ConnectSchema(org.apache.kafka.connect.data.ConnectSchema) ProtobufSchema(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) ProtobufSchema(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) DynamicMessage(com.google.protobuf.DynamicMessage) Struct(org.apache.kafka.connect.data.Struct) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) List(java.util.List) ArrayList(java.util.ArrayList) Descriptors(com.google.protobuf.Descriptors) ProtobufSchemaAndValue(io.confluent.kafka.serializers.protobuf.ProtobufSchemaAndValue) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor) Test(org.junit.Test)

Example 14 with MessageElement

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);
}
Also used : NestedMessage(io.confluent.kafka.serializers.protobuf.test.NestedTestProto.NestedMessage) DynamicMessage(com.google.protobuf.DynamicMessage) MapReferencesMessage(io.confluent.connect.protobuf.test.MapReferences.MapReferencesMessage) Message(com.google.protobuf.Message) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) Descriptors(com.google.protobuf.Descriptors) TestMessageProtos(io.confluent.kafka.serializers.protobuf.test.TestMessageProtos) ProtobufSchemaAndValue(io.confluent.kafka.serializers.protobuf.ProtobufSchemaAndValue) ProtobufSchemaAndValue(io.confluent.kafka.serializers.protobuf.ProtobufSchemaAndValue) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

Example 15 with MessageElement

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);
}
Also used : TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) ArrayList(java.util.ArrayList) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement)

Aggregations

MessageElement (com.squareup.wire.schema.internal.parser.MessageElement)30 TypeElement (com.squareup.wire.schema.internal.parser.TypeElement)25 EnumElement (com.squareup.wire.schema.internal.parser.EnumElement)21 FieldElement (com.squareup.wire.schema.internal.parser.FieldElement)14 ProtoFileElement (com.squareup.wire.schema.internal.parser.ProtoFileElement)14 ServiceElement (com.squareup.wire.schema.internal.parser.ServiceElement)14 HashSet (java.util.HashSet)14 OptionElement (com.squareup.wire.schema.internal.parser.OptionElement)12 OneOfElement (com.squareup.wire.schema.internal.parser.OneOfElement)11 ArrayList (java.util.ArrayList)11 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)10 Syntax (com.squareup.wire.Syntax)10 ImmutableList (com.google.common.collect.ImmutableList)9 DescriptorProto (com.google.protobuf.DescriptorProtos.DescriptorProto)9 EnumDescriptorProto (com.google.protobuf.DescriptorProtos.EnumDescriptorProto)9 EnumValueDescriptorProto (com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto)9 FieldDescriptorProto (com.google.protobuf.DescriptorProtos.FieldDescriptorProto)9 FileDescriptorProto (com.google.protobuf.DescriptorProtos.FileDescriptorProto)9 MethodDescriptorProto (com.google.protobuf.DescriptorProtos.MethodDescriptorProto)9 OneofDescriptorProto (com.google.protobuf.DescriptorProtos.OneofDescriptorProto)9