Search in sources :

Example 1 with ExtensionRegistry

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

use of com.google.protobuf.ExtensionRegistry in project retrofit by square.

the class ProtoConverterFactoryTest method setUp.

@Before
public void setUp() {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(ProtoConverterFactory.create()).build();
    service = retrofit.create(Service.class);
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    PhoneProtos.registerAllExtensions(registry);
    Retrofit retrofitWithRegistry = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(ProtoConverterFactory.createWithRegistry(registry)).build();
    serviceWithRegistry = retrofitWithRegistry.create(ServiceWithRegistry.class);
}
Also used : Retrofit(retrofit2.Retrofit) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) Before(org.junit.Before)

Example 3 with ExtensionRegistry

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

use of com.google.protobuf.ExtensionRegistry in project j2objc by google.

the class CompatibilityTest method testEmptyFile.

public void testEmptyFile() throws Exception {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    // Should be a noop, test that this compiles.
    EmptyFile.registerAllExtensions(registry);
}
Also used : ExtensionRegistry(com.google.protobuf.ExtensionRegistry)

Example 5 with ExtensionRegistry

use of com.google.protobuf.ExtensionRegistry in project j2objc by google.

the class CompatibilityTest method testMergeAndParseDelimitedFromInputStream.

public void testMergeAndParseDelimitedFromInputStream() throws Exception {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    registry.add(Typical.myPrimitiveExtension);
    byte[] rawData = asBytes(new int[] { 0x0C, 0x08, 0x06, 0x60, 0x01, 0x7A, 0x03, 0x62, 0x61, 0x72, 0xC8, 0x3E, 0x2D, 0x0C, 0x08, 0x06, 0x60, 0x01, 0x7A, 0x03, 0x62, 0x61, 0x72, 0xC8, 0x3E, 0x2D });
    ByteArrayInputStream in = new ByteArrayInputStream(rawData);
    TypicalData.Builder dataBuilder = TypicalData.newBuilder();
    assertTrue(dataBuilder.mergeDelimitedFrom(in, registry));
    checkMergeAndParse(dataBuilder.build(), true);
    // Test that the second message reads correctly.
    dataBuilder = TypicalData.newBuilder();
    assertTrue(dataBuilder.mergeDelimitedFrom(in, registry));
    checkMergeAndParse(dataBuilder.build(), true);
    // Test the parseDelimitedFrom API.
    in = new ByteArrayInputStream(rawData);
    checkMergeAndParse(TypicalData.parseDelimitedFrom(in, registry), true);
    // Test that the second message reads correctly.
    checkMergeAndParse(TypicalData.parseDelimitedFrom(in, registry), true);
    // test API without ExtensionRegistry
    dataBuilder = TypicalData.newBuilder();
    assertTrue(dataBuilder.mergeDelimitedFrom(new ByteArrayInputStream(rawData)));
    checkMergeAndParse(dataBuilder.build(), false);
    checkMergeAndParse(TypicalData.parseDelimitedFrom(new ByteArrayInputStream(rawData)), false);
}
Also used : TypicalData(protos.TypicalData) ByteArrayInputStream(java.io.ByteArrayInputStream) ExtensionRegistry(com.google.protobuf.ExtensionRegistry)

Aggregations

ExtensionRegistry (com.google.protobuf.ExtensionRegistry)25 ByteArrayInputStream (java.io.ByteArrayInputStream)9 Descriptor (com.google.protobuf.Descriptors.Descriptor)4 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)4 MessageData (protos.MessageData)4 EnumDescriptor (com.google.protobuf.Descriptors.EnumDescriptor)3 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)3 EnumMsg (protos.EnumMsg)3 Test (org.junit.Test)2 PrimitiveFields (protos.PrimitiveFields)2 StringMsg (protos.StringMsg)2 TypicalData (protos.TypicalData)2 TypicalDataMessage (protos.TypicalDataMessage)2 ByteString (com.google.protobuf.ByteString)1 CodedInputStream (com.google.protobuf.CodedInputStream)1 GenericDescriptor (com.google.protobuf.Descriptors.GenericDescriptor)1 ExtensionRegistryLite (com.google.protobuf.ExtensionRegistryLite)1 GeneratedMessage (com.google.protobuf.GeneratedMessage)1 Message (com.google.protobuf.Message)1 Base (com.groupon.jesos.TestProtos.Base)1