Search in sources :

Example 11 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 12 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 13 with ExtensionRegistry

use of com.google.protobuf.ExtensionRegistry in project toolkit by googleapis.

the class GapicTestConfig method getDescriptor.

/**
 * Returns the file descriptor set generated from the sources of this api.
 */
@Override
public FileDescriptorSet getDescriptor() throws IOException {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    AnnotationsProto.registerAllExtensions(registry);
    AuthProto.registerAllExtensions(registry);
    return FileDescriptorSet.parseFrom(Files.newInputStream(getDescriptorFile()), registry);
}
Also used : ExtensionRegistry(com.google.protobuf.ExtensionRegistry)

Example 14 with ExtensionRegistry

use of com.google.protobuf.ExtensionRegistry in project drools by kiegroup.

the class PersisterHelper method buildRegistry.

public static ExtensionRegistry buildRegistry(MarshallerReaderContext context, ProcessMarshaller processMarshaller) {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    if (processMarshaller != null) {
        context.parameterObject = registry;
        processMarshaller.init(context);
    }
    return registry;
}
Also used : ExtensionRegistry(com.google.protobuf.ExtensionRegistry)

Example 15 with ExtensionRegistry

use of com.google.protobuf.ExtensionRegistry in project jesos by groupon.

the class TestProtobufRegistry method testRoundtrip.

@Test
public void testRoundtrip() throws Exception {
    ExtensionRegistry e = ProtobufRegistry.INSTANCE.mutableExtensionRegistry();
    TestProtos.registerAllExtensions(e);
    ExtBase extBase = ExtBase.newBuilder().setA("172.42.1.2").setB("br0").setD(24).setC("172.42.1.1").setE("00:11:22:33:44:55").build();
    Base base = Base.newBuilder().setExtension(ExtBase.type, extBase).setType(Base.Type.EXT_BASE).build();
    byte[] bytes = base.toByteArray();
    Base generated = Base.parseFrom(new ByteArrayInputStream(bytes), e);
    ExtBase extGenerated = generated.getExtension(ExtBase.type);
    assertEquals(extBase.getA(), extGenerated.getA());
    assertEquals(extBase.getB(), extGenerated.getB());
    assertEquals(extBase.getC(), extGenerated.getC());
    assertEquals(extBase.getD(), extGenerated.getD());
    assertEquals(extBase.getE(), extGenerated.getE());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ExtBase(com.groupon.jesos.TestProtos.ExtBase) ExtBase(com.groupon.jesos.TestProtos.ExtBase) Base(com.groupon.jesos.TestProtos.Base) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) Test(org.junit.Test)

Aggregations

ExtensionRegistry (com.google.protobuf.ExtensionRegistry)29 ByteArrayInputStream (java.io.ByteArrayInputStream)10 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 IOException (java.io.IOException)2 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