Search in sources :

Example 1 with Descriptor

use of com.google.protobuf.Descriptors.Descriptor in project jvm-serializers by eishay.

the class JsonFormat method mergeField.

/**
     * Parse a single field from {@code tokenizer} and merge it into {@code builder}. If a ',' is
     * detected after the field ends, the next field will be parsed automatically
     */
private static void mergeField(Tokenizer tokenizer, ExtensionRegistry extensionRegistry, Message.Builder builder) throws ParseException {
    FieldDescriptor field;
    Descriptor type = builder.getDescriptorForType();
    ExtensionRegistry.ExtensionInfo extension = null;
    if (tokenizer.tryConsume("[")) {
        // An extension.
        StringBuilder name = new StringBuilder(tokenizer.consumeIdentifier());
        while (tokenizer.tryConsume(".")) {
            name.append(".");
            name.append(tokenizer.consumeIdentifier());
        }
        extension = extensionRegistry.findExtensionByName(name.toString());
        if (extension == null) {
            throw tokenizer.parseExceptionPreviousToken("Extension \"" + name + "\" not found in the ExtensionRegistry.");
        } else if (extension.descriptor.getContainingType() != type) {
            throw tokenizer.parseExceptionPreviousToken("Extension \"" + name + "\" does not extend message type \"" + type.getFullName() + "\".");
        }
        tokenizer.consume("]");
        field = extension.descriptor;
    } else {
        String name = tokenizer.consumeIdentifier();
        field = type.findFieldByName(name);
        // names.
        if (field == null) {
            // Explicitly specify US locale so that this code does not break when
            // executing in Turkey.
            String lowerName = name.toLowerCase(Locale.US);
            field = type.findFieldByName(lowerName);
            // If the case-insensitive match worked but the field is NOT a group,
            if ((field != null) && (field.getType() != FieldDescriptor.Type.GROUP)) {
                field = null;
            }
        }
        // Again, special-case group names as described above.
        if ((field != null) && (field.getType() == FieldDescriptor.Type.GROUP) && !field.getMessageType().getName().equals(name)) {
            field = null;
        }
        if (field == null) {
            throw tokenizer.parseExceptionPreviousToken("Message type \"" + type.getFullName() + "\" has no field named \"" + name + "\".");
        }
    }
    tokenizer.consume(":");
    boolean array = tokenizer.tryConsume("[");
    if (array) {
        while (!tokenizer.tryConsume("]")) {
            handleValue(tokenizer, extensionRegistry, builder, field, extension);
            tokenizer.tryConsume(",");
        }
    } else {
        handleValue(tokenizer, extensionRegistry, builder, field, extension);
    }
    if (tokenizer.tryConsume(",")) {
        // Continue with the next field
        mergeField(tokenizer, extensionRegistry, builder);
    }
}
Also used : Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor) ByteString(com.google.protobuf.ByteString) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) ExtensionRegistry(com.google.protobuf.ExtensionRegistry)

Example 2 with Descriptor

use of com.google.protobuf.Descriptors.Descriptor in project j2objc by google.

the class CompatibilityTest method testNewBuilderForField.

public void testNewBuilderForField() throws Exception {
    Descriptor descriptor = TypicalData.Builder.getDescriptor();
    FieldDescriptor fieldDescriptor = descriptor.findFieldByNumber(11);
    TypicalData.Builder dataBuilder = TypicalData.newBuilder();
    TypicalDataMessage.Builder messageBuilder = (TypicalDataMessage.Builder) dataBuilder.newBuilderForField(fieldDescriptor);
    TypicalDataMessage message = messageBuilder.setMyMessageInt(10).build();
    assertEquals(10, message.getMyMessageInt());
    fieldDescriptor = descriptor.findFieldByNumber(1);
    try {
        dataBuilder.newBuilderForField(fieldDescriptor);
        fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
    // Expected.
    }
}
Also used : TypicalDataMessage(protos.TypicalDataMessage) TypicalData(protos.TypicalData) ExtendableMessageOrBuilder(com.google.protobuf.GeneratedMessage.ExtendableMessageOrBuilder) TypicalDataOrBuilder(protos.TypicalDataOrBuilder) MsgWithDefaultsOrBuilder(protos.MsgWithDefaultsOrBuilder) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 3 with Descriptor

use of com.google.protobuf.Descriptors.Descriptor in project j2objc by google.

the class CompatibilityTest method testExtensionRegistry.

public void testExtensionRegistry() throws Exception {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    Typical.registerAllExtensions(registry);
    Descriptor descriptor = TypicalData.Builder.getDescriptor();
    FieldDescriptor fieldDescriptor = descriptor.findFieldByNumber(1);
    assertFalse(fieldDescriptor.isExtension());
    ExtensionRegistry.ExtensionInfo extensionInfo = registry.findExtensionByNumber(descriptor, 1000);
    assertNotNull(extensionInfo);
    FieldDescriptor extensionFieldDescriptor = extensionInfo.descriptor;
    assertNotNull(extensionFieldDescriptor);
    assertEquals(1000, extensionFieldDescriptor.getNumber());
    assertTrue(extensionFieldDescriptor.isExtension());
    Message message = extensionInfo.defaultInstance;
    assertTrue(message instanceof TypicalDataMessage);
    TypicalDataMessage data = ((TypicalDataMessage.Builder) message.toBuilder()).setMyMessageInt(100).build();
    assertEquals(100, data.getMyMessageInt());
    // Primitive extension
    extensionInfo = registry.findExtensionByNumber(descriptor, 1001);
    assertNotNull(extensionInfo);
    extensionFieldDescriptor = extensionInfo.descriptor;
    assertNotNull(extensionFieldDescriptor);
    assertEquals(1001, extensionFieldDescriptor.getNumber());
    assertTrue(extensionFieldDescriptor.isExtension());
    assertNull(extensionInfo.defaultInstance);
}
Also used : TypicalDataMessage(protos.TypicalDataMessage) GeneratedMessage(com.google.protobuf.GeneratedMessage) TypicalDataMessage(protos.TypicalDataMessage) Message(com.google.protobuf.Message) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 4 with Descriptor

use of com.google.protobuf.Descriptors.Descriptor in project j2objc by google.

the class CompatibilityTest method testEmptyFieldOptions.

public void testEmptyFieldOptions() {
    Descriptor descriptor = TypicalData.Builder.getDescriptor();
    FieldDescriptor intField = descriptor.findFieldByNumber(1);
    assertNotNull(intField.getOptions());
}
Also used : Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 5 with Descriptor

use of com.google.protobuf.Descriptors.Descriptor in project j2objc by google.

the class CompatibilityTest method testGetAllFields.

public void testGetAllFields() throws Exception {
    GeneratedMessage data = TypicalData.newBuilder().setMyInt(1).addRepeatedInt32(2).setExtension(Typical.myExtension, TypicalDataMessage.getDefaultInstance()).setExtension(Typical.myPrimitiveExtension, 3).build();
    Map<FieldDescriptor, Object> allFields = data.getAllFields();
    assertEquals(4, allFields.size());
    assertNotNull(allFields.get(Typical.myExtension.getDescriptor()));
    assertEquals(4, data.toBuilder().getAllFields().size());
    Descriptor descriptor = TypicalData.Builder.getDescriptor();
    FieldDescriptor intField = descriptor.findFieldByNumber(1);
    assertEquals(1, allFields.get(intField));
    assertEquals(3, allFields.get(Typical.myPrimitiveExtension.getDescriptor()));
}
Also used : Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor) GeneratedMessage(com.google.protobuf.GeneratedMessage) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Aggregations

Descriptor (com.google.protobuf.Descriptors.Descriptor)25 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)22 EnumDescriptor (com.google.protobuf.Descriptors.EnumDescriptor)13 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)13 ExtensionRegistry (com.google.protobuf.ExtensionRegistry)4 TypicalData (protos.TypicalData)4 ArrayList (java.util.ArrayList)3 TypicalDataMessage (protos.TypicalDataMessage)3 GenericDescriptor (com.google.protobuf.Descriptors.GenericDescriptor)2 GeneratedMessage (com.google.protobuf.GeneratedMessage)2 HashSet (java.util.HashSet)2 ByteString (com.google.protobuf.ByteString)1 Descriptors (com.google.protobuf.Descriptors)1 Type (com.google.protobuf.Descriptors.FieldDescriptor.Type)1 ExtensionRegistryLite (com.google.protobuf.ExtensionRegistryLite)1 ExtendableMessageOrBuilder (com.google.protobuf.GeneratedMessage.ExtendableMessageOrBuilder)1 Message (com.google.protobuf.Message)1 KnownTypes.getDescriptor (io.spine.type.KnownTypes.getDescriptor)1 TypeUrl (io.spine.type.TypeUrl)1 NonDeterministicException (org.apache.beam.sdk.coders.Coder.NonDeterministicException)1