Search in sources :

Example 1 with FieldDescriptor

use of com.google.protobuf.Descriptors.FieldDescriptor 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 FieldDescriptor

use of com.google.protobuf.Descriptors.FieldDescriptor 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 FieldDescriptor

use of com.google.protobuf.Descriptors.FieldDescriptor 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 FieldDescriptor

use of com.google.protobuf.Descriptors.FieldDescriptor 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 FieldDescriptor

use of com.google.protobuf.Descriptors.FieldDescriptor 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

FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)45 Descriptor (com.google.protobuf.Descriptors.Descriptor)18 Test (org.junit.Test)14 EnumDescriptor (com.google.protobuf.Descriptors.EnumDescriptor)11 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)11 BooleanFieldValidator (io.spine.validate.BooleanFieldValidator)9 ByteStringFieldValidator (io.spine.validate.ByteStringFieldValidator)9 FloatFieldValidator (io.spine.validate.FloatFieldValidator)9 LongFieldValidator (io.spine.validate.LongFieldValidator)9 StringFieldValidator (io.spine.validate.StringFieldValidator)9 TypicalData (protos.TypicalData)5 ArrayList (java.util.ArrayList)4 ByteString (com.google.protobuf.ByteString)3 ExtensionRegistry (com.google.protobuf.ExtensionRegistry)3 ValidationResult (io.spine.server.event.enrich.ReferenceValidator.ValidationResult)3 Map (java.util.Map)3 GeneratedMessage (com.google.protobuf.GeneratedMessage)2 Collection (java.util.Collection)2 TypicalDataMessage (protos.TypicalDataMessage)2 ImageAnnotatorClient (com.google.cloud.vision.spi.v1.ImageAnnotatorClient)1